HDU 1022 Train Problem I stack 基础题

题意:

有一个火车站,只有一个口同时用于进入和出去,但不能有2辆或以上同时进出。

给出一个n代表有n辆火车(n<=9),编号分别为1到n。

然后给出2个序列,分别代表火车进站的顺序,火车出站的顺序。

问按照这个进站的顺序,能不能按照这个出站的顺序出站。

若能,输出这个口进出的顺序。

Sample Input

3 123 321

3 123 312

Sample Output

Yes.

in

in

in

out

out

out

FINISH

No.

FINISH

直接用stack进行模拟就OK了。

只要确定好什么情况下为Yes,什么情况下为No。

 1 #include<cstdio>
 2 #include<stack>
 3 #include<cstring>
 4 using namespace std;
 5 char in[12];
 6 char out[12];
 7 int ans[24];
 8 int main()
 9 {
10     int n;
11     while(scanf("%d",&n)!=EOF)
12     {
13         scanf("%s",in+1);
14         scanf("%s",out+1);
15         memset(ans,-1,sizeof(ans));
16         stack<char>s;
17         while(!s.empty())
18             s.pop();
19         int i=1,j=1;
20         s.push(in[i]);
21         int tot=1;
22         ans[tot++]=0;
23         bool flag=false;
24         while(i<n+1)
25         {
26             while(s.size()&&s.top()==out[j])
27             {
28                 s.pop();
29                 ans[tot++]=1;
30                 j++;
31                 if(j==n+1)
32                     break;
33             }
34             if(j==n+1)
35             {
36                 flag=true;
37                 break;
38             }
39             i++;
40             if(i<=n)
41             {
42                 s.push(in[i]);
43                 ans[tot++]=0;
44             }
45         }
46         if(flag)
47         {
48             printf("Yes.\n");
49             for(int i=1;i<tot;i++)
50                 if(ans[i]==0)
51                     printf("in\n");
52                 else
53                     printf("out\n");
54             printf("FINISH\n");
55         }
56         else
57         {
58             printf("No.\nFINISH\n");
59         }
60     }
61     return 0;
62 }

提交代码

时间: 2024-10-10 05:41:44

HDU 1022 Train Problem I stack 基础题的相关文章

hdu 1022 Train Problem I(stack)

用数组模拟栈 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char str1[30],str2[30]; int sta[30]; int main() { int n; int i,j,k; int ans[100]; while(scanf("%d",&n)!=EOF) { scan

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 1022 Train Problem I (数据结构 —— 栈)

Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes

HDU 1022 Train Problem I(栈的操作规则)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 45375    Accepted Submission(s): 16966 Problem Description As the new term come

HDU 1022 Train Problem I (STL 栈模拟)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30420    Accepted Submission(s): 11492 Problem Description As the new term comes, the Ignatius Train Station is very busy nowaday

hdu 1022 Train Problem I(栈的应用+STL)

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20521    Accepted Submission(s): 7712 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDU 1022.Train Problem I【栈的应用】【8月19】

Train Problem I Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^).

hdu 1022 Train Problem

Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30588    Accepted Submission(s): 11561 Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays

HDU 1022 Train Problem I

描述As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there i