Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25773 Accepted Submission(s): 9729
Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can‘t leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
Output
The output contains a string "No." if you can‘t exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
Sample Input
3 123 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. FINISH
题解:火车进站,类似于栈;;;
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 using namespace std; 5 char in[20],out[10],m[30][5]; 6 int t; 7 void display(char *s){ 8 strcpy(m[t++],s); 9 } 10 int main(){ 11 int n; 12 while(~scanf("%d",&n)){memset(m,0,sizeof(m)); 13 memset(in,0,sizeof(in)); 14 memset(out,0,sizeof(out)); 15 t=0;scanf("%s%s",in,out); 16 stack<char>train; 17 for(int j=0,i=0;i<n;){ 18 if(!train.empty()&&train.top()==out[i])display("out"),i++,train.pop(); 19 else if(in[j])train.push(in[j++]),display("in"); 20 else break; 21 }//printf("%d\n",train.size()); 22 //while(!train.empty())printf("%c",train.top()),train.pop(); 23 display("FINISH"); 24 if(train.empty()){puts("Yes."); 25 for(int i=0;i<t;++i)printf("%s\n",m[i]);} 26 else puts("No."),puts("FINISH"); 27 } 28 return 0; 29 }
另外,自己写了几个关于栈的括号配对问题,贴下:
代码:
1 #include<stdio.h> 2 char m[10010]; 3 int top; 4 bool pop(){ 5 top--; 6 if(top<0)return false; 7 else return true; 8 } 9 void push(char s){ 10 top++; 11 m[top]=s; 12 } 13 int main(){ 14 char x[10010]; 15 int T; 16 scanf("%d",&T); 17 while(T--){top=0; 18 scanf("%s",x); 19 for(int i=0;x[i];i++){ 20 if(x[i]==‘(‘||x[i]==‘[‘)push(x[i]); 21 else if(x[i]==‘)‘&&m[top]==‘(‘||x[i]==‘]‘&&m[top]==‘[‘){if(!pop())break;} 22 else push(x[i]); 23 }//printf("%d",top); 24 //while(top)printf("%c",m[top--]); 25 if(top==0)puts("Yes"); 26 else puts("No"); 27 } 28 return 0; 29 }
1 #include<stdio.h> 2 #include<stack> 3 using namespace std; 4 char s[10010]; 5 int main(){ 6 int T,temp; 7 scanf("%d",&T); 8 while(T--){temp=1; 9 stack<char>m; 10 scanf("%s",s); 11 for(int i=0;s[i];i++){if(m.empty()&&(s[i]==‘)‘||s[i]==‘]‘)){ 12 temp=0; 13 puts("No"); 14 break; 15 } 16 if(s[i]==‘(‘||s[i]==‘[‘)m.push(s[i]); 17 else if(s[i]==‘)‘&&m.top()==‘(‘||s[i]==‘]‘&&m.top()==‘[‘)m.pop(); 18 else m.push(s[i]); 19 } 20 if(m.empty()&&temp)puts("Yes"); 21 else if(temp)puts("No"); 22 } 23 return 0;}