HDU Train Problem I 1022 栈模拟

题目大意:

给你一个n 代表有n列 火车,  第一个给你的一个字符串 代表即将进入到轨道上火车的编号顺序, 第二个字符串代表的是 火车出来之后到顺序,

分析一下就知道这,这个问题就是栈, 先进后出吗, 就是问你这个编号有没有可能出现, 有可能的话输出顺序,没可能直接输出No

题目分析:

我们用栈进行模拟, 先判断是否有这种出栈的可能, 假如有这种可能的话我们在模拟 一遍出栈的顺就行了。

每一次有新元素入栈我们就判断他是否雨 第二个字符串的第一个元素是否相等, 假如一样, 就让次元素出栈。

代码

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <stack>
 7 using namespace std;
 8 #define maxn 15
 9
10
11 bool OK(char str1[], char str2[],int n)//判断是否存在这种出栈的可能
12 {
13     stack<char> P;
14     P.push(‘#‘);//因为有元素要删除赋值, 不能为空, 我们让第一个元素为‘#’
15     char ans;
16     int i, j;
17     for(i=0, j=0; i<n; i++)
18     {
19         P.push(str1[i]);
20         ans = P.top();
21         while(ans == str2[j] )
22         {
23             P.pop();
24             j ++;
25             ans = P.top();//此处是要赋值判断的, 假如为空的话就无法赋值, 因此我们让第一个元素为‘#’
26         }
27     }
28     if(j == n)
29         return true;
30     return false;
31 }
32
33 void Putt(char str1[], char str2[],int n)
34 {
35     stack<char> P;
36     P.push(‘#‘);
37     for(int i=0, j=0; i<n; i++)
38     {
39         P.push(str1[i]);
40         printf("in\n");
41         char ans = P.top();
42         while(ans == str2[j])
43         {
44             P.pop();
45             printf("out\n");
46             j ++;
47             ans = P.top();
48         }
49     }
50 }
51 int main()
52 {
53     int n;
54     char str1[maxn], str2[maxn];
55     while(cin >> n >> str1 >> str2)
56     {
57         if(OK(str1, str2, n) )
58         {
59             printf("Yes.\n");
60             Putt(str1,str2,n);
61         }
62         else
63             printf("No.\n");
64
65         printf("FINISH\n");
66     }
67     return 0;
68 }
时间: 2024-10-01 15:53:41

HDU Train Problem I 1022 栈模拟的相关文章

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

HDU1022 Train Problem I (栈)

栈+队列 1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 #include<queue> 5 using namespace std; 6 int main() 7 { 8 int n; 9 char a[11],b[11]; 10 stack<char>s; 11 queue<int>q; 12 while(scanf("%d",&

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): 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^).

Train Problem I(栈)

Train Problem I Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student wa

杭电 ACM HDU Train Problem I

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

HDU Train Problem II (卡特兰数+大数)

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway.   Input The input contains

HDU 1022 Train Problem I 用栈瞎搞

题目大意:有n辆火车,按一定的顺序进站(第一个字符串顺序),问是否能按规定的顺序出站(按第二个字符串的顺序出去),如果能输出每辆火车进出站的过程. 题目思路:栈的特点是先进后出,和题意类似,还有有一种情况是:开进来立马有开出去.并用vis[]数组的0,1标记进出站情况. 具体看代码 #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm&