https://www.acmicpc.net/problem/4949
Problem
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.io.IOException;
class Main {
public String VPS (String str) {
char[] tmp = str.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i=0; i<tmp.length; i++) {
if (tmp[i] == '.') break;
if ((tmp[i] == '(' || tmp[i] == '[')) stack.push(tmp[i]);
else if (tmp[i] == ')') {
if (!stack.isEmpty() && stack.peek() =='(') stack.pop();
else stack.push(tmp[i]);
}
else if (tmp[i] == ']') {
if (!stack.isEmpty() && stack.peek() =='[') stack.pop();
else stack.push(tmp[i]);
}
}
if (!stack.isEmpty()) return "no";
else return "yes";
}
public static void main(String[] args) throws IOException{
Main ex = new Main();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String target = br.readLine();
if (target.equals(".")) break;
else System.out.println(ex.VPS(target));
}
}
}
Result