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 {
static Stack<String> stack =new Stack<String>();
static String resultMessage="";
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
try {
String strCount=br.readLine();
int testCount=Integer.parseInt(strCount);
String tempInput="";
String strInput="";
for(int i=0;i<testCount;i++){
resultMessage="";
stack.clear();
tempInput=br.readLine();
for(int k=0;k<tempInput.length();k++){
if(k==tempInput.length()-1){
strInput=tempInput.substring(k);
if("(".equals(strInput)){
stack.push(strInput);
}else{
if(stack.size()>0){
stack.pop();
}else{
resultMessage="NO";
break;
}
}
}else{
strInput=tempInput.substring(k,k+1);
if("(".equals(strInput)){
stack.push(strInput);
}else{
if(stack.size()>0){
stack.pop();
}else{
resultMessage="NO";
break;
}
}
}
}
if(stack.size()==0&&!"NO".equals(resultMessage)){
bw.write("YES"+"\n");
}else{
bw.write("NO"+"\n");
}
}
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
'알고리즘 > 스택' 카테고리의 다른 글
1406번 에디터 (0) | 2020.06.21 |
---|---|
1894번 스택수열 (0) | 2020.06.21 |
9093번 단어 뒤집기 (0) | 2020.06.07 |
10828번 스택 (0) | 2020.06.07 |