HDU1022: Train Problem I

Source: Internet
Author: User

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

 


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 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 321
3 123 312


Sample Output
Yes.
In
In
In
Out
Out
Out
FINISH
No.
FINISH

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

// Stack Application

[Cpp]
# Include <iostream>
# Include <stack>
# Include <cstdio>
# Include <cstring>
Using namespace std;
 
Stack <int> S;
 
Int main ()
{
Int n, I, j, k;
Bool flag [10000];
String in, out;
While (~ Scanf ("% d % * c", & n ))
{
Cin> in> out;
I = j = k = 0;
While (j <n + 1 & I <n)
{
If (! S. empty () & S. top () = out [I])
{
I ++;
Flag [k] = true;
K ++;
S. pop ();
}
Else
{
S. push (in [j]);
Flag [k] = false;
J ++;
K ++;
}
}
If (k! = 2 * n)
{
Cout <"No." <endl;
}
Else
{
Cout <"Yes." <endl;
For (I = 0; I <k; I ++)
{
If (flag [I])
Cout <"out" <endl;
Else
Cout <"in" <endl;
}
}
Cout <"FINISH" <endl;
}
 
Return 0;
}

# Include <iostream>
# Include <stack>
# Include <cstdio>
# Include <cstring>
Using namespace std;

Stack <int> S;

Int main ()
{
Int n, I, j, k;
Bool flag [10000];
String in, out;
While (~ Scanf ("% d % * c", & n ))
{
Cin> in> out;
I = j = k = 0;
While (j <n + 1 & I <n)
{
If (! S. empty () & S. top () = out [I])
{
I ++;
Flag [k] = true;
K ++;
S. pop ();
}
Else
{
S. push (in [j]);
Flag [k] = false;
J ++;
K ++;
}
}
If (k! = 2 * n)
{
Cout <"No." <endl;
}
Else
{
Cout <"Yes." <endl;
For (I = 0; I <k; I ++)
{
If (flag [I])
Cout <"out" <endl;
Else
Cout <"in" <endl;
}
}
Cout <"FINISH" <endl;
}

Return 0;
}

 

 

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.