Spaghetti
Time Limit:1s | Memory limit:32M |
Accepted Submit:1 | Total Submit:8 |
Early languages like Fortran IV use conditional and unconditional goto
statements instead of structured statements like if
and while. In Fortran IV, each statement occupies a line of
input. The first five positions in each line are reserved for an optional
label, which is an integer. The next position is reserved for a continuation
marker, which we shall not consider further. Therefore, statements occupy
positions 7 and beyond in each input line. The goto statement looks
like this
goto label
and the conditional goto statement looks like this
if(expression)goto label
The language contains many other statements, but only the conditional
goto begins with "if(" and ends with ")goto
label" where
label is an integer. All spaces are ignored within a Fortran IV
statement. For this problem, the "stop" statement, which halts execution,
appears only as the last line of the program.
Your job is to determine whether or not two Fortran programs are equivalent.
They are equivalent if, for all possible inputs,
they execute exactly the same sequence of
statements, ignoring unconditional gotos and labels.
By "the same sequence of statements" we mean statements that are textually
identical, after spaces and labels are
removed. You must assume that each conditional goto will be taken for
some inputs and not taken for others. Unconditional gotos are, of course,
always taken. The two programs in the sample input are equivalent.
Your input consists of two programs
separated by a blank line. No input line exceeds 80 characters and no
program contains more than 1000 lines. Each label used in a goto statement
appears to the left of exactly one statment; no label is repeated.
Output consists if a single line, stating either "The programs are
equivalent." or "The programs are not equivalent."
Each input test cases are separated with a single blank line.
Sample Input
read 6, i,k,j
99 if(i .lt. j)goto 33
goto 55
33 i=j
goto 99
55 k=j+1
stop
read6,i,k,j
if(i.lt.j)goto12345
77 k=j+1
goto5555
12345 i=j
if(i.lt.j)goto12345
goto77
88 goto88
5555 stop
Output for Sample Input
The programs are equivalent.
Original: Waterloo local 1999.01.31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define no_key 0
#define if_statement 1
#define goto_statement 2
#define if_goto_statement 3
#define stop_statment 4
char key_words[5][10];
#define Label_size 100000
int label_index[2][Label_size];
struct Code_info
{
int key_statment;
char expression[100];
int label;
};
struct Code_info code_info[2][1010];
int equal_map[1001][1001];
int top[2];
int v_pc[2];
char * if_goto_key(char *p, int if_goto)
{
int i;
for (i = 0; i < 10; ++i)
{
if (key_words[if_goto][i] == '\0')
return p;
while (*p == ' ')
++p;
if (*p != key_words[if_goto][i])
return NULL;
++p;
}
return NULL;
}
int ignore_goto(int index, int v_pc)
{
int ori;
if (code_info[index][v_pc].key_statment != goto_statement)
return v_pc;
ori = v_pc;
do
{
if (code_info[index][v_pc].key_statment != goto_statement)
break;
v_pc = label_index[index][code_info[index][v_pc].label];
}while(ori != v_pc);
if (ori == v_pc)
return -1;
return v_pc;
}
int program_compare()
{
int stack[2][100000];
v_pc[0] = v_pc[1] = 0;
top[0] = top[1] = 0;
while (1)
{
/* ignore goto line */
v_pc[0] = ignore_goto(0, v_pc[0]);
v_pc[1] = ignore_goto(1, v_pc[1]);
if (equal_map[v_pc[0]][v_pc[1]] == 1)
{
if (top[0] == 0 || top[1] == 0)
{
if (top[0] == top[1])
return 1;
else
return 0;
}
v_pc[0] = stack[0][--top[0]];
v_pc[1] = stack[1][--top[1]];
continue;
}
/* dead circle */
if (v_pc[0] == -1 || v_pc[1] == -1)
{
if (v_pc[0] == v_pc[1])
return 1;
else
return 0;
}
if (code_info[0][v_pc[0]].key_statment != code_info[1][v_pc[1]].key_statment)
return 0;
/* stop statement */
if (code_info[0][v_pc[0]].key_statment == stop_statment)
{
if (top[0] != top[1])
return 0;
if (top[0] == 0)
return 1;
v_pc[0] = stack[0][--top[0]];
v_pc[1] = stack[1][--top[1]];
continue;
}
/* expression */
if (strcmp(code_info[0][v_pc[0]].expression, code_info[1][v_pc[1]].expression) != 0)
return 0;
if (code_info[0][v_pc[0]].key_statment == if_goto_statement)
{
equal_map[v_pc[0]][v_pc[1]] = 1;
stack[0][top[0]++] = v_pc[0] + 1;
stack[1][top[1]++] = v_pc[1] + 1;
v_pc[0] = label_index[0][code_info[0][v_pc[0]].label];
v_pc[1] = label_index[1][code_info[1][v_pc[1]].label];
}
else
{
v_pc[0]++;
v_pc[1]++;
}
}
return 1;
}
int main()
{
int i, j, label, line;
char code[100];
char *p = NULL, *q = NULL;
strcpy(key_words[if_statement], "if(");
strcpy(key_words[goto_statement], "goto");
strcpy(key_words[stop_statment], "stop");
while(1)
{
memset(equal_map, 0, sizeof(equal_map));
for (i = 0; i < 2; ++i)
{
line = 0;
while(p = gets(code))
{
code_info[i][line].key_statment = 0;
p = code;
/* get lable */
label = 0;
for (j = 0; j < 5; ++j, ++p)
{
if (*p != ' ')
label = label * 10 + *p - '0';
}
if (label != 0)
label_index[i][label] = line;
p += 1;
if (if_goto_key(p, stop_statment))
{
code_info[i][line].key_statment |= stop_statment;
break;
}
if (q = if_goto_key(p, if_statement))
{
code_info[i][line].key_statment |= if_statement;
p = q;
q = code_info[i][line].expression;
while (*p != ')')
{
if (*p == ' ')
{
++p;
continue;
}
*q++ = *p++;
}
*q = '\0';
++p;
}
if (q = if_goto_key(p, goto_statement))
{
code_info[i][line].key_statment |= goto_statement;
p = q;
label = 0;
while (*p != '\0')
{
if (*p != ' ')
label = label * 10 + *p - '0';
++p;
}
code_info[i][line].label = label;
}
if (*p != '\0')
{
q = code_info[i][line].expression;
while (*p != '\0')
{
if (*p == ' ')
{
++p;
continue;
}
*q++ = *p++;
}
*q = '\0';
}
++line;
}
if (p == NULL)
return 0;
gets(code);
}
if (program_compare())
printf("The programs are equivalent.\n");
else
printf("The programs are not equivalent.\n");
}
return 0;
}
|
阅读(1301) | 评论(0) | 转发(0) |