Rails problem
总是wa~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> int main() { int n, i, j, k, atop, cmd[20]; char a[10], b[10]; while(scanf("%d %s %s", &n, a, b) != EOF){ for(i = 0, j = 0, k = 0; i < n; i++){ atop = i + 1; cmd[k++] = 1; while(atop > 0 && a[atop - 1] == b[j]){ cmd[k++] = 0; j++; atop--; } } if(j == n) { printf("Yes.\n"); for(i = 0; i < k; i++){ if(cmd[i] == 1) printf("in\n"); else printf("out\n"); } printf("FINISH\n"); } else { printf("No.\nFINISH\n"); } } return 0; }
经过修改,发现问题出现在栈的使用,调试了这组数据:5 67543 74536,才发现自己没有清栈。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 int main() 3 { 4 int n, i, j, k, top, cmd[20]; 5 char a[10], b[10], c[10]; 6 while(scanf("%d %s %s", &n, a, b) != EOF){ 7 8 top = 1; 9 for(i = 0, j = 0, k = 0; i < n; i++){ 10 11 c[top++] = a[i];/*入栈*/ 12 cmd[k++] = 1; 13 while(c[top-1] == b[j]){ 14 15 c[top-1] = 0;/*出栈*/ 16 top--; 17 cmd[k++] = 0; 18 j++; 19 20 } 21 22 } 23 if(j == n) 24 { 25 26 printf("Yes.\n"); 27 for(i = 0; i < k; i++){ 28 29 if(cmd[i] == 1) 30 printf("in\n"); 31 else 32 printf("out\n"); 33 34 } 35 printf("FINISH\n"); 36 37 } 38 else 39 { 40 41 printf("No.\nFINISH\n"); 42 43 } 44 45 } 46 return 0; 47 }
解决此问题后,写下自己的用栈心得:
入栈,先赋值再上移栈顶;
出栈,清零再下移栈顶。
注意:一定要保持栈的数目不变,这样才是栈的使用规则