本题是基本的入栈出栈问题,建立栈之后,基本操作即可。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
string in, out; //输入输出队列
string order[100]; //输出消息
void trainStation(int n);
int main()
{
int n;
while (cin >> n)
{
trainStation(n);
}
return 0;
}
void trainStation(int n)
{
stack<char> s; //建立char型栈
cin >> in >> out;
int j = 0, k = 0;
for (int i = 0; i < n; i++) //i控制入栈队列
{
s.push(in[i]); //入栈
order[k++] = "in";
while (!s.empty() && s.top() == out[j]) //j控制出栈队列
{
s.pop(); //出栈
order[k++] = "out"; //k控制输出消息数组
j++;
}
}
if (s.empty()) //栈为空则能够按规定顺序出栈
{
cout << "Yes." << endl;
for (int i = 0; i < k; i++)
cout << order[i] << endl;
cout << "FINISH" << endl;
}
else
cout << "No." << endl << "FINISH" << endl;
}
继续加油。