[HDOJ1022]Train Problem I

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

手写栈,模拟出入的过程。很简单,不停入栈,如果和当前要求出站车辆相同的话就出站,否则继续入栈。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <set>
10 #include <stack>
11 #include <list>
12 #include <vector>
13
14 using namespace std;
15
16 const int maxn = 100010;
17 int n;
18 char in[maxn];
19 char out[maxn];
20 char mystack[maxn];
21 int ans[maxn];  //记下步骤
22 int top, head, stp, now;
23
24 void init() {
25     memset(mystack, 0, sizeof(mystack));
26     memset(ans, 0, sizeof(ans));
27     stp = 0;    //步骤
28     top = 0;    //栈顶
29     head = 0;   //车头
30     now = 0;    //待出栈车辆
31 }
32
33 void solve() {
34     while(head < n) {
35             mystack[++top] = in[head++];
36             ans[stp++] = 1;
37             while(top && mystack[top] == out[now]) {
38                 top--;
39                 now++;
40                 ans[stp++] = 0;
41             }
42     }
43     if(now == n) {
44         printf("Yes.\n");
45         for(int i = 0; i < stp; i++) {
46             if(ans[i]) {
47                 printf("in\n");
48             }
49             else {
50                 printf("out\n");
51             }
52         }
53         printf("FINISH\n");
54     }
55     else {
56         printf("No.\nFINISH\n");
57     }
58 }
59 int main() {
60     // freopen("in", "r", stdin);
61     while(~scanf("%d", &n)) {
62         scanf("%s %s", in, out);
63         init();
64         solve();
65     }
66 }
时间: 2024-10-13 18:59:46

[HDOJ1022]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

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

Train Problem II 卡特兰裸题(入门题)

Train Problem II  题目大意:给你一个数n,表示有n辆火车,编号从1到n,从远方驶过来,问你有多少种出站的可能. 解题思路:模拟栈的问题而已.  卡特兰问题. 1 import java.math.*; 2 import java.util.*; 3 import java.io.*; 4 5 public class Main 6 { 7 static int MS=101; 8 public static void main(String[] args) 9 { 10 Sca

hdoj 1023 Train Problem II 【卡特兰】+【高精度】

题意:询问有多少种进站出站的顺序. 经典卡特兰.我对卡特兰目前的认识就是有n个1和n个-1,组成一个为2n的数列的方式有多少种.这就跟火车进站出站类似, 至于具体的卡特兰数的介绍,百度解释的很详细. 代码1(c语言): /* h(n) = h(n-1)*(4*n-2)/(n+1); */ #include <stdio.h> #include <string.h> #define M 110 int s[M][M] = {0}, b[M]; void init(){ s[1][0]

Train Problem II HDU 1023 卡特兰数

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 se

hdoj 1022 Train Problem I 【简易STL】

题意:不解释(这题是学数据结构必做的) 以前自学数据结构的时候,只是会顺序表来模拟栈.最近简单学习了stack头文件 又来做了一遍(还是以前的味道) 代码: #include <stdio.h> #include <stack> #include <string.h> using std::stack; stack<char > s; char s1[100], s2[100]; int vis[10]; char stac[100]; int main()

Train Problem II (卡特兰数+大数问题)

卡特兰数: Catalan数 原理: 令h(1)=1,h(0)=1,catalan数满足递归式: h(n)= h(1)*h(n-1) + h(2)*h(n-2) + ... + h(n-1)h(1) (其中n>=2) 另类递归式: h(n)=((4*n-2)/(n+1))*h(n-1); 该递推关系的解为: h(n+1)=C(2n,n)/(n+1) (n=1,2,3,...) 最典型的四类应用:(实质上却都一样,无非是递归等式的应用,就看你能不能分解问题写出递归式了) 1.括号化问题. 矩阵链乘

train problem I (栈水题)

杭电1002http://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): 25276    Accepted Submission(s): 9529 Problem Description As the new term com

HDU 1022 之 Train Problem I

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