3 123 321 3 123 312
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.".
本题的思路呢,因为给定了初始和变化后的序列,
所以用栈来模拟,初始栈空,若栈顶元素与序列2当前元素不同,则序列1当前元素压栈,直至相同,出栈,修改当前元素,重复直到结束。中间需记录进出情况。但是有个毛病在这里,由于栈没有clear函数,但是每次进栈出栈之前我们要保证栈为空,我开始的时候就没有考虑导致总是wa,应该把栈的声明放在while里面。
ac代码:
#include<iostream>
#include<string>
#include<stack>
using namespace std;
int flag[25] = { 0 };
int main()
{
int n;
string a, b;
while (cin >> n >> a >> b)
{
stack<char>s;
int k = 0;
int temp = 0;
for (int i = 0; i < n; i++)
//cout << a[i];
{
s.push(a[i]);
flag[k++] = 1;
while (!s.empty() && s.top() == b[temp])
{
flag[k++] = 0;
s.pop();
temp++;
}
}
if (temp == n)
{
cout << "Yes." << endl;
for (int i = 0; i < k; i++)
{
if (flag[i] == 1)
cout << "in" << endl;
else
cout << "out" << endl;
}
}
else
cout << "No." << endl;
cout << "FINISH" << endl;
}
return 0;
}