325 - Identifying Legal Pascal Real Constants
Moderator: Board moderators
325 - Identifying Legal Pascal Real Constants
Does anybody have any idea of tricky input ?? I got so many times wrong answer...><
325 DFA? WHY WA?
It's just a simple DFA problem but the judge gave me WA.
Please help me.
Please help me.
Code: Select all
#include <iostream>
using namespace std;
char str[1000];
int trans[8][4] =
{
{1,2,9,9},
{9,2,9,9},
{9,2,3,5},
{9,4,9,9},
{9,4,9,5},
{6,7,9,9},
{9,7,9,9},
{9,7,9,9}
};
int getMap (char c) {
switch (c)
{
case '+':
case '-':
return 0;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return 1;
break;
case '.':
return 2;
break;
case 'e':
case 'E':
return 3;
break;
default:
return 4;
break;
}
}
int check ()
{
int len = strlen(str);
int state = 0;
int i,j;
char *p = str;
for (i=len-1;i>=0;i--)
if (str[i]!=' ')
{
str[i+1] = '\0';
break;
}
while (*p==' ') p++;
cout<<p;
for (;*p;p++)
{
j = getMap(*p);
state = trans[state][j];
if (state==9) return 0;
}
return (state==2 || state==4 || state==7);
}
int main ()
{
while (1)
{
cin.getline(str,999);
if (str[0]=='*') break;
if (check())
{
cout<<" is legal."<<endl;
} else
{
cout<<" is illegal."<<endl;
}
}
return 0;
}
Time makes a fool of memory
And yet my memories still shine
And yet my memories still shine
-
- New poster
- Posts: 11
- Joined: Wed Jan 09, 2002 2:00 am
- Location: Portugal
My accepted solution gives this output on these tests:
Input:
1E1
1e1
1E
1EE1
1 E1
.1E1
1E.1
1E1.1
++1E1
1E--1
1.1.1E1
abcd
1
1.1
0e-0
*
Output:
1E1 is legal.
1e1 is legal.
1E is illegal.
1EE1 is illegal.
1 E1 is illegal.
.1E1 is illegal.
1E.1 is illegal.
1E1.1 is illegal.
++1E1 is illegal.
1E--1 is illegal.
1.1.1E1 is illegal.
abcd is illegal.
1 is illegal.
1.1 is legal.
0e-0 is legal.
Hope it helps
Jorge Pinto
Input:
1E1
1e1
1E
1EE1
1 E1
.1E1
1E.1
1E1.1
++1E1
1E--1
1.1.1E1
abcd
1
1.1
0e-0
*
Output:
1E1 is legal.
1e1 is legal.
1E is illegal.
1EE1 is illegal.
1 E1 is illegal.
.1E1 is illegal.
1E.1 is illegal.
1E1.1 is illegal.
++1E1 is illegal.
1E--1 is illegal.
1.1.1E1 is illegal.
abcd is illegal.
1 is illegal.
1.1 is legal.
0e-0 is legal.
Hope it helps
Jorge Pinto
325 legal constant
what is the tricky input in this problem???
Before, I get Accepted on this problem, I got frustated on WA. Since there are so many tricky cases on this problem, you must be very careful.
I think the followings will help you :
1. No any other valid character are allowed (e.g : a,b,c,d,...)
2. Using more than 1 dot before e/E is illegal (e.g: 1.1.1e1 is illegal)
3. 0e-0 is legal
4. An integer value is illegal (e.g: 9999 is illegal)
5. The exponent should be in integer
6. And so on.
I think the followings will help you :
1. No any other valid character are allowed (e.g : a,b,c,d,...)
2. Using more than 1 dot before e/E is illegal (e.g: 1.1.1e1 is illegal)
3. 0e-0 is legal
4. An integer value is illegal (e.g: 9999 is illegal)
5. The exponent should be in integer
6. And so on.
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Try this method:
only same states are allowed, also create biggest figure of good constant, it is: x[.(x)+][{E|e}(x)+]
where (x)+ means one or more digit
[x] means optional part of expression
{x|y} means one and exactly one of specified in brackets values
create a graph of all possible states of this expression -> it's quite small (below 20 states) and mark states which are correct as end states. implement finite state machine and run it )))
I did it in this way and got accepted first time - very important are boundary conditions - unexcepted characters in input and so on ...
Best regards
Dominik
only same states are allowed, also create biggest figure of good constant, it is: x[.(x)+][{E|e}(x)+]
where (x)+ means one or more digit
[x] means optional part of expression
{x|y} means one and exactly one of specified in brackets values
create a graph of all possible states of this expression -> it's quite small (below 20 states) and mark states which are correct as end states. implement finite state machine and run it )))
I did it in this way and got accepted first time - very important are boundary conditions - unexcepted characters in input and so on ...
Best regards
Dominik
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact: