#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 5;
int p[N][30];
string s;
string ans;
void rec(int l, int r) {
if (l >= r) return;
if (r - l == 1) {
ans[l] = '(';
ans[r] = ')';
return;
}
if (s[l] == s[r]) {
ans[l] = '(';
ans[r] = ')';
rec(l + 1, r - 1);
return;
}
int pos = p[r][s[l] - 'a'+1];
ans[l] = '(';
ans[pos] = ')';
if (r - l <= 1) return;
rec(l + 1, pos - 1);
rec(pos + 1, r);
}
long long pw[1000 * 100 + 5];
const int MOD = 1000 * 1000 * 1000 + 7;
map<long long, vector<int> > g[30];
int main() {
ios_base::sync_with_stdio(false);
//freopen("7-router.out","w",stdout);
cin >> s;
stack<char> st;
for (int i = 0; i<s.length(); i++) {
ans += "x";
if (!st.size()) {
st.push(s[i]);
continue;
}
else {
char c = st.top();
if (c == s[i]) {
st.pop();
continue;
}
st.push(s[i]);
}
}
if (st.size()) {
cout << -1 << endl;
return 0;
}
pw[0] = 1;
for (int i = 1; i <= s.length(); i++) {
pw[i] = pw[i - 1] * 259;
pw[i] %= MOD;
}
long long hsh = 0;
for (int i = 0; i<s.length(); i++) {
if (!st.size()) {
st.push(s[i]);
hsh += (pw[st.size()] * (s[i] - 'a' + 1));
hsh %= MOD;
/*int x = mp[hsh];
if (bl[hsh]) {
p[i][s[x] - 'a'] = x;
}*/
for (int ii = 1; ii <= 26; ii++) {
for (int j = 0; j < g[ii][hsh].size(); j++) {
p[i][ii] = max(p[i][ii], g[ii][hsh][j]);
}
}
g[s[i]-'a'+1][hsh].push_back(i);
continue;
}
char c = st.top();
if (c == s[i]) {
hsh -= (pw[st.size()] * (s[i] - 'a' + 1));
hsh += ((long long)30*MOD);
hsh %= MOD;
st.pop();
}
else {
st.push(s[i]);
hsh += (pw[st.size()] * (s[i] - 'a' + 1));
hsh %= MOD;
}
/*int x = mp[hsh];
if (bl[hsh]) {
p[i][s[x] - 'a'] = x;
}*/
for (int ii = 1; ii <= 26; ii++) {
for (int j = 0; j < g[ii][hsh].size(); j++) {
p[i][ii] = max(p[i][ii], g[ii][hsh][j]);
}
}
g[s[i] - 'a' + 1][hsh].push_back(i);
}
/*for (int j = 0; j<s.length(); j++) {
for (int i = 1; i<=2; i++) cout << p[j][i] << " ";
cout << endl;
}*/
rec(0, s.length() - 1);
cout << ans << endl;
return 0;
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:35:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i<s.length(); i++) {
~^~~~~~~~~~~
match.cpp:55:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i <= s.length(); i++) {
~~^~~~~~~~~~~~~
match.cpp:60:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i<s.length(); i++) {
~^~~~~~~~~~~
match.cpp:70:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < g[ii][hsh].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~
match.cpp:94:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < g[ii][hsh].size(); j++) {
~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
1144 KB |
Output is correct |
5 |
Correct |
6 ms |
1400 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
7 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
1144 KB |
Output is correct |
5 |
Correct |
6 ms |
1400 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
7 ms |
1400 KB |
Output is correct |
8 |
Correct |
26 ms |
5368 KB |
Output is correct |
9 |
Correct |
33 ms |
6492 KB |
Output is correct |
10 |
Correct |
42 ms |
8056 KB |
Output is correct |
11 |
Correct |
39 ms |
8028 KB |
Output is correct |
12 |
Correct |
793 ms |
68088 KB |
Output is correct |
13 |
Correct |
868 ms |
73580 KB |
Output is correct |
14 |
Correct |
886 ms |
77760 KB |
Output is correct |
15 |
Correct |
578 ms |
11612 KB |
Output is correct |
16 |
Correct |
588 ms |
11656 KB |
Output is correct |
17 |
Correct |
823 ms |
55524 KB |
Output is correct |
18 |
Correct |
491 ms |
13396 KB |
Output is correct |
19 |
Correct |
1344 ms |
98932 KB |
Output is correct |
20 |
Correct |
780 ms |
67948 KB |
Output is correct |
21 |
Correct |
1132 ms |
97124 KB |
Output is correct |