-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path1214 C. Bad Sequence.cpp
46 lines (43 loc) · 1 KB
/
1214 C. Bad Sequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
fastread();
ll n,f = 0,l = 0;
string s;
cin>>n>>s;
int flag = 0;
stack<char>st;
for(ll i=0; i<n; i++){
if(s[i] == '('){
st.push(s[i]);
}
else{
if(!st.empty() && st.top() == '('){
st.pop();
}
else{
st.push(s[i]);
}
}
if(!st.empty() && st.top() == ')' && !flag){
st.pop();
flag = 1;
}
}
if(flag == 0 && st.size() == 0){
cout<<"YES"<<endl;
}
else if(flag == 1 && st.size() == 1 && st.top() == '('){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}