-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.cpp
112 lines (110 loc) · 2.5 KB
/
D.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
long long ara[200005];
int main()
{
fastio;
long long t,n,i,l,r;
string s;
cin>>t;
while(t--)
{
l=0;
r=0;
cin>>n;
cin>>s;
for(i=0;i<n;i++)
{
if(s[i]=='(')
l++;
else
r++;
}
if(l!=r)
{
cout<<-1<<"\n";
continue;
}
stack<pair<char,long long> > st;
for(i=0;i<n;i++)
{
if(s[i]=='(')
st.push(make_pair('(',i));
else
{
if(st.empty())
st.push(make_pair(')',i));
else if(st.top().first=='(')
{
ara[st.top().second]=1;
ara[i]=1;
st.pop();
}
else
st.push(make_pair(')',i));
}
}
long long p=1;
if(st.empty())
{
p=1;
cout<<1<<"\n";
for(i=0;i<n;i++)
cout<<1<<" ";
cout<<"\n";
continue;
}
else if(st.size()==n){
p=1;
cout<<1<<"\n";
for(i=0;i<n;i++)
cout<<1<<" ";
cout<<"\n";
continue;
}
else
{
p=2;
reverse(s.begin(),s.end());
while(!st.empty())
st.pop();
for(i=0;i<n;i++)
if(s[i]=='(')
st.push(make_pair('(',i));
else
{
if(st.empty())
st.push(make_pair(')',i));
else if(st.top().first=='(')
{
ara[st.top().second]=1;
ara[i]=1;
st.pop();
}
else
st.push(make_pair(')',i));
}
}
long long x;
if(st.size()==n || st.size()==0){
if(p==2)
cout<<1<<"\n";
x=1;
}
else{
x=2;
cout<<2<<"\n";
}
while(!(st.empty()))
{
ara[st.top().second]=x;
st.pop();
}
for(i=n-1;i>=0;i--)
cout<<ara[i]<<" ";
cout<<"\n";
}
return 0;
}