#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e5 + 5;
int n, f[26], o[26];
string s, t = "";
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
for(char c : s) f[c - 'a']++;
bool ok = 0;
for(int i = 0; i < 26; i++) ok |= (f[i] & 1);
if(ok)
{
cout << -1;
return 0;
}
for(int i = 0; i < n; i++)
{
int l = s[i] - 'a';
if(f[l] > o[l])
{
t += '(';
o[l]++;
f[l]--;
}
else
{
t += ')';
o[l]--;
f[l]--;
}
}
ok = 1;
stack<int> st;
for(int i = 0; i < n; i++)
{
if(t[i] == '(') st.push(i);
else
{
ok &= s[st.top()] == s[i];
st.pop();
}
}
if(!ok) t = "-1";
cout << t;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |