(Hdu step 8.1.2) Train problem I (station basic application--judging whether a sequence can get a second sequence after the stack)

Source: Internet
Author: User

Topic:

Train problem I
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): Accepted submission (s): 60
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.
Output
The output contains a string \\\\\\\ "no.\\\\\\\" If you can\\\\\\\ ' t Exchange O2 to O1, or your should output a line Contai NS \\\\\\\ "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
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 this we can\\\\\\\ ' t let train 1 leave before train 2, because train 2 was at the top of the railway at the Momen t.so We output \\\\\\\ "no.\\\\\\\".
Authorignatius.l


Topic Analysis:

The application of the station, simple question.



The code is as follows:

/* * b1.cpp * * Created on:2015 March 19 * author:administrator * * #include <iostream> #include <cstdio> #in Clude <stack>using namespace Std;const int maxn = 101;int Main () {int n;//sequence number of elements string str1;//first sequence string str2;//second sequence int result[maxn];//is used to mark in and out of the stack while (CIN >> n >> str1 >> str2) {stack<char> st;int i = 0;//used to mark the first Where does the sequence compare now? int J = 0;//is used to mark where the second sequence is currently compared to int k;st.push (str1[0]);//The first element of the first sequence is now in the stack result[0] = True;k = 1;while (i < n &amp ;& J < N) {//When the sequence has not been compared//if the stack is not empty && the stack top element and the second sequence are currently compared to the same element as the IF ((st.empty () = = False) && (st.top () = = str2[ J]) {j++;//Moves the index of the second sequence backward by one st.pop ();//stack top element out of stack result[k++] = false;//tag element in and out}else{//if the stack is already empty | | The top element of the stack is not equal to the element that is currently matched in the second sequence if (i = = N) {///If all elements of the first element are already in the stack break;//jump out of the loop}//otherwise there are elements in the first sequence that are not in the stack St.push (Str1[++i]);// Lower the next element of a sequence result[k++] = true;//mark in and out}}//output if (i = = N) {cout << "No." << Endl;} Else{cout << "Yes" << endl;for (i = 0; i < K; ++i) {if (result[i] = true) {cout << "In" << Endl;} Else{cout << "Out" << Endl;}}} cout << "FINISH" << Endl;} return 0;}




(Hdu step 8.1.2) Train problem I (station basic application--judging whether a sequence can get a second sequence after the stack)

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.