标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24377 Accepted Submission(s): 9196
1 #include<stdio.h> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 char o1[10],o2[10],stack[10]; 7 int n,A=1,B=1,top=0,inout[10],flag=0,ok; //用inout数组来保存压栈和出栈,1为压,0为出 8 while(scanf("%d %s %s",&n,o1,o2)!=EOF) 9 { 10 memset(inout,0,sizeof(inout)); 11 ok=1; 12 A=0; 13 B=0; 14 flag=0; 15 top=0; 16 while(B<=n) 17 { 18 if(o1[A]==o2[B]) //当前进栈的车与目标顺序中当前列车一致,即进站后立即出站 19 { 20 A++; 21 B++; 22 inout[flag++]=2; 23 } 24 else if(top&&stack[top]==o2[B]) //上一种情况不符合,栈顶的列车与目标顺序当前列车一致,即只需出站 25 { 26 top--; 27 B++; 28 inout[flag++]=0; 29 } 30 else if(A<=n) // 上两种情况均不符合,只需要压栈 31 { 32 stack[++top]=o1[A++]; 33 inout[flag++]=1; 34 } 35 else //上述三种情况均不符合,此时A>n 36 { 37 ok=0; 38 break; 39 } 40 41 } 42 if(ok) 43 { 44 printf("Yes.\n"); 45 for(int j=0;j<flag-1;j++) 46 if(inout [j]==2) 47 printf("in\nout\n"); 48 else if(inout[j]==1) 49 printf("in\n"); 50 else if(inout[j]==0) 51 printf("out\n"); 52 printf("FINISH\n"); 53 } 54 else 55 printf("No.\nFINISH\n"); 56 } 57 }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/4461051.html