标签:
这道题:是模拟出栈,判断出栈顺序的可能性。
基本上大家的做法都是直接模拟栈的出栈入栈并将顺序用0,1序列来表示,我暂时没想到什么好的思路。
import java.util.*; import java.io.*; public class Main{ public static void main(String[] arg){ Scanner scan = new Scanner(new BufferedInputStream(System.in)); int[] outFlag = new int[18];//最多有9辆火车,1表示出,0表示进 char[] stack = new char[20]; while(scan.hasNextInt()){ int n =scan.nextInt(); char[] put; char[] pop; put= scan.next().toCharArray(); pop = scan.next().toCharArray(); int top = -1; int in=0,out=0,flag=0; while(out!=n&&in!=n+1){ if(top!=-1&&stack[top] == pop[out]){ top--; out++; outFlag[flag++] = 1; continue; } if(in==n){ break; } top++; stack[top] = put[in++]; outFlag[flag++] = 0; } if(flag==2*n){ System.out.println("Yes."); for(int i = 0 ; i!= flag ; i ++){ if(outFlag[i]==0){ System.out.println("in"); continue; } System.out.println("out"); } }else{ System.out.println("No."); } System.out.println("FINISH"); } scan.close(); } }
标签:
原文地址:http://www.cnblogs.com/xiezie/p/5586730.html