百俊均衡の世界
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Queue <Character> q = new LinkedList<>();
ArrayList <Character> al = new ArrayList<>();
while (true) {
q.clear();
al.clear();
String str = sc.nextLine();
if(str.equals(".")) {
break;
}
String fis = "";
for (int i = 0; i < str.length(); i++) {
char t = str.charAt(i);
if ((t == '(') || (t == ')') || (t == '[') || (t == ']')) {
q.add(t);
al.add(t);
}
}
System.out.println(solver(q));
}
}
private static String solver(Queue <Character> q) {
Stack<Character> stack = new Stack<>();
int n = q.size();
for(int i=0 ; i<n ; i++ ) {
char c = q.remove();
if(c == '(' || c=='[') {
stack.push(c);
}
else if(c == ')') {
if(stack.empty() || stack.peek() != '(') {
return "no";
}
else {
stack.pop();
}
}
else if( c==']') {
// 스택이 비어있거나 pop할 원소가 대괄호랑 매칭이 안되는 경우
if(stack.empty() || stack.peek() != '[') {
return "no";
}
else {
stack.pop();
}
}
}
if(stack.empty()) {
return "yes";
}
else {
return "no";
}
//return "";
}
}
Reference
この問題について(百俊均衡の世界), 我々は、より多くの情報をここで見つけました https://velog.io/@d-h-k/백준-균형잡힌-세상テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol