Train Problem I
3 123 321 3 123 312
Yes. in in in out out out FINISH No. FINISHFor the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1. In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3. Now we can let train 3 leave. But after that we can‘t let train 1 leave before train 2, because train 2 is at the top of the railway at the moment. So we output "No.".HintHint
#include<cstdio> #include<stack> using namespace std; int main(){ int n; char f1[1010],f2[1010];//f1记录入栈顺序,f2记录出栈顺序 while(scanf("%d%s%s",&n,f1,f2)!=EOF){ int g[1010],i=0,j=0,k=0;//g[]记录进出情况 stack<char> t; while(i<n){ if(t.empty()||t.top()!=f2[i]&&j<n){ t.push(f1[j++]); g[k++]=1;//1为in } else if(t.top()==f2[i]){ t.pop(); g[k++]=0;//0为out i++; } else break; } if(t.empty()){//栈t为空,则可以生成当前出栈序列 printf("Yes.\n"); for(int i=0;i<k;i++){ if(g[i]) printf("in\n"); else printf("out\n"); } printf("FINISH\n"); } else printf("No.\nFINISH\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1022.Train Problem I【栈的应用】【8月19】
原文地址:http://blog.csdn.net/a995549572/article/details/47779781