Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23574 Accepted Submission(s): 8907
#include<cstdio> #include<cstring> #include<queue> #include<stack> #include<algorithm> using namespace std; char in[12]; char out[12]; int n; void solve() { queue<char>Q;//火车出站序列 queue<int>ans;//进出顺序,1表示in,0表示out stack<char>S;//火车入栈序列 for(int i=0; i<n; ++i) Q.push(out[i]); for(int i=0; i<n; i++){ if(!S.empty()&&S.top()==Q.front()){//wa了一次在这里,没有考虑到例子5 14325 13245 Q.pop();S.pop(); ans.push(0); i--;//由于是用前一个和Q.front()比较,所以是s[i]先不入栈 } else if(in[i]==Q.front()){ Q.pop(); ans.push(1); ans.push(0); } else{ ans.push(1); S.push(in[i]); } } while(!S.empty()&&!Q.empty()){ if(S.top()==Q.front()){ S.pop();Q.pop(); ans.push(0); } else break; } while(!S.empty()){ if(S.top()==Q.front()){ ans.push(0); S.pop();Q.pop(); } else break; } if(S.empty()){ printf("Yes.\n"); while(!ans.empty()){ if(ans.front()) printf("in\n"); else printf("out\n"); ans.pop(); } printf("FINISH\n"); } else printf("No.\nFINISH\n"); } int main() { while(~scanf("%d%s%s",&n,in,out)) solve(); return 0; }
FINISH
原文:http://www.cnblogs.com/orchidzjl/p/4363525.html