本题的题目链接为:Train Problem I
其实这是一题模拟stack栈的进出问题,具体代码见如下
#include<iostream>
#include<stack>
const int max = 100;
using namespace std;
int main()
{
stack<char> s;
int n, i, j, k, result[max];//n代表火车的个数,result[max]代表结果状态,1代表入栈,0代表出栈
char str1[max], str2[max];//序列1和序列2
while (cin >> n >> str1 >> str2)
{
j = 0, i = 0, k = 1;
s.push(str1[0]);//为防止栈空,压入一个
result[0] = 1;//记录一个进来了
while (i < n && j < n)
{
if (s.size() && s.top() == str2[j])
{
j++;
s.pop(); //如果和序列2相等就弹栈
result[k++] = 0;
}
else
{
if (i == n) break;
s.push(str1[++i]);
result[k++] = 1;
}
}
if (i == n)
cout << "No." << endl;
else
{
cout << "Yes." << endl;
for (i = 0; i < k; i++)
if (result[i])
cout << "in" << endl;
else
cout << "out" << endl;
}
cout << "FINISH" << endl;
}
}