栈的简单实现:
#include <iostream>
using namespace std;
char a[10], b[10], stack[10];
char out[20][4];
int main()
{
int n;
while(cin >> n)
{
memset(out, 0, sizeof(out));
cin >> a >> b;
int i = 0, j= 0, top = 0;
int t = 0;
while((stack[top] == b[j] || i != n) && j != n)
{
if(b[j] == stack[top])
{
j++;
top--;
strcpy(out[t++], "out");
}
else
{
stack[++top] = a[i];
i++;
strcpy(out[t++], "in");
}
}
if(t == 2 * n)
{
cout << "Yes." << endl;
for(int i = 0; i < t; i++)
cout << out[i] << endl;
cout << "FINISH" << endl;
}
else
{
cout << "No." << endl;
cout << "FINISH" <<endl;
}
}
return 0;
}
STL:
#include <iostream>
#include <queue>
#include <stack>
#include <string>
using namespace std;
const int N = 11;
int n;
int i, j;
char order1[N], order2[N];
void pro()
{
stack<char> stk;
queue<string> q;
for(i = 0, j = 0; i < n && j <= n;)
{
if(stk.empty() || stk.top() != order2[i])
{
if(j == n)
{
cout << "No.\nFINISH" << endl;
return;
}
stk.push(order1[j++]);
q.push("in");
}
else
{
stk.pop();
q.push("out");
i++;
}
}
cout << "Yes." << endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
cout << "FINISH" << endl;
}
int main()
{
while(cin >> n)
{
cin >> order1 >> order2;
pro();
}
return 0;
}