Hdu-1022-train problem I (c + + && simple stacks)

Source: Internet
Author: User

Train problem ITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24737 Accepted Submission (s): 9316


Problem Descriptionas The new term comes, the Ignatius Train station is very busy nowadays. A lot of student want-get back-to-school by train (because the trains in the Ignatius train station are the fastest all O Ver the world ^v^). But here comes a problem, there are only one railway where all the trains stop. The trains come in from one side and get off 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, tr Ain A can ' t leave until train B leaves. The pictures below figure out the problem. Now the problem for your is, there was at the trains of the station and all the trains have an ID (numbered from 1 to N), the Trains get into the railway in a order O1, your task is to determine whether the trains can get out of an order O2.

Inputthe input contains several test cases. Each test case consists of an integer, the number of trains, and 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 should output a line contains "Yes.", and Then output your on 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 3213) 123 312

Sample Output
Yes.inininoutoutoutFINISHNo.FINISHHintHintfor the first Sample Input, we let train 1 get in, then train 2 and Train 3.So now train 3 are at the top of the railway, so train 3 can leave first, then train 2 and train 1.In the SEC  Ond Sample input, we should let train 3 leave first, so we had 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 are at the top of the railway at the moment. So we output "No.".

Authorignatius.l
Recommendwe carefully selected several similar problems for you:1026 1023 1032 1010 1003
The topic said so much, is actually a simple stack problem, read into the number n, read two strings, the string 1 is in the order of the stack, the string 2 requirements are out of the stack order! The order of the stack is constant, but the elements of the stack can be stacked to see if the string 2 is ready for the stacking order. If you can output yes. And then put the inside of the stack out of the order of the stack out, I am here to read the user's writing, I think it is very good, with the C++stl library inside the Stack,java no similar to the stack of the class library ... If you write in Java, you have to re-write Push,pop,top,empty method, decisive C + + walk!
#include <cstdio> #include <cstring> #include <stack> #include <iostream>using namespace std;                         int main () {int n,j, k, flag[50];                            Flag array log in stack out stack order char s1[15], s2[15];    Stack <char> s;        while (~scanf ("%d%s%s", &n,s1,s2)} {while (!s.empty ()) S.pop ();        memset (flag,-1,sizeof (flag));        j = k = 0;            for (int i=0;i<n;i++) {s.push (s1[i]);            Flag[k++]=1;            while (!s.empty () &&s.top () ==s2[j])//If the stack is not empty and the top element of the stack equals the element that is currently out of the stack!                           {S.pop ();                       Out Stack flag[k++]=0;                               Mark Out Stack J + +;            Record the number of stacks}} if (J==n) {printf ("yes.\n");                for (int i=0;i<k;i++) {if (Flag[i]) {printf ("in\n");        } else        {printf ("out\n");        }}} else {printf ("no.\n");    } printf ("finish\n"); } return 0;}

Hdu-1022-train problem I (c + + && simple stacks)

Related Article

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.