• 문제 링크
5966번: Cow Cotillion
The cow cotillion, a fancy dance every spring, requires the cows (shown as ">") and bulls (shown as "<") to bow to each other during a dance. Schematically, one properly bowing pair of cattle is shown like this: "><". Sometimes another pair of cattle will
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (n-- > 0) {
if (isLegal(br.readLine().split(" ")[1].toCharArray())) sb.append("legal\n");
else sb.append("illegal\n");
}
bw.write(sb.toString());
bw.flush();
}
private static boolean isLegal(char[] arr) {
Stack<Character> stk = new Stack<>();
for (char c : arr) {
if (!stk.empty() && c == '<' && stk.peek() == '>') stk.pop();
else stk.push(c);
}
return stk.empty();
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 5938 Daisy Chains in the Field - Graph Theory / Java (0) | 2023.07.04 |
---|---|
[백준] 17199 Milk Factory - Graph Theory / Java (0) | 2023.07.03 |
[백준] 23568 Find the House - Data Structure / Java (0) | 2023.07.01 |
[백준] 10654 Cow Jog - Data Structure / Java (0) | 2023.06.30 |
[백준] 10657 Cow Jog - Data Structure / Java (0) | 2023.06.30 |
댓글