#include<bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 7;
int dp[26][N];
string s, w;
stack<char> st;
void Ans(int l, int r)
{
if(l >= r)
return;
int k = dp[s[l] - 'a'][r];
//cout << l << " " << r << " " << k << " " << w << "\n";
w[l] = '('; w[k] = ')';
Ans(l + 1, k - 1); Ans(k + 1, r);
}
void Solve()
{
int n, x;
cin >> s; n = s.size();
for(int i = 1; i <= n; ++i)
w.push_back('.');
for(int i = 0; i < n; ++i)
{
if(st.size() > 0 && st.top() == s[i])
st.pop();
else
st.push(s[i]);
}
if(st.size())
{
cout << -1 << "\n"; return;
}
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < 26; ++j)
{
if(j == s[i] - 'a')
dp[j][i] = i;
else if(i != 0)
{
x = dp[s[i] - 'a'][i - 1];
if(x != 0)
dp[j][i] = dp[j][x - 1];
}
}
}
Ans(0, n - 1);
cout << w << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
9 ms |
7764 KB |
Output is correct |
13 |
Correct |
10 ms |
8532 KB |
Output is correct |
14 |
Correct |
12 ms |
9328 KB |
Output is correct |
15 |
Correct |
11 ms |
10964 KB |
Output is correct |
16 |
Correct |
12 ms |
10964 KB |
Output is correct |
17 |
Correct |
12 ms |
11544 KB |
Output is correct |
18 |
Correct |
11 ms |
10836 KB |
Output is correct |
19 |
Correct |
13 ms |
11780 KB |
Output is correct |
20 |
Correct |
10 ms |
8020 KB |
Output is correct |
21 |
Correct |
15 ms |
12620 KB |
Output is correct |