hdu - 1022 题解

题意:火车站有一个铁轨,出口和入口是同一个。给定火车入站序列,问能否调成成出站序列,如果能,输出出站操作。

题解:模拟栈即可,如果栈空,则从入站序列取出首元素压栈,如果栈顶元素与出站序列首元素不同,则从入站序列取出首元素压栈,如果栈顶元素与出站序列首元素相同,则出栈,更新出站序列首元素。如果入站序列已空,而还在尝试取出首元素压栈,则判定为不能调整。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<string>
 5 #include<vector>
 6 #include<stack>
 7 using namespace std;
 8 stack<int> st;
 9 int main()
10 {
11     int n;
12     while(scanf("%d",&n)!=EOF)
13     {
14         while(!st.empty())
15         {
16             st.pop();
17         }
18         vector<string> res;
19         res.clear();
20         string s1,s2;
21         cin>>s1>>s2;
22         int tip1=0,tip2=0;
23         while(tip2<s2.length())
24         {
25             if(st.empty()||st.top()!=s2[tip2]-'0')
26             {
27                 if(tip1==s1.length())
28                 {
29                     break;
30                 }
31                 st.push(s1[tip1]-'0');
32                 res.push_back("in");
33                 tip1+=1;
34             }
35             else
36             {
37                 res.push_back("out");
38                 st.pop();
39                 tip2+=1;
40             }
41         }
42         if(tip2!=s2.length())
43         {
44             printf("No.\n");
45         }
46         else
47         {
48             printf("Yes.\n");
49             vector<string>::iterator it;
50             for(it=res.begin();it!=res.end();it++)
51             {
52                 cout<<*it<<endl;
53             }
54         }
55         printf("FINISH\n");
56     }
57     return 0;
58 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值