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 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 is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can‘t leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output

The output contains a string "No." if you can‘t exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

Hint

Hint

For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can‘t let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

Author

Ignatius.L

Recommend

We have carefully selected several similar problems for you:  1026 1023 1004 1032 1010

这个题要是手写栈的话一定要注意范围各个参数的范围

题意: 火车进站,出站能否按所给顺序出站  标准栈。

  代码1:

 1 #include<cstdio>
 2 #include<string>
 3 #include<iostream>
 4 #define N 20
 5 using namespace std;
 6 string sc[N];
 7 string in,out;
 8 char str[N];
 9 int main()
10 {
11    int t ,i , j ,c , k ;
12    while(~scanf("%d",&t))
13    {
14                in.clear();out.clear();
15                cin>>in>>out;
16             j = 0;c =0 ; k =0;
17            for( i = 0 ; i < t ; i++ )
18            {
19              str[c]=in[i];
20              sc[k++]="in";
21              while(c!=-1&&str[c]==out[j])//这儿有点问题,c=0这儿有歧义,有时候栈为空,有时候不空,最好别这么写
22                                         //也就是,str有变化的时候,第一时间修改c的值
23                                         //这儿就是用混了,写成了c != 0,事实上,这儿c=0的时候,栈里还有一个元素
24               {
25                 sc[k++] = "out";
26                 c--;j++;
27               }
28               c++;
29         }
30         if(j==t) {printf("Yes.\n");
31           for( i = 0 ;i < k ; i++)
32             printf("%s\n", sc[i].c_str());} //string是不可以printf的,要用c_str函数,转为字符数组
33            else     printf("No.\n");
34                printf("FINISH\n");//少了换行
35     }
36     //system("PAUSE");
37     return 0;
38     }

代码2:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stack>
 4 using namespace std;
 5 int main()
 6 {
 7     int n, i, j, k, flag[50];
 8     char s1[15], s2[15];
 9     stack <char> s;
10     while(~scanf("%d %s%s",&n,s1,s2))
11     {
12         while(!s.empty())  s.pop(); //也可以不写这一句,把 stack <char> s; 就可以了
13         memset(flag,-1,sizeof(flag));
14         j = k = 0;
15         for(i = 0; i < n; i++)
16         {
17             s.push(s1[i]);
18             flag[k++] = 1;
19             while(!s.empty() && s.top() == s2[j])
20             {
21                 flag[k++] = 0;
22                 s.pop();
23                 j++;
24             }
25         }
26         if(j == n)
27         {
28             printf("Yes.\n");
29             for(i = 0; i < k; i++)
30             {
31                 if(flag[i])
32                     printf("in\n");
33                 else
34                     printf("out\n");
35             }
36         }
37         else
38             printf("No.\n");
39         printf("FINISH\n");
40     }
41     return 0;
42 }

代码3:(感谢提供代码的伟大帅气的松哥~)

 1 #include <cstdio>
 2 using namespace std;
 3
 4 int main()
 5 {
 6     int n;
 7     char o1[20], o2[20];
 8     int stack[20];
 9     bool ans[20];
10     while(~scanf("%d", &n))
11     {
12         scanf("%s %s", o1, o2);
13         int top = 0, cur = 0, c = 0;
14         for(int i = 0; i < n; i++)
15         {
16             stack[top++] = o1[i]-‘0‘;
17             ans[c++] = 1;
18             while(top > 0 && stack[top-1] == o2[cur]-‘0‘)
19             {
20                 top--;
21                 cur++;
22                 ans[c++] = 0;
23             }
24         }
25         if(top > 0) puts("No.");
26         else
27         {
28             puts("Yes.");
29             for(int i = 0; i < c; i++)
30             {
31                 if(ans[i] == 0) puts("out");
32                 else puts("in");
33             }
34         }
35         puts("FINISH");
36     }
37     return 0;
38 }
时间: 2024-10-13 03:53:53

train problem I (栈水题)的相关文章

HDU 5832 A water problem(某水题)

HDU 5832 A water problem(某水题) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two planets named Haha and Xixi in the universe and they were created with the universe beginning. There is

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

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

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

Train Problem I(栈)

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

HDU1898 Sempr == The Best Problem Solver?【水题】

Sempr == The Best Problem Solver? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1317    Accepted Submission(s): 865 Problem Description As is known to all, Sempr(Liangjing Wang) had solved mo

HDU2101 A + B Problem Too【水题】

A + B Problem Too Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12745    Accepted Submission(s): 9499 Problem Description This problem is also a A + B problem,but it has a little difference,y

HDU 5427 A problem of sorting 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5427 A problem of sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1447    Accepted Submission(s): 554 Problem Description There are many p

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