码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1022 Train Problem I---模拟栈

时间:2018-04-02 22:51:24      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:输出   color   target   else   printf   mat   pos   ons   str   

题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1022

题目大意:

车的进出站问题,先给出N个火车,再按序列一的方式进站,判断能否以序列二的方式出站,若能先输出“Yes.”,再输出出站步骤,以FINISH结束,若不能,输出“No.”,仍以FINISH结束。

思路:

直接模拟栈,注意细节!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 using namespace std;
 9 typedef long long ll;
10 const int maxn = 1e6 + 10;
11 const int INF = 1 << 30;
12 int T, n, m;
13 int v[20];
14 int main()
15 {
16     string s1, s2;
17     while(cin >> n >> s1 >> s2)
18     {
19         int j = 0;
20         int tot = 0, ok = 1;
21         stack<char>q;
22         for(int i = 0; i < s1.size(); i++)
23         {
24             q.push(s1[i]);
25             v[tot++] = 1;
26             while(!q.empty() && q.top() == s2[j])//一开始将j++写在这里,是错的,因为每次判断条件都会加一
27             {
28                 q.pop();
29                 v[tot++] = 0;
30                 j++;
31             }
32         }
33         if(j == n && tot == 2 * n)printf("Yes.\n");
34         else printf("No.\n");
35         if(j == n && tot == 2 * n)
36         {
37             for(int i = 0; i < tot; i++)if(v[i])printf("in\n");
38             else printf("out\n");
39         }
40         printf("FINISH\n");
41     }
42     return 0;
43 }

 

hdu1022 Train Problem I---模拟栈

标签:输出   color   target   else   printf   mat   pos   ons   str   

原文地址:https://www.cnblogs.com/fzl194/p/8698354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!