HDU 1022 train problem I (Data Structure-stack)

Source: Internet
Author: User
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.".


Question: Give the initial sequence and sequence a and sequence B, and a stack C, A can only enter C, cannot come back, C can only enter B, find whether the target can be reached and print the path.

A typical stack is directly simulated.

# Include <stdio. h> # include <string. h> # include <algorithm> # include <math. h> # include <ctype. h> using namespace STD; typedef long ll; const int max = 0x3f3f3f; int N, OP [4004]; char a [2005], B [2005], c [2005]; int main () {While (~ Scanf ("% d % S % s", & n, a, B) {int I = 0, j = 0, Top =-1, K = 0; while (j <n) {// There are still cars in B that do not match if (a [I] = B [J]) {// The first vehicle of a and B directly match op [k ++] = 1; OP [k ++] = 0; I ++, J ++ ;} else if (top> = 0 & C [Top] = B [J]) {// the car in the stack matches B with OP [k ++] = 0; j ++, top --;} else if (I <n) {// if none of them match, put the car in a into the stack top ++; c [Top] = A [I]; OP [k ++] = 1; I ++;} else break; // exit the loop when there is no car in a} If (j> = N) {printf ("yes. \ n "); For (INT I = 0; I <K; I ++) if (OP [I] = 1) printf (" in \ n "); else printf ("Out \ n");} else printf ("no. \ n "); printf (" Finish \ n ");} return 0 ;}


Zookeeper

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.