#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll hashmod = 1230523952352LL;
const ll k=31LL;
const int maxn = 100100;
map<ll, vector<int> >wi[26];
map<ll, vector<int> >wo[26];
string code, seq;
ll power[maxn], chash[maxn];
int n;
bool check() {
stack<char>st;
if(n%2==1) return false;
for(int i=0;i<code.length();i++) {
if(st.empty()) st.push(code[i]);
else if(!st.empty() && st.top() == code[i]) st.pop();
else if(!st.empty() && st.top() != code[i]) st.push(code[i]);
}
if(st.empty()) return true;
else return false;
}
ll bin_search(int bukva, ll ch, int value) {
int index = 0;
for(int cekor = wi[bukva][ch].size();cekor>0;cekor/=2) {
while(index+cekor < wi[bukva][ch].size() && wi[bukva][ch][index+cekor] <= value) index+=cekor;
}
return wi[bukva][ch][index];
}
void solve(int li=0, int ri=n-1) {
if(ri <= li) return;
int bukva = int(code[li] - 'a');
int closing = bin_search(bukva, chash[li], ri);
seq[li] = '(';
seq[closing] = ')';
if(closing == ri) solve(li+1, ri-1);
else {
solve(li+1, closing-1);
solve(closing+1, ri);
}
}
int main() {
cin>>code;
seq = code;
n = code.length();
if(!check()) {
cout<<-1;
return 0;
}
ll curr_hash = 0LL;
power[0] = 1LL;
for(int i=1;i<=n;i++) {
power[i] = 1LL*(power[i-1] * k) % hashmod;
}
stack<char>st;
for(int i=0;i<n;i++) {
wo[int(code[i]-'a')][curr_hash].push_back(i);
chash[i] = curr_hash;
if(st.empty()) {
st.push(code[i]);
curr_hash += (int(code[i]-'a' + 1) * power[st.size()]) % hashmod;
curr_hash %= hashmod;
}
else {
if(st.top() == code[i]) {
curr_hash += hashmod;
curr_hash -= (int(st.top()-'a' + 1) * power[st.size()]) % hashmod;
curr_hash += hashmod;
curr_hash %= hashmod;
st.pop();
}
else {
st.push(code[i]);
curr_hash += (int(code[i]-'a' + 1) * power[st.size()]) % hashmod;
curr_hash %= hashmod;
}
}
wi[int(code[i]-'a')][curr_hash].push_back(i);
}
solve();
cout<<seq<<"\n";
return 0;
}
Compilation message
match.cpp: In function 'bool check()':
match.cpp:16:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<code.length();i++) {
~^~~~~~~~~~~~~~
match.cpp: In function 'long long int bin_search(int, long long int, int)':
match.cpp:28:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(index+cekor < wi[bukva][ch].size() && wi[bukva][ch][index+cekor] <= value) index+=cekor;
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
568 KB |
Output is correct |
4 |
Correct |
3 ms |
776 KB |
Output is correct |
5 |
Correct |
4 ms |
836 KB |
Output is correct |
6 |
Correct |
3 ms |
836 KB |
Output is correct |
7 |
Correct |
4 ms |
836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
568 KB |
Output is correct |
4 |
Correct |
3 ms |
776 KB |
Output is correct |
5 |
Correct |
4 ms |
836 KB |
Output is correct |
6 |
Correct |
3 ms |
836 KB |
Output is correct |
7 |
Correct |
4 ms |
836 KB |
Output is correct |
8 |
Correct |
8 ms |
1720 KB |
Output is correct |
9 |
Correct |
10 ms |
1872 KB |
Output is correct |
10 |
Correct |
11 ms |
2264 KB |
Output is correct |
11 |
Correct |
15 ms |
2272 KB |
Output is correct |
12 |
Correct |
173 ms |
14556 KB |
Output is correct |
13 |
Correct |
181 ms |
15964 KB |
Output is correct |
14 |
Correct |
185 ms |
16636 KB |
Output is correct |
15 |
Correct |
42 ms |
16636 KB |
Output is correct |
16 |
Correct |
39 ms |
16636 KB |
Output is correct |
17 |
Correct |
167 ms |
16636 KB |
Output is correct |
18 |
Correct |
47 ms |
16636 KB |
Output is correct |
19 |
Correct |
273 ms |
21516 KB |
Output is correct |
20 |
Correct |
146 ms |
21516 KB |
Output is correct |
21 |
Correct |
264 ms |
21516 KB |
Output is correct |