Code is ugly
[Java]
Package D0724;
Import java. util .*;
Public class HDU1022 {
Static String strIn;
Static String strOut;
Static Stack <Character> stackIn; // Save the inbound order
Static Stack <Character> stackOut; // receives the Stack Sequence
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
Int n;
While (SC. hasNext ()){
StackIn = new Stack <Character> ();
StackOut = new Stack <Character> ();
N = SC. nextInt ();
StrIn = SC. next ();
StrOut = SC. next ();
String result = getResult (n );
If (result. equals ("no "))
System. out. println ("No .");
Else {
System. out. println ("Yes .");
Result = result. trim ();
String [] str = result. split ("\\");
// Note that it cannot be String [] str = result. split (""); it does not seem to be supported by Hang Dian.
For (int I = 0; I <str. length; I ++)
If (! Str. equals (""))
System. out. println (str [I]);
}
System. out. println ("FINISH ");
}
}
Private static String getResult (int n ){
Int I, j, k;
String result = "";
Char top = strIn. charAt (0); // stack top pointer
// Initialize the stack
For (I = n-1; I> = 0; I --){
Char temp = strOut. charAt (I );
StackOut. push (temp );
}
// First let the first train enter the station
StackIn. push (strIn. charAt (0 ));
Result + = "in ";
I = j = k = 1;
While (! StackOut. isEmpty ()){
Char t = stackOut. pop ();
If (t = top ){
Result + = "out ";
StackIn. pop ();
If (! StackIn. isEmpty ())
Top = stackIn. peek ();
} Else {
For (I = j; I <n; I ++ ){
If (strIn. charAt (I) = t ){
// System. out. println (j + "-" + I );
For (k = j; k <= I; k ++ ){
StackIn. push (strIn. charAt (k ));
Result + = "in ";
}
Break;
}
}
J = I + 1;
If (I> = n ){
Result = "no ";
Return result;
}
StackIn. pop ();
Result + = "out ";
If (! StackIn. isEmpty ())
Top = stackIn. peek ();
}
} Www.2cto.com
Return result;
}
}
Author: lhfight