hdu1022 Train Problem I

http://acm.hdu.edu.cn/showproblem.php?pid=1022

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 #include<stack>
 7 using namespace std;
 8 const int N=100;
 9 
10 int main()
11 {
12     //freopen("in.txt","r",stdin);
13     stack<char>s;
14     int n,result[N];
15     char s1[N],s2[N];
16     int i,j,k;
17     while(cin>>n>>s1>>s2)
18     {
19         i=0;j=0;k=1;
20         s.push(s1[i]);
21         result[0]=1;
22         while(i<n&&j<n)
23         {
24             if(s.size() && s.top()==s2[j])//如果栈取元素和s2当前元素相同,弹栈
25             {
26                 j++;
27                 s.pop();
28                 result[k++]=0;
29             }
30             else//如果不相同,继续讲s1后一个元素压栈
31             {
32                 if(i==n)
33                 break;
34                 s.push(s1[++i]);
35                 result[k++]=1;
36             }
37         }
38         if(i==n)//如果i==n代表找不到s2中的当前元素
39         cout<<"No."<<endl;
40         else
41         {
42             cout<<"Yes."<<endl;
43             for(i=0;i<k;i++)
44             {
45                 if(result[i])
46                 cout<<"in"<<endl;
47                 else
48                 cout<<"out"<<endl;
49             }
50         }
51         cout<<"FINISH"<<endl;
52     }
53     return 0;
54 }

 

posted @ 2015-05-09 21:13  煎饼馃子  阅读(108)  评论(0编辑  收藏  举报