#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void add(vector<char> &st, char c)
{
if (!st.empty() && st.back() == c) st.pop_back();
else st.push_back(c);
}
const int maxn = 1e5 + 5, abc = 26;
//const int maxn = 15, abc = 2;
vector<vector<int> > pv(maxn, vector<int>(abc, -1));
string ans, s;
void rek(int l, int r) // vratane
{
if (l > r) return;
int m = pv[r][s[l]-'a'];
assert(l < m);
ans[l] = '(', ans[m] = ')';
rek(l+1, m-1), rek(m+1, r);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
int n = s.size();
vector<char> st;
for (int i = 0; i < n; i++)
add(st, s[i]);
if (!st.empty())
{
cout << "-1\n";
return 0;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < abc; j++)
{
if (s[i]-'a' == j) pv[i][j] = i;
else if (i && pv[i-1][s[i]-'a'] > 0) pv[i][j] = pv[pv[i-1][s[i]-'a']-1][j];
//cout << pv[i][j] << " ";
}
//cout << endl;
}
ans.resize(n, '?');
rek(0, n-1);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13516 KB |
Output is correct |
2 |
Correct |
11 ms |
13572 KB |
Output is correct |
3 |
Correct |
11 ms |
13516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13516 KB |
Output is correct |
2 |
Correct |
11 ms |
13572 KB |
Output is correct |
3 |
Correct |
11 ms |
13516 KB |
Output is correct |
4 |
Correct |
11 ms |
13516 KB |
Output is correct |
5 |
Correct |
11 ms |
13608 KB |
Output is correct |
6 |
Correct |
11 ms |
13628 KB |
Output is correct |
7 |
Correct |
11 ms |
13644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13516 KB |
Output is correct |
2 |
Correct |
11 ms |
13572 KB |
Output is correct |
3 |
Correct |
11 ms |
13516 KB |
Output is correct |
4 |
Correct |
11 ms |
13516 KB |
Output is correct |
5 |
Correct |
11 ms |
13608 KB |
Output is correct |
6 |
Correct |
11 ms |
13628 KB |
Output is correct |
7 |
Correct |
11 ms |
13644 KB |
Output is correct |
8 |
Correct |
12 ms |
13608 KB |
Output is correct |
9 |
Correct |
12 ms |
13728 KB |
Output is correct |
10 |
Correct |
12 ms |
13636 KB |
Output is correct |
11 |
Correct |
12 ms |
13760 KB |
Output is correct |
12 |
Correct |
17 ms |
14572 KB |
Output is correct |
13 |
Correct |
19 ms |
14716 KB |
Output is correct |
14 |
Correct |
19 ms |
14916 KB |
Output is correct |
15 |
Correct |
20 ms |
15436 KB |
Output is correct |
16 |
Correct |
21 ms |
15364 KB |
Output is correct |
17 |
Correct |
21 ms |
15452 KB |
Output is correct |
18 |
Correct |
22 ms |
14560 KB |
Output is correct |
19 |
Correct |
22 ms |
14844 KB |
Output is correct |
20 |
Correct |
17 ms |
14780 KB |
Output is correct |
21 |
Correct |
24 ms |
15240 KB |
Output is correct |