#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
char v[DIM],sol[DIM],s[DIM];
int poz[DIM][30];
int n,i,j;
void solve (int st, int dr){
/// in st pun ( si trb sa gasesc )
if (st >= dr)
return;
if (st == dr-1){
sol[st] = '(';
sol[dr] = ')';
return;
}
/*for (i=dr;i>st;--i){ /// aparent asta a mers bine in practica
if (!k && v[i] == v[st])
break;
if (k && s[k] == v[i])
k--;
else s[++k] = v[i];
}*/
sol[st] = '(';
int idx = poz[dr][v[st]-'a'];
sol[idx] = ')';
solve (st+1,idx-1);
solve (idx+1,dr);
}
int main (){
//ifstream cin ("match.in");
//ofstream cout ("match.out");
cin>>v+1;
n = strlen (v+1);
/// verific daca am solutie
int k = 0;
s[++k] = v[1];
for (i=2;i<=n;++i){
if (k && s[k] == v[i])
k--;
else s[++k] = v[i];
}
if (k){
cout<<-1<<"\n";
return 0;
}
for (i=1;i<=n;i++){
for (j=0;j<26;j++){
if (poz[i-1][v[i]-'a'])
poz[i][j] = poz[poz[i-1][v[i]-'a']-1][j];
}
poz[i][v[i]-'a'] = i;
}
solve (1,n);
cout<<sol+1<<"\n";
return 0;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:39:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin>>v+1;
~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
6 ms |
1152 KB |
Output is correct |
9 |
Correct |
7 ms |
1280 KB |
Output is correct |
10 |
Correct |
6 ms |
1280 KB |
Output is correct |
11 |
Correct |
6 ms |
1408 KB |
Output is correct |
12 |
Correct |
14 ms |
8320 KB |
Output is correct |
13 |
Correct |
15 ms |
9088 KB |
Output is correct |
14 |
Correct |
17 ms |
9856 KB |
Output is correct |
15 |
Correct |
18 ms |
11520 KB |
Output is correct |
16 |
Correct |
17 ms |
11520 KB |
Output is correct |
17 |
Correct |
18 ms |
12160 KB |
Output is correct |
18 |
Correct |
20 ms |
11904 KB |
Output is correct |
19 |
Correct |
20 ms |
12800 KB |
Output is correct |
20 |
Correct |
17 ms |
8704 KB |
Output is correct |
21 |
Correct |
22 ms |
13688 KB |
Output is correct |