#include <bits/stdc++.h>
using namespace std;
const int len = 1e5+5, sig = 26;
char str[len], out[len];
int arr[len], dp[len][sig];
stack<int> mys;
bool check(int n){
for (int i = 1; i <= n; i++){
if (!mys.empty() && mys.top() == arr[i])
mys.pop();
else
mys.push(arr[i]);
}
return (mys.empty());
}
void solve(int l, int r){
if (l > r) return;
int k = dp[r][arr[l]];
//printf("l = %d, r = %d, k = %d\n", l, r, k);
out[l] = '(';
out[k] = ')';
solve(l+1, k-1);
solve(k+1, r);
}
int main(){
scanf("%s", str);
int n = strlen(str);
for (int i = 1; i <= n; i++)
arr[i] = str[i-1]-'a';
if (!check(n)){
printf("-1\n");
return 0;
}
for (int i = 1; i <= n; i++){
int k = dp[i-1][arr[i]];
if (k > 0)
for (int j = 0; j < sig; j++)
dp[i][j] = dp[k-1][j];
dp[i][arr[i]] = i;
}
solve(1, n);
for (int i = 1; i <= n; i++)
printf("%c", out[i]);
printf("\n");
return 0;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
1128 KB |
Output is correct |
9 |
Correct |
6 ms |
1152 KB |
Output is correct |
10 |
Correct |
5 ms |
1280 KB |
Output is correct |
11 |
Correct |
6 ms |
1280 KB |
Output is correct |
12 |
Correct |
12 ms |
7424 KB |
Output is correct |
13 |
Correct |
13 ms |
8064 KB |
Output is correct |
14 |
Correct |
14 ms |
8832 KB |
Output is correct |
15 |
Correct |
16 ms |
10112 KB |
Output is correct |
16 |
Correct |
17 ms |
10112 KB |
Output is correct |
17 |
Correct |
18 ms |
10752 KB |
Output is correct |
18 |
Correct |
19 ms |
10624 KB |
Output is correct |
19 |
Correct |
18 ms |
11392 KB |
Output is correct |
20 |
Correct |
19 ms |
7544 KB |
Output is correct |
21 |
Correct |
20 ms |
12160 KB |
Output is correct |