(栈)HDU 1022-Train Problem I

原题链接:

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


翻译:

随着新学期的到来,如今Ignatius火车站很繁忙,许多学生想要坐火车回学校(因为Ignatius火车站的火车是全世界最快的)。但是这里有个问题,在所有的火车站点上只有一个铁轨,所以,所有火车都是从一边进入,再从另一边出去。对于这个问题,如果火车A第一个进入铁轨,然后火车B在火车A前离开,一直到火车B离开,火车A都不能离开。这个图片展示了这个问题。现在在火车站上有最多有9个火车,所有的火车有一个编号(从1到n),火车以O1次序进入铁轨,你的任务是确定火车是否能以O2的次序离开。

输入包含多个测试数据。每组测试数据是由一个整数(列车的数量)和两个字符串,火车进入的次序是:O1,火车离开的次序:O2,到文件尾结束。

如果你不能交换O1和O2,你要输出一行“No”,否则你应该输出一行“Yes”,然后输出你交换次序的方式(火车入轨为”in”,火车出轨为”out”),最后打印一行“FINISH”。


c语言:

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 struct Node{
  5     int num;
  6     struct Node *next;
  7 };
  8 
  9 typedef struct Node *NODE;
 10 typedef NODE Stack;
 11 Stack CreatStack();
 12 void MakeEmpty(Stack s);
 13 void push(int n, Stack s);
 14 void pop(Stack s);
 15 int top(Stack s);
 16 int isEmpty(Stack s);
 17 
 18 
 19 int main(){
 20     int n;
 21     int a,b,c;
 22     //a指向下一次要进入的火车
 23     //b指向下一次要出去的火车
 24     char in[15] = { 0 };
 25     char out[15] = { 0 };
 26     int step[25];
 27     //数组step和c用来记录火车的进入和出去,默认-1,进入1,出去0
 28     while (scanf("%d %s %s", &n,in,out)!=EOF){
 29             memset(step,-1,sizeof(step));
 30         Stack s = CreatStack();
 31         a = 0;
 32         b = 0;
 33         c = 0;
 34         while (a < n){
 35             //如果栈不是空,或者栈顶不符合当前需要出去的火车吗,就进入
 36             if (isEmpty(s) || top(s) != out[b]-'0'){
 37                 push(in[a] - '0', s);
 38                 step[c++] = 1;
 39                 a++;
 40             }
 41             //循环判断可以出去的
 42             while (!isEmpty(s) && top(s) == out[b]-'0'){
 43                 pop(s);
 44                 step[c++]=0;
 45                 b++;
 46             }
 47         }
 48         if (a == n ){
 49             if (isEmpty(s)){
 50                 printf("Yes.
");
 51                 int i = 0;
 52                 for(i=0;i<c;i++){
 53                         if(step[i]==1){
 54                                 printf("in
");
 55                             }
 56                             else{
 57                                 printf("out
");
 58                             }
 59                 }
 60             }
 61             else{
 62                 printf("No.
");
 63             }
 64         }
 65         printf("FINISH
");
 66     }
 67     return 0;
 68 }
 69 
 70 //检查栈是否为空
 71 int isEmpty(Stack s){
 72     return s->next == NULL;
 73 }
 74 //清空栈
 75 void MakeEmpty(Stack s){
 76     while (!isEmpty(s)){
 77         pop(s);
 78     }
 79 }
 80 //建立栈
 81 Stack CreatStack(){
 82     Stack s;
 83     s = malloc(sizeof(struct Node));
 84     s->next = NULL;
 85     MakeEmpty(s);
 86     return s;
 87 }
 88 //进栈
 89 void push(int n, Stack s){
 90     NODE new_node;
 91     new_node = malloc(sizeof(struct Node));
 92     new_node->num = n;
 93     new_node->next = s->next;
 94     s->next = new_node;
 95 }
 96 //出栈
 97 void pop(Stack s){
 98     NODE temp_node = s->next;
 99     s->next = temp_node->next;
100     free(temp_node);
101 }
102 //返回栈顶
103 int top(Stack s){
104     if (!isEmpty(s)){
105         return s->next->num;
106     }
107     return 0;
108 }
View Code

C++:

 1 #include<iostream>
 2 #include<stack>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int main(){
 8     int n;
 9     int a,b,c;
10     char in[15];
11     char out[15];
12     int step[25];
13     while(cin>>n>>in>>out){
14         memset(step,-1,sizeof(step));
15         stack<int> s;
16         a=b=c=0;
17         while(a<n){
18             if(s.empty()||s.top()!=out[b]-'0'){
19                 s.push(in[a]-'0');
20                 step[c++] = 1;
21                 a++;
22             }
23             while (!s.empty() && s.top() == out[b]-'0'){
24                 s.pop();
25                 step[c++]=0;
26                 b++;
27             }
28         }
29         if (a == n){
30             if (s.empty()){
31                 cout<<"Yes."<<endl;
32                 int i = 0;
33                 for(i=0;i<c;i++){
34                     if(step[i]==1){
35                         cout<<"in"<<endl;
36                     }
37                     else{
38                         cout<<"out"<<endl;
39                     }
40                 }
41             }
42             else{
43                 cout<<"No."<<endl;
44             }
45         }
46         cout<<"FINISH"<<endl;
47 
48     }
49     return 0;
50 }
 
原文地址:https://www.cnblogs.com/tak-fate/p/5765977.html