Algorithm๐ฐ/๋ฐฑ์ค
[๋ฐฑ์ค] 9012 ๊ดํธ string
Jouureee
2021. 4. 21. 15:04
๋ฌธ์ :
9012๋ฒ: ๊ดํธ
๊ดํธ ๋ฌธ์์ด(Parenthesis String, PS)์ ๋ ๊ฐ์ ๊ดํธ ๊ธฐํธ์ธ ‘(’ ์ ‘)’ ๋ง์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ ๋ฌธ์์ด์ด๋ค. ๊ทธ ์ค์์ ๊ดํธ์ ๋ชจ์์ด ๋ฐ๋ฅด๊ฒ ๊ตฌ์ฑ๋ ๋ฌธ์์ด์ ์ฌ๋ฐ๋ฅธ ๊ดํธ ๋ฌธ์์ด(Valid PS, VPS)์ด๋ผ๊ณ
www.acmicpc.net
๋ถ์:
์ ์ ๋ฅผ ๋ณธ ์ ์ด ์์ด ์ด๋ป๊ฒ ์ ๊ทผํ๋ฉด ํจ์จ์ ์ผ๊น ๊ณ ๋ฏผํ๋ ์ค stack์ผ๋ก ํผ ๋ฐฉ์์ด ํ๊ธฐ์ ์ด์๋ค!!
c++ ์ฝ๋ :
//
// bracket.cpp
// SOMA๐ฉ๐ป๐ป
//
// Created by JoSoJeong on 2021/04/21.
//
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int N;
bool isBracket(string s){
int len = (int)s.length();
stack <char> ss;
for(int i = 0; i < len; i++){
char c = s[i];
if(c == '('){
ss.push(c);
}else{
if(!ss.empty()){
ss.pop();
}else{
return false;
}
}
}
return ss.empty();
}
int main(){
cin >> N;
for(int i =0; i < N; i++){
string s;
cin >> s;
if(isBracket(s)){
cout << "YES" << '\n';
}else {
cout << "NO" << '\n';
}
}
return 0;
}