hdu1022 Train Problem I---模拟栈

题目链接:
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 }

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

时间: 2024-11-05 23:20:14

hdu1022 Train Problem I---模拟栈的相关文章

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 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易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

hdu1022 train problem 栈的应用

#include #include #include using namespace std; int main() { int n; while(cin >> n) { stack one; string od1,od2; bool state[10001]; cin >> od1 >> od2; int from = 0 , to = 0; int i = 0; while(from < n) { while(od1[from] != od2[to]) one

hdu1022 Train Problem I 栈的应用

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

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

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

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 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