이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vector<int>> vvi;
#define debug(x) cerr << (#x) << " " << (x) << endl;
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
int n;
string s;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = sz(s);
stack<char> st;
for(int i = 0; i < n; i++){
if(!st.empty() && st.top() == s[i]) st.pop();
else st.push(s[i]);
}
if(!st.empty()){
cout << "-1\n";
return 0;
}
string ans = "";
for(int i = 0; i < n; i++){
if(!st.empty() && st.top() == s[i]){
stack<char> t = st;
t.push(s[i]);
for(int j = i+1; j < n; j++){
if(!t.empty() && t.top() == s[j]) t.pop();
else t.push(s[j]);
}
if(t.empty()){
st.push(s[i]);
ans.push_back('(');
}
else{
st.pop();
ans.push_back(')');
}
}
else{
st.push(s[i]);
ans.push_back('(');
}
}
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |