hdu 1022:Train Problem I(数据结构,栈,递归,dfs)

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18095    Accepted Submission(s): 6764


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:   1023  1020  1032  1019  1012 
 
  数据结构:栈的使用 + 递归
  这道题上年的暑假做过,当时没做出来,这次用递归成功解决。
  思路用递归遍历所有情况并记录正确情况的步骤,最后输出。一开始没用递归,超时,我想是因为递归函数中剪枝做的比较好,所以反而要快。
  另外这道题用栈来做比较好。
  AC代码:
 
 1 #include 
 2 #include 
 3 using namespace std;
 4 char o1[1001],o2[1001];
 5 bool q[1001];
 6 int n;
 7 bool dfs(bool c,int cn,int index1,int index2,stack <char> s)
 8 {
 9     if(cn>=2*n)
10         return true;
11     if(c==true){    //当前要求入栈
12         if(index1>=n)
13             return false;
14         s.push(o1[index1++]); 
15         q[cn] = c;
16     }
17     else {
18         if(s.empty())
19             return false;    //当前这样操作不可以
20         else{
21             char t;
22             if(s.top()==o2[index2]){
23                 index2++;
24                 s.pop();
25                 q[cn] = c;
26             }
27             else 
28                 return false;
29         } 
30     }
31     if(dfs(true,cn+1,index1,index2,s))
32         return true;
33     else if(dfs(false,cn+1,index1,index2,s))
34         return true;
35     return false;
36 }
37 int main()
38 {
39     while(cin>>n){
40         cin>>o1>>o2;
41         //true为入栈,false为出栈
42         stack <char> s;
43         if(dfs(true,0,0,0,s)){    //返回情况 
44             cout<<"Yes."<<endl;
45             for(int i=0;i2;i++)
46                 if(q[i])
47                     cout<<"in"<<endl;
48                 else 
49                     cout<<"out"<<endl;
50             cout<<"FINISH"<<endl;
51         }
52         else{
53             cout<<"No."<<endl;
54             cout<<"FINISH"<<endl;
55         }
56     }
57     return 0;
58 }

 

Freecode : www.cnblogs.com/yym2013

你可能感兴趣的:(hdu 1022:Train Problem I(数据结构,栈,递归,dfs))