题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022
题意:
给你2个字符串,第一个字符串str1代表火车的入站顺序,第二个字符串str2代表出站序列,如果str1能满足str2的要求,就输出YES。并输入进站出站的顺序。
思路:C++栈的应用
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int num, total;
string str1, str2;
vector <string> v;
stack <int> s;
while(scanf("%d", &num) != EOF)
{
total = 0; //初始化
v.clear()
while(!s.empty()) //注意清空栈
s.pop();
cin>>str1>>str2;
for(int i = 0; i < num; ++i)
{
s.push(str1[i]);
v.push_back("in");
while(!s.empty() && s.top() == str2[total]) //total即可代表顶端
{
total++;
s.pop();
v.push_back("out");
}
}
if(s.empty())
{
printf("Yes.\n");
for(int i = 0; i < v.size(); ++i) //进站出站顺序
cout<<v[i]<<endl;
printf("FINISH\n");
}
else
printf("No.\nFINISH\n");
}
return 0;
}