SW Expert Academy 1974. 공과 잡초
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AYGtoa3qARcDFARC
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for(int t=1; t<=T; t++) {
sb.append('#').append(t).append(' ');
char ch[] = br.readLine().toCharArray();
char temp = ch[0];
int result = 0;
int len = ch.length;
for(int i=1; i<len; i++) {
String str = Character.toString(temp) + Character.toString(ch[i]);
if( str.equals("(|") || str.equals("|)") ) {
result++;
}
else if( str.equals("()") ) result++;
temp = ch[i];
}
sb.append(result).append('\n');
}
bw.write(sb.toString()); bw.flush(); bw.close();
} // End of main
} // End of Main class
import java.io.*
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`))
val sb = StringBuilder()
val T = br.readLine().toInt()
for(t in 1..T) {
sb.append('#').append(t).append(' ')
val ch = br.readLine().toCharArray()
var temp = ch[0]
var result = 0
val len = ch.size
for(i in 1 until len) {
var str = temp.toString() + ch[i].toString()
if(str.equals("(|") || str.equals("|)")) result++
else if(str.equals("()")) result++
temp = ch[i]
}
sb.append(result).append('\n')
}
bw.write(sb.toString()); bw.close();
} // End of main