#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e3 + 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];
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);
cout << t;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Incorrect |
1 ms |
592 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |