HDU1022 (stack Application)

Source: Internet
Author: User

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

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.