Hangzhou Electric ACM HDU Train problem I

Source: Internet
Author: User

Train problem I Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 25008 Accepted Submission (s): 9435


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.".

Two implementations of Author:
#include <iostream> #include <vector> #include <stack>using namespace Std;int main () {int n;string str1 , Str2;while (cin>>n) {    cin>>str1>>str2;    Vector<string>cmd;stack<int>ls;int cnt[100];for (int i=0;i<str2.size (); i++) cnt[i]=str2[i]-' 0 '; int t =0;for (int j=0;j<n;j++) {ls.push (str1[j]-' 0 '), Cmd.push_back ("in"), while (!ls.empty () &&ls.top () ==cnt[t] {Ls.pop (); T++;cmd.push_back ("Out");}} if (Ls.empty ()) {    cout<< "Yes." <<endl;    for (int i=0;i<cmd.size (); i++)                    Cout<<cmd[i]<<endl;} elsecout<< "No." <<endl;cout<< "FINISH" <<ENDL;} return 0;}

#include <iostream> #include <string> #include <vector> #include <stack>using namespace Std;int    Main () {int n;    int cnt[100];    String str1,str2;        while (cin>>n) {vector<string>cmd;        Cin.get ();        cin>>str1>>str2;        stack<int>dict;        for (int i=0; i<str2.size (); i++) {cnt[i]=str2[i]-' 0 ';        } int c=0;        Dict.push (str1[c]-' 0 ');        Cmd.push_back ("in");        int t=0;            while (1) {int x=dict.top ();                while (X==cnt[t]) {loop:cmd.push_back ("out");                Dict.pop ();                    if (!dict.empty ()) {int x=dict.top ();                    t++;                    if (X==cnt[t]) {goto loop;                    }} else {t++;                Break }           } if (C+1==str1.size ()) break;            Dict.push (str1[++c]-' 0 ');        Cmd.push_back ("in"); } if (Dict.empty ()) {cout<< "Yes."            <<endl;        for (int i=0; i<cmd.size (); i++) cout<<cmd[i]<<endl; } else cout<< "No."        <<endl;    cout<< "FINISH" <<endl; } return 0;}

Hangzhou Electric ACM HDU Train problem I

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.