hdu 1022 Train Problem I 模拟

本题就是栈的模拟题,我是自己用数组写的栈,没用STL。

没什么说的,上代码。

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 char STACK[100];
 5 int point;
 6 char a[100];
 7 char b[100];
 8 string abc[200];
 9 int main()
10 {
11    int a_p,b_p,i,j,t,n,count;
12    while(cin>>n)
13    {
14       cin>>a>>b;
15       memset(STACK,0,sizeof(STACK));
16       a_p=b_p=count=0;
17       point=0;          //point=0表示空栈
18       while(!(a_p==n&&b_p==n)&&(a_p<=n)&&b_p<=n)
19       {
20          if(STACK[point]==b[b_p]&&point)  //如果栈顶等于b的最前位置的话,执行出栈操作
21          {
22             STACK[point]=0;
23             point--;
24             b_p++;
25             abc[count++]="out";
26          }
27          else if(STACK[point]!=b[b_p])   //如果栈顶不等于b的最前位置的话,执行入栈操作
28          {
29             point++;
30             STACK[point]=a[a_p];
31             a_p++;
32             abc[count++]="in";
33          }
34       }
35       if(point==0)
36       {
37          cout<<"Yes."<<endl;
38          for(i=0;i<count;++i)
39           cout<<abc[i]<<endl;
40          cout<<"FINISH"<<endl;
41       }
42       else
43       {
44          cout<<"No."<<endl;
45          cout<<"FINISH"<<endl;
46       }
47    }
48    return 0;
49 }
原文地址:https://www.cnblogs.com/symons1992/p/2734002.html