ACM (6) -- Stack

Source: Internet
Author: User
Stack is a special linear structure. It can only operate on the top elements of the stack, such as inbound and outbound operations. The following example describes the basic usage. Title: Train 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 Shocould 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 input3 123 3213 123 312 sample outputyes. inininoutoutoutfinishno. train Station A is very busy. If train B is at the station Inbound, or a can only wait for B to exit after B (then forward first ). The question requires n (n is 9 at the maximum, and the number ranges from 1 to n) trains, the train arrival sequence, and the train exit sequence, so that you can determine whether this requirement can be met, if the sequence of inbound and outbound operations can be written. Solution: the basic principle is to first-in-first-out, but first-out for a specific train. Therefore, we should first consider the sequence of stack entry. In the process of stack entry, we can determine whether to exit the stack based on the sequence of the stack. If the stack is not required, we will continue to go into the stack, if an outbound stack is required, the outbound stack operation is performed. If the element to be output to the stack is not at the top of the stack, the sequence is invalid. The following is an analysis of the inbound sequence 1234 and the outbound sequence 1423. Consider the inbound Stack: 1 into the stack, and the top element of the stack is 1; Consider the outbound Stack: because the top element of the stack is the same as the first element in the outbound stack sequence, 1 out of the stack, there is no element in the Post-stack; Consider the stack: there is no element in the stack, so there is no need to exit the stack; Consider the stack: 2 into the stack, the top element of the stack is 2; Consider the stack: because the top element of the stack is 2 and the element of the stack to be output is 4, the stack cannot be output. Consider the following: 3 in the stack, and 3 in the top element of the stack, stack includes 2 and 3; Consider the stack: because the top element of the stack is 3, and the element of the stack to be output is 4, the stack cannot be output; Consider the stack: 4 into the stack, the top element of the stack is 4, and the stack includes 2, 3, and 4. Considering the top element of the stack is 4 and the element of the output stack is 4, elements 2 and 3 are the top elements of the stack. Consider the stack: because the top element of the stack is 3 and the element of the stack to be output is 2, the stack cannot be output; Consider the stack: All elements have already been written into the stack, so the stack cannot be output; the sequence is invalid because the stack cannot be imported or output, and the stack has elements. The following is the reference code:/** train problem I */public static string Test2 (INT N, string S1, string S2) {// record the operation result stringbuffer sb = new stringbuffer (); // indicates the stack int [] A = new int [9]; // indicates the top element of the stack, int Index =-1; // The number of elements in the stack, int index1 = 0; // The number of elements in the stack, int index2 = 0; // place the first element in the stack index ++; A [Index] = s1.charat (index1); index1 ++; sb. append ("in/N"); // if there are elements in the stack, process while (index1 <s1.length () | index2 <s2.length ()) {// if the top element of the stack is consistent with the element in S2 Out-of-stack if (index>-1 & s2.charat (index2) = A [Index]) {index --; sb. append ("Out/N"); index2 ++;} else if (index1 = s1.length () {// cannot implement break ;} else {// If otherwise, add the next element in S1 to the stack index ++; A [Index] = s1.charat (index1); index1 ++; sb. append ("in/N") ;}} if (index! =-1) {return "no. /nfinish/N ";} else {sb. insert (0, "yes. /n "); sb. append ("Finish/N"); return sb. tostring ();}}

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.