Train problem I HDU 1022

Source: Internet
Author: User
Train problem I

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6948 accepted submission (s): 2547

Problem description

As
The new term comes, the Ignatius train station is very busy nowadays.
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
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
Can't leave until train B leaves. The pictures below figure out
Problem. Now the problem for you is, there are at most 9 trains in
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
Trains can get out in an order O2.
 

Input

The
Input contains several test cases. Each test case consists of
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 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
Railway, and "out" for a train getting out of the Railway). Print
Line contains "finish" after each test case. More details in the sample
Output.

Sample input3 123 321
3 123 312

Sample outputyes.
In
In
In
Out
Out
Out
Finish
No.
Finish

// Simulate stack with Array
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>

Char str1 [20], str2 [20];
Int stack [20], top;
Int visit [20];
Int N;

Void solve ()
{
Int I, j, Len, K = 0, L;


For (I = 0, j = 0; I <n; I ++ ){
If (str1 [I]! = Str2 [J]) {

Stack [top ++] = str1 [I]-'0 ';

Visit [k ++] = 1;


}
Else {
Stack [top ++] = str1 [I]-'0 ';
Visit [k ++] = 1;
For (L = Top-1; L> = 0; l --){
If (stack [l] = str2 [J]-'0 '){
Top --;
Visit [k ++] = 0;
J ++;
}
Else
Break;
}
}
}
// Printf ("% d \ n", top, J );
If (j = N)
Puts ("yes .");
Else {
Puts ("no .");
Puts ("finish ");
Return;
}
For (I = 0; I <K; I ++)
If (visit [I])
Puts ("in ");
Else
Puts ("out ");
Puts ("finish ");

}



Int main ()
{

While (scanf ("% d", & N )! = EOF ){
Scanf ("% S % s", str1, str2 );
Top = 0;
Memset (stack, 0, sizeof (stack ));
Memset (visit, 0, sizeof (visit ));
Solve ();
}
Return 0;
}

Hint

Hint


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

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.