这道题TLE了N多次,完全不明白为什么,稍微改了一下,居然过了。使用gets过的,看讨论帖有人还推荐用hash。
#include <stdio.h>
#include <string.h> #define LOCAL 0
#define MAXNUM 5005
#define isSpace(ch) (ch==' '||ch=='\t'||ch=='\n') char stand[MAXNUM];
char data[MAXNUM];
char word[MAXNUM]; int main() {
int n, len1, len2, i, j, flg;
#if LOCAL
FILE *fout = fopen("data", "w");
#endif
scanf("%d%*c", &n); while (n--) {
scanf("%*s%*c"); // START
len1 = ;
while () {
if (gets(word) == NULL) {
stand[len1++] = '\n';
continue;
}
if ( !strcmp(word, "END") )
break;
strcpy(stand+len1, word);
len1 += strlen(word);
stand[len1++] = '\n';
}
stand[len1++] = '\0';
scanf("%*s%*c"); // START
len2 = ;
while () {
if (gets(word) == NULL) {
data[len2++] = '\n';
continue;
}
if ( !strcmp(word, "END") )
break;
strcpy(data+len2, word);
len2 += strlen(word);
data[len2++] = '\n';
}
data[len2++] = '\0';
#if LOCAL
fprintf(fout, "standard:\n%s", stand);
fprintf(fout, "data:\n%s", data);
#endif
i = j = flg = ;
while (i<len1 && j<len2) {
if (stand[i] == data[j]) {
j++;
i++;
} else {
if ( isSpace(stand[i]) ) {
flg = ;
i++;
} else if ( isSpace(data[j]) ) {
flg = ;
j++;
} else {
flg = ;
break;
}
}
}
if (flg == ) {
printf("Wrong Answer\n");
continue;
}
while (i<len1) {
if ( isSpace(stand[i]) )
flg = ;
else {
flg = ;
break;
}
++i;
}
while (j<len2 && flg!=) {
if ( isSpace(data[j]) )
flg = ;
else {
flg = ;
break;
}
++j;
}
if (flg==)
printf("Wrong Answer\n");
else if (flg==)
printf("Presentation Error\n");
else
printf("Accepted\n");
}
#if LOCAL
fclose(fout);
#endif
return ;
}