传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022
给你两串数 输出让第一串变成第二串的进出栈过程,如果能,输出yes,并输出进出栈的过程,如果不能,输出no。在每种情况最后输出FINISH
栈的基本运用吧!用一个vector数组记录进出栈情况。
#include <iostream>
#include <stack>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int n;
char str1[100],str2[100];
int main()
{
vector<string> p;
while(cin>>n)
{
p.clear();
stack<int> s;
int A=0,B=0;
scanf("%s%s",str1,str2);
int ok=1;
while(B<n)
{
if(str1[A]==str2[B])
{
A++;B++;
p.push_back("in");
p.push_back("out");
}
else if(!s.empty() && s.top()==str2[B])
{
s.pop();
p.push_back("out");
B++;
}
else if(A<n)
{
s.push(str1[A++]);
p.push_back("in");
}
else
{
ok=0;
break;
}
}
printf("%s\n",ok?"Yes.":"No.");
if(ok)
for(int i=0;i<p.size();i++)
cout<<p[i]<<endl;
cout<<"FINISH\n";
}
return 0;
}