이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |