hdu1022 Train Problem I

http://acm.hdu.edu.cn/showproblem.php?pid=1022

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 #include<stack>
 7 using namespace std;
 8 const int N=100;
 9
10 int main()
11 {
12     //freopen("in.txt","r",stdin);
13     stack<char>s;
14     int n,result[N];
15     char s1[N],s2[N];
16     int i,j,k;
17     while(cin>>n>>s1>>s2)
18     {
19         i=0;j=0;k=1;
20         s.push(s1[i]);
21         result[0]=1;
22         while(i<n&&j<n)
23         {
24             if(s.size() && s.top()==s2[j])//如果栈取元素和s2当前元素相同,弹栈
25             {
26                 j++;
27                 s.pop();
28                 result[k++]=0;
29             }
30             else//如果不相同,继续讲s1后一个元素压栈
31             {
32                 if(i==n)
33                 break;
34                 s.push(s1[++i]);
35                 result[k++]=1;
36             }
37         }
38         if(i==n)//如果i==n代表找不到s2中的当前元素
39         cout<<"No."<<endl;
40         else
41         {
42             cout<<"Yes."<<endl;
43             for(i=0;i<k;i++)
44             {
45                 if(result[i])
46                 cout<<"in"<<endl;
47                 else
48                 cout<<"out"<<endl;
49             }
50         }
51         cout<<"FINISH"<<endl;
52     }
53     return 0;
54 }
时间: 2024-10-07 06:43:33

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",&

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

HDU1022 Train Problem I 栈的模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常见错误:使用前未清空栈 使用STL思路较为清晰 代码附上, 欢迎各位大神指点~~ #include <cstdio> #include <stack> #include <iostream> #include <vector> using namespace s

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<

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