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

题解:火车进站,类似于栈;;;

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stack>
 4 using namespace std;
 5 char in[20],out[10],m[30][5];
 6 int t;
 7 void display(char *s){
 8     strcpy(m[t++],s);
 9 }
10 int main(){
11     int n;
12     while(~scanf("%d",&n)){memset(m,0,sizeof(m));
13     memset(in,0,sizeof(in));
14     memset(out,0,sizeof(out));
15         t=0;scanf("%s%s",in,out);
16         stack<char>train;
17             for(int j=0,i=0;i<n;){
18                 if(!train.empty()&&train.top()==out[i])display("out"),i++,train.pop();
19                 else if(in[j])train.push(in[j++]),display("in");
20                 else break;
21             }//printf("%d\n",train.size());
22             //while(!train.empty())printf("%c",train.top()),train.pop();
23             display("FINISH");
24         if(train.empty()){puts("Yes.");
25         for(int i=0;i<t;++i)printf("%s\n",m[i]);}
26         else puts("No."),puts("FINISH");
27     }
28     return 0;
29 }

另外,自己写了几个关于栈的括号配对问题,贴下:

代码:

 1 #include<stdio.h>
 2 char m[10010];
 3 int top;
 4 bool pop(){
 5     top--;
 6     if(top<0)return false;
 7     else return true;
 8 }
 9 void push(char s){
10     top++;
11     m[top]=s;
12 }
13 int main(){
14     char x[10010];
15     int T;
16     scanf("%d",&T);
17     while(T--){top=0;
18         scanf("%s",x);
19         for(int i=0;x[i];i++){
20             if(x[i]==‘(‘||x[i]==‘[‘)push(x[i]);
21             else if(x[i]==‘)‘&&m[top]==‘(‘||x[i]==‘]‘&&m[top]==‘[‘){if(!pop())break;}
22             else push(x[i]);
23         }//printf("%d",top);
24         //while(top)printf("%c",m[top--]);
25         if(top==0)puts("Yes");
26         else puts("No");
27     }
28     return 0;
29 }
 1 #include<stdio.h>
 2 #include<stack>
 3 using namespace std;
 4 char s[10010];
 5 int main(){
 6     int T,temp;
 7     scanf("%d",&T);
 8     while(T--){temp=1;
 9     stack<char>m;
10         scanf("%s",s);
11         for(int i=0;s[i];i++){if(m.empty()&&(s[i]==‘)‘||s[i]==‘]‘)){
12             temp=0;
13             puts("No");
14             break;
15         }
16             if(s[i]==‘(‘||s[i]==‘[‘)m.push(s[i]);
17             else if(s[i]==‘)‘&&m.top()==‘(‘||s[i]==‘]‘&&m.top()==‘[‘)m.pop();
18             else m.push(s[i]);
19         }
20         if(m.empty()&&temp)puts("Yes");
21         else if(temp)puts("No");
22     }
23 return 0;}
时间: 2024-08-02 16:19:22

Train Problem I(栈)的相关文章

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

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 (栈)

栈+队列 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

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

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

hdoj 1022 Train Problem I 【顺序栈】

最近打算重新看一边数据结构,昨天看的时候感觉栈这部分部分能看懂了. 于是就赶紧来实践一下!! Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19493    Accepted Submission(s): 7305 Problem Description As the new term comes, t

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