HDU 1022 train problem I (STL stack simulation)

Source: Internet
Author: User
Train problem I Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 30420 accepted submission (s): 11492


Problem descriptionas 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.
 
Inputthe 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.
 
Outputthe output contains a string "no. "If you can't exchange O2 to O1, Or you shoshould output a line contains" yes. ", and then output your way in exchanging the order (you shocould 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 3213 123 312
 
Sample output
Yes.inininoutoutoutFINISHNo.FINISHHintHint 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

Original question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1022


The train station has only one track. Ask if the train can enter the railway station according to the previous sequence and whether the train station can be listed in the subsequent sequence.

Stack simulation:

AC code:

# Include <iostream> # include <stack> # define maxn 100 using namespace STD; int main () {stack <char> S; int N, I, J, K; bool result [maxn]; // The result array is used to indicate the result. 1 indicates the result is pushed to the stack. 0 indicates char in [maxn], out [maxn]; // sequence 1 and Sequence 2 while (CIN> N> in> out) {J = 0, I = 0, k = 1; S. push (in [0]); // to prevent empty stacks, press the result [0] = 1; // record one. While (I <n & J <n) {If (! S. empty () & S. top () = out [J]) {// if the top element of the stack is the same as the current element of Sequence 2, the stack is played, and the sequence 2 group moves one to the back. J ++; S. Pop (); Result [k ++] = 0 ;}else {// otherwise, the current element is taken from sequence 1 and pushed into the stack. S. push (in [++ I]); Result [k ++] = 1 ;}} if (I = N) // If I = n indicates that the top element of the stack is not equal to the current element of Sequence 2, and all the elements in sequence 1 have already been included in the stack, the same answer of Sequence 2 cannot be obtained. Cout <"no. "<Endl; else {cout <" yes. "<Endl; for (I = 0; I <K; I ++) if (result [I]) cout <" in "<Endl; else cout <"out" <Endl;} cout <"finish" <Endl;} return 0 ;}

AC code 2:

#include <iostream>#include <stack>#include <cstdio>#include <cstring>using namespace std;stack<int> S;int main(){    int n,i,j,k;    bool flag[10000];    string in,out;    while(~scanf("%d%*c",&n))    {        cin >> in >> out;        i = j = k = 0;        while(j<n+1 && i<n)        {            if(!S.empty() && S.top() == out[i])            {                i++;                flag[k] = true;                k++;                S.pop();            }            else            {                S.push(in[j]);                flag[k] = false;                j++;                k++;            }        }        cout<<k<<endl;        if(k!=2*n)        {            cout << "No." << endl;        }        else        {            cout << "Yes." << endl;            for(i = 0; i<k; i++)            {                if(flag[i])                    cout << "out" << endl;                else                    cout << "in" << endl;            }        }        cout << "FINISH" << endl;    }    return 0;}


AC code 3: C Language Simulation:

# include<stdio.h># include<string.h>int main(){    int n,i,j,k,head;    char a[1000],b[1000],t[1000],str[1000][10];    while(scanf("%d%s%s",&n,a,b)!=EOF)    {        head=i=j=k=0;        strcpy(str[k++],"in");        while(i<n)        {            t[head]=a[i];            while(t[head]==b[j]&&b[j]!='\0'&&t[head]!='\0')            {                head--;                j++;                strcpy(str[k++],"out");            }            head++;            strcpy(str[k++],"in");            i++;        }        if(head==0)        {            printf("Yes.\n");            for(i=0; i<k-1; i++)                printf("%s\n",str[i]);            printf("FINISH\n");        }        else        {            printf("No.\n");            printf("FINISH\n");        }    }    return 0;}




HDU 1022 train problem I (STL stack simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.