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
栈的水题一道,就直接对进出栈进行模拟,和之前做的那道题类似,用来练练STL还是不错的,直接用STL水过。
http://blog.csdn.net/whjkm/article/details/38455291 可以看看之前的那道题;
用来vector 和 stack容器,用vector来存储字符串;
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <stack> #include <vector> using namespace std; int main() { int n,i,j; string str1,str2; while(cin>>n>>str1>>str2) { stack<int>s; vector<string>v; for(i=0,j=0;i<n&&j<n;i++) { s.push(str1[i]);//把元素进栈 v.push_back("in");//压进vector容器 while(s.top()==str2[j])//进行匹配 { if(!s.empty()) { s.pop(); v.push_back("out"); } j++; if(s.empty()) break; } } if(j==n) { printf("Yes.\n"); for(i=0; i< v.size();i++) cout<< v[i] <<endl; printf("FINISH\n"); } else printf("No.\nFINISH\n"); } return 0; }
hdu 1022 Train Problem I(栈的应用+STL),布布扣,bubuko.com
hdu 1022 Train Problem I(栈的应用+STL)
原文地址:http://blog.csdn.net/whjkm/article/details/38456533