【HDOJ】1022 Train Problem I
栈和队列训练题目。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 1005 5 6 char in[MAXNUM]; 7 char out[MAXNUM]; 8 char stack[MAXNUM]; 9 char order[MAXNUM]; // 1:in, 0:out 10 11 int main() { 12 int n; 13 int i, j, k, tmp, flg; 14 int top; 15 16 while (scanf("%d %s %s", &n, in, out) != EOF) { 17 top = k = 0; 18 for (i=0; in[i]!=out[0]&&i<n; ++i) { 19 stack[top++] = in[i]; 20 order[k++] = 1; 21 } 22 tmp = i+1; 23 order[k++]=1; order[k++]=0; 24 flg = 0; 25 for (i=1; i<n; ++i) { 26 if (top) { 27 if (stack[top-1] == out[i]) { 28 --top; 29 order[k++] = 0; 30 continue; 31 } 32 for (j=0; j<top-1; ++j) { 33 if (stack[j] == out[i]) { 34 printf("No.\nFINISH\n"); 35 flg = 1; 36 break; 37 } 38 } 39 } 40 if (flg) 41 break; 42 for (j=tmp; in[j]!=out[i]&&j<n; ++j) { 43 stack[top++] = in[j]; 44 order[k++] = 1; 45 } 46 tmp = j+1; 47 order[k++]=1; order[k++]=0; 48 } 49 if (flg) 50 continue; 51 printf("Yes.\n"); 52 for (i=0; i<k; ++i) 53 if (order[i]) 54 printf("in\n"); 55 else 56 printf("out\n"); 57 printf("FINISH\n"); 58 } 59 60 return 0; 61 }