• 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
    ",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
    ",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;}
  • 相关阅读:
    【洛谷5304】[GXOI/ZOI2019] 旅行者(二进制分组+最短路)
    【LOJ6485】LJJ 学二项式定理(单位根反演)
    【CF932E】Team Work(第二类斯特林数简单题)
    【CF960G】Bandit Blues(第一类斯特林数)
    【洛谷4689】[Ynoi2016] 这是我自己的发明(莫队)
    【洛谷5355】[Ynoi2017] 由乃的玉米田(莫队+bitset)
    【洛谷5268】[SNOI2017] 一个简单的询问(莫队)
    【洛谷4688】[Ynoi2016] 掉进兔子洞(莫队+bitset)
    【洛谷3653】小清新数学题(数论)
    【洛谷6626】[省选联考 2020 B 卷] 消息传递(点分治基础题)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4679652.html
Copyright © 2020-2023  润新知