hdoj-1022(栈的模拟)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <cstdio>
 6 using namespace std;3
 7 const int N=1000+7;
 8 stack <char> ss;
 9 char t1[N];
10 char t2[N];
11 int a,b;// a 表示将要入栈的第一个数  b 表示下一个出栈的数
12 int ans[2*N]; int num;
13 int n;
14 char str [][5]={"in","out"};
15 int main ()
16 {
17     while (~scanf ("%d %s %s",&n,t1+1,t2+1)) {
18         while (!ss.empty()) ss.pop();
19         a=b=1; num=0;
20         bool flag=1;
21         while (b<=n) {
22             if (t1[a]==t2[b])  { ans[++num]=0; ans[++num]=1; a++; b++; }  // 如果入栈的数刚好是出栈的情况
23             else if (!ss.empty()&&ss.top()==t2[b])  { ans[++num]=1; ss.pop(); b++; } //或者直接栈中的第一个数出栈
24             else if (a<=n)   { ans[++num]=0; ss.push(t1[a]); a++; }//无法出栈就入栈。。。模拟出栈入栈的顺序
25             else {
26                     flag=0;   break;//如果A全部入栈 而无法全部出栈
27             }
28         }
29         if (!flag)  cout<<"No." <<endl;
30         else {
31             cout<<"Yes."<<endl;
32             for (int i=1;i<=num;i++)
33                 cout<<str[ans[i]]<<endl;
34         }
35         cout<<"FINISH"<<endl;
36     }
37     return 0;
38 }
抓住青春的尾巴。。。
原文地址:https://www.cnblogs.com/xidian-mao/p/8522310.html