HDU 1022 Train Order
QUESTION:
SOURCE: http://acm.hdu.edu.cn/showproblem.php?pid=1022
MENTALITY
The train station is a model of stack, which operations can only occur at the top element. Two orders are given. To check whether trains can leave the station in order 2. We need to first know how to recognize when operations such as pop() and push() occur.
pop():
Two situation:
(1) trains have all stopped in the station(i = inO.length)
(2) parts of trains have stopped in the station while others have not(i < inO.length)
check if top element is equivalent to current element in outO. If so, pop the top element.
push()
Conditions:
(1) i < inO.lneght
(2) inO[i] != outO[j]
Other situation may be considered as illegal leaving order
Code
package stack.HDU;
import java.io.*;
import java.math.*;
import java.util.*;
public class HDU1022 {
public static void main(String[] args) {
InputStream inputStream = System.in;// new FileInputStream("C:\\Users\\wavator\\Downloads\\test.in");
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
static class Task {
public void solve(InputReader in, PrintWriter out) {
ArrayList<Integer> trainN = new ArrayList<>();
ArrayList<String[]> order = new ArrayList<>();
while(in.hasNext()){
trainN.add(in.nextInt());
String[] temp = new String[2];
temp[0] = in.next();
temp[1] = in.next();
order.add(temp);
}
for(int t = 0 ; t < trainN.size() ; t++){
char[] inO = order.get(t)[0].toCharArray();
char[] outO = order.get(t)[1].toCharArray();
String[] output = new String[2*trainN.get(t)];
int i = 0;
int j = 0;
int k = 0;
boolean valid = true;
Stack stack = new Stack(trainN.get(t));
stack.push(inO[i++]);
output[k++] = "in";
while(i < inO.length || j < outO.length){
if(i < inO.length){
if(stack.top() == outO[j]){
stack.pop();
output[k++] = "out";
j++;
}else {
stack.push(inO[i++]);
output[k++] = "in";
}
}else if(!stack.isEmpty() && stack.top() == outO[j]) {
stack.pop();
output[k++] = "out";
j++;
}else {
valid = false;
break;
}
}
if(valid){
out.println("Yes.");
for(int m = 0 ; m < output.length ; m++ ){
out.println(output[m]);
}
}else {
out.println("No.");
}
if(t < trainN.size()){
out.println("FINISH");
}else {
out.print("FINISH");
}
}
}
}
static class Stack{
char[] arr;
int pointer = -1;
public Stack(int length){
arr = new char[length];
}
public void push(char x){
if(pointer < arr.length-1){
arr[++pointer] = x;
}
}
public void pop(){
if(pointer > -1){
pointer--;
}
}
public char top(){
if(pointer > -1){
return arr[pointer];
}else {
return 'E';
}
}
public boolean isEmpty(){
if(pointer > -1){
return false;
}else {
return true;
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public char[] nextCharArray() {
return next().toCharArray();
}
// public boolean hasNext() {
// try {
// return reader.ready();
// } catch(IOException e) {
// throw new RuntimeException(e);
// }
// }
public boolean hasNext() {
try {
String string = reader.readLine();
if (string == null) {
return false;
}
tokenizer = new StringTokenizer(string);
return tokenizer.hasMoreTokens();
} catch (IOException e) {
return false;
}
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecinal() {
return new BigDecimal(next());
}
}
}