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
 
Problem Description
As 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 cant 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.
 
Input
The 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.
 
Output
The output contains a string "No. "if you cant 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 321
3 123 312
 
Sample Output
Yes.
In
In
In
Out
Out
Out
FINISH
No.
FINISH
 
Train Station A is very busy. If train B enters the station before train A leaves the station, or train A can only wait or exit after train B leaves the station ). 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 stack entry: 1. The top element of the stack is 1;
Think out of Stack: because the top element of the stack is the same as the first element in the output stack sequence, the top element of the stack is 1 out of the stack, and no element exists in the back of the stack;
Think out of Stack: there is no element in the stack, so there is no need to go out of the stack;
Consider stack entry: 2 into the stack, and 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 stack entry: 3 into the stack, the top element of the stack is 3, and the 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 stack entry: 4 into the stack. The top element of the stack is 4. The stack includes 2, 3, and 4;
Consider the stack: because the top element of the stack is 4 and the element of the stack is 4, the elements of the stack are 2 and 3, which 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 stack entry: All elements have already been written into the stack, so the stack cannot be taken out;
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 operation results
StringBuffer sb = new StringBuffer ();
// Stack
Int [] a = new int [9];
// Indicates the top element of the stack.
Int index =-1;
// Number of the inbound stack Elements
Int index1 = 0;
// Number of the output stack Element
Int index2 = 0;
// Place the first element in the stack
Index ++;
A [index] = s1.charAt (index1 );
Index1 ++;
Sb. append ("in ");
// Process as long as there are elements in the stack
While (index1 <s1.length () | index2 <s2.length ()){
// If the top element of the stack is consistent with the element in s2
If (index>-1 & s2.charAt (index2) = a [index]) {
Index --;
Sb. append ("out ");
Index2 ++;
} Else if (index1 = s1.length () {// cannot be implemented
Break;
} Else {// If otherwise, add the next element in s1 to the stack.
Index ++;
A [index] = s1.charAt (index1 );
Index1 ++;
Sb. append ("in ");
}
}
If (index! =-1 ){
Return "No. FINISH ";
} Else {
Sb. insert (0, "Yes .");

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.