#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
set<int>indexes[26];
string code;
int n;
bool check_valid() {
if(n%2==1) return false;
stack<char>st;
for(int i=0;i<n;i++) {
if(st.empty()) st.push(code[i]);
else if(!st.empty() && st.top() == code[i]) st.pop();
else if(!st.empty() && st.top() != code[i]) st.push(code[i]);
}
if(st.size() == 0) return true;
else return false;
}
int main() {
cin>>code;
n = code.length();
for(int i=0;i<n;i++) {
indexes[int(code[i]-'a')].insert(i);
}
string seq = code;
if(!check_valid()) {
cout<<"-1\n";
}
else {
for(int i=0;i<n;i++) {
if(seq[i] != '(' && seq[i] != ')') {
int ind = int(code[i]-'a');
int st = i;
int last = *(indexes[ind].rbegin());
seq[st] = '(';
seq[last] = ')';
indexes[ind].erase(i);
indexes[ind].erase(last);
}
}
cout<<seq<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Incorrect |
2 ms |
360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |