【HDOJ】1073 Online Judge
这道题TLE了N多次,完全不明白为什么,稍微改了一下,居然过了。使用gets过的,看讨论帖有人还推荐用hash。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define LOCAL 0 5 #define MAXNUM 5005 6 #define isSpace(ch) (ch==' '||ch=='\t'||ch=='\n') 7 8 char stand[MAXNUM]; 9 char data[MAXNUM]; 10 char word[MAXNUM]; 11 12 int main() { 13 int n, len1, len2, i, j, flg; 14 #if LOCAL 15 FILE *fout = fopen("data", "w"); 16 #endif 17 scanf("%d%*c", &n); 18 19 while (n--) { 20 scanf("%*s%*c"); // START 21 len1 = 0; 22 while (1) { 23 if (gets(word) == NULL) { 24 stand[len1++] = '\n'; 25 continue; 26 } 27 if ( !strcmp(word, "END") ) 28 break; 29 strcpy(stand+len1, word); 30 len1 += strlen(word); 31 stand[len1++] = '\n'; 32 } 33 stand[len1++] = '\0'; 34 scanf("%*s%*c"); // START 35 len2 = 0; 36 while (1) { 37 if (gets(word) == NULL) { 38 data[len2++] = '\n'; 39 continue; 40 } 41 if ( !strcmp(word, "END") ) 42 break; 43 strcpy(data+len2, word); 44 len2 += strlen(word); 45 data[len2++] = '\n'; 46 } 47 data[len2++] = '\0'; 48 #if LOCAL 49 fprintf(fout, "standard:\n%s", stand); 50 fprintf(fout, "data:\n%s", data); 51 #endif 52 i = j = flg = 0; 53 while (i<len1 && j<len2) { 54 if (stand[i] == data[j]) { 55 j++; 56 i++; 57 } else { 58 if ( isSpace(stand[i]) ) { 59 flg = 1; 60 i++; 61 } else if ( isSpace(data[j]) ) { 62 flg = 1; 63 j++; 64 } else { 65 flg = 2; 66 break; 67 } 68 } 69 } 70 if (flg == 2) { 71 printf("Wrong Answer\n"); 72 continue; 73 } 74 while (i<len1) { 75 if ( isSpace(stand[i]) ) 76 flg = 1; 77 else { 78 flg = 2; 79 break; 80 } 81 ++i; 82 } 83 while (j<len2 && flg!=2) { 84 if ( isSpace(data[j]) ) 85 flg = 1; 86 else { 87 flg = 2; 88 break; 89 } 90 ++j; 91 } 92 if (flg==2) 93 printf("Wrong Answer\n"); 94 else if (flg==1) 95 printf("Presentation Error\n"); 96 else 97 printf("Accepted\n"); 98 } 99 #if LOCAL 100 fclose(fout); 101 #endif 102 return 0; 103 }
浙公网安备 33010602011771号