OutputThe output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
Sample Input
3 123 321 3 123 312Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
For 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.".
經典火車入站模擬,貼代碼
#include<iostream>
#include<stack>
using namespace std;
int n,i,j,cnt,ans[1005];
char str1[1005],str2[1005];
stack<char>s;
int main()
{
while(cin>>n)
{
cin>>str1>>str2;
while(!s.empty()) s.pop();
cnt=0;j=0;
for(i=0;i<n;i++)
{
s.push(str1[i]);
ans[cnt++]=1;
while(j<n&&!s.empty())
{
if (s.top()==str2[j])
{
ans[cnt++]=0;
j++;
s.pop();
}
else break;
}
}
if (s.empty())
{
cout<<"Yes."<<endl;
for(i=0;i<cnt;i++)
if (ans[i]==0) cout<<"out"<<endl;
else cout<<"in"<<endl;
cout<<"FINISH"<<endl;
}
else
{
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
}
return 0;
}