HDU 1022 Train Problem I
Posted arrokoth
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1022 Train Problem I相关的知识,希望对你有一定的参考价值。
写了好久orz。。。
姑且贴个题目吧。
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 56851 Accepted Submission(s): 21249
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解题思路:
一开始并不会写,莫得想法。看了评论区大佬的样例写了写才有感觉。。。
有几组特别好的样例:
6 123121 121321
Yes.
in
out
in
out
in
in
out
out
in
out
in
out
FINISH
Yes.
in
out
in
out
in
in
out
out
in
out
in
out
FINISH
5
12342 24321
Yes.
in
in
out
in
in
out
out
in
out
out
FINISH
12342 24321
Yes.
in
in
out
in
in
out
out
in
out
out
FINISH
理解之后问题就不大了(虽然我觉得我写的仍然很麻烦)
比如看第一组数据的话:
123121 121321
第一个进站 1
第一个出站 1
所以这里 1 进站之后就立刻出站了。
然后往后看:
第二个进站 2
第二个出站 2
第三进站 3
但第三出站是 1
所以这之后还有入站的火车。
直到 1 入站后再看下个出站火车的编号。
到这里就能有一个大致的思路了。大概是模拟一个类似进出栈的过程。比较栈内数和应出站火车编号来判断是否出栈。比较进站火车与出站火车编号来判断是否入栈。
完整代码:
1 #include <cmath> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <climits> 6 #include <map> 7 #include <set> 8 #include <queue> 9 #include <stack> 10 #include <vector> 11 #include <string> 12 #include <iostream> 13 #include <algorithm> 14 15 16 #define N 100010 17 18 using namespace std; 19 20 typedef long long int ll; 21 22 int main() 23 { 24 int n; 25 while(cin>>n){ 26 char ch; 27 stack<char> sk; //栈 28 vector<char> vc1; //进入顺序 29 vector<char> vc2; //驶出顺序 30 vector<int> time; //记录in/out 31 //输入字符 32 for(int i=0; i<n; i++){ 33 cin>>ch; 34 vc1.push_back(ch); 35 } 36 for(int i=0; i<n; i++){ 37 cin>>ch; 38 vc2.push_back(ch); 39 } 40 //i, j用来遍历 41 int i, j; 42 i=j=0; 43 while(i<n && j<n){ 44 if(vc1[i]!=vc2[j]){ 45 sk.push(vc1[i]); 46 time.push_back(1); 47 i++; 48 } 49 else if(vc1[i] == vc2[j]){ 50 //模拟一次弹栈出栈的过程 51 time.push_back(1); 52 time.push_back(0); 53 i++, j++; 54 //将可能在栈内的数弹出 55 if(!sk.empty()){ 56 while(!sk.empty() && j<n && vc2[j]==sk.top()){ 57 sk.pop(); 58 time.push_back(0); 59 j++; //将相同的数字弹出栈 60 } 61 } 62 } 63 } 64 int flag=1; 65 if(i==n && j<n){ 66 //检验剩下的顺序是否正确 67 while(j<n){ 68 if(vc2[j]!=sk.top()){ 69 flag=0; 70 break; 71 } 72 else{ 73 time.push_back(0); 74 sk.pop(); 75 j++; 76 } 77 } 78 } 79 else if(j==n && i<n) flag=0; //没找到这样的样例,以防万一还是写吧^^ 80 //顺序正确的话 81 if(flag) { 82 printf("Yes. "); 83 n=time.size(); 84 for(i=0; i<n; i++){ 85 if(time[i]) printf("in "); 86 else printf("out "); 87 } 88 } 89 else printf("No. "); 90 printf("FINISH "); 91 //多实例,清空 92 time.clear(); 93 while(!sk.empty()) sk.pop(); 94 vc1.clear(); 95 vc2.clear(); 96 } 97 return 0; 98 }
以上是关于HDU 1022 Train Problem I的主要内容,如果未能解决你的问题,请参考以下文章