#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)
int main() {
//freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
string S; cin >> S;
int N = S.length();
string T = "";
stack<char> stk;
FOR(i,0,N-1) if (stk.empty() || stk.top() != S[i]) stk.push(S[i]); else stk.pop();
if (!stk.empty()) { cout << -1 << '\n'; }
else {
FOR(i,0,N-1) {
int M = (int)T.length();
FOR(j,0,M-1) if (T[j] == '(') stk.push(S[j]); else stk.pop();
stk.push(S[i]);
//cout << i << " i stk " << stk.size() << " :: " << T << endl;
FOR(j,M+1,N-1) if (stk.empty() || stk.top() != S[j]) stk.push(S[j]); else stk.pop();
T += (stk.empty() ? '(' : ')');
while (!stk.empty()) stk.pop();
}
cout << T << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
376 KB |
Output is correct |
7 |
Correct |
15 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
4 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
376 KB |
Output is correct |
7 |
Correct |
15 ms |
376 KB |
Output is correct |
8 |
Correct |
237 ms |
528 KB |
Output is correct |
9 |
Correct |
258 ms |
504 KB |
Output is correct |
10 |
Correct |
185 ms |
504 KB |
Output is correct |
11 |
Correct |
164 ms |
440 KB |
Output is correct |
12 |
Execution timed out |
2053 ms |
504 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |