#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3F3F3F3F3F3F3F3F
const int MXN = 3e6 + 5;
const int mod = 998244353;
int n;
string s, res;
void f(int l, int r)
{
int sz = r - l + 1;
if (sz & 1)
{
cout << -1 << '\n';
exit(0);
}
vector<int> idx[26];
for (int i = l; i <= r; i++) idx[s[i] - 'a'].push_back(i);
for (int i = 0; i < 26; i++)
{
if (idx[i].empty()) continue;
if (idx[i][0] > l || idx[i].back() < r) f(idx[i][0], idx[i].back());
}
int c[2] = {0, 0};
for (int i = l; i <= r; i++)
{
if (res[i] == '(') c[0]++;
else c[1]++;
}
if (c[0] > sz / 2 || c[1] > sz / 2)
{
cout << -1 << '\n';
exit(0);
}
c[0] = sz / 2 - c[0], c[1] = sz / 2 - c[1];
for (int i = l; i <= r; i++)
{
if (res[i] != '#') continue;
if (c[0]) res[i] = '(', c[0]--;
else res[i] = ')';
}
}
void _()
{
cin >> s;
res = string(n, '#');
n = s.length();
f(0, n - 1);
vector<int> v;
for (int i = 0; i < n; i++)
{
if (res[i] == ')' && v.empty())
{
cout << -1 << '\n';
exit(0);
}
if (res[i] == ')' && s[v.back()] != s[i])
{
cout << -1 << '\n';
exit(0);
}
if (res[i] == ')') v.pop_back();
else v.push_back(i);
}
cout << res << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) _();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |