#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e5 + 5;
string s, t;
int n, last[26][sz];
void f(int l, int r)
{
if(l > r) return;
int pos = last[s[l] - 'a'][r];
assert(pos >= 0);
t[l] = '(', t[pos] = ')';
f(l + 1, pos - 1), f(pos + 1, r);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
stack<int> st;
for(int i = 0; i < n; i++)
{
if(!st.empty() && s[st.top()] == s[i])
{
st.pop();
}
else
{
st.push(i);
}
}
if(!st.empty())
{
cout << -1;
return 0;
}
memset(last, -1, sizeof(last));
for(int i = 0; i < n; i++)
{
int l = s[i] - 'a';
last[l][i] = i;
if(i == 0) continue;
for(int j = 0; j < 26; j++)
{
last[j][i] = max(last[j][i - 1], last[j][i]);
}
}
t.resize(n);
f(0, n - 1);
assert(0);
cout << t;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
41800 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
41800 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
41800 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |