# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
62219 | kingpig9 | Match (CEOI16_match) | C++11 | 2049 ms | 1200 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
int N;
string S;
string ans;
bool good (string seq) {
stack<int> stk;
for (int i = 0; i < seq.size(); i++) {
if (seq[i] == '(') {
stk.push(i);
} else {
if (stk.empty() || S[stk.top()] != S[i]) {
return false;
}
stk.pop();
}
}
for (int i = seq.size(); i < N; i++) {
//see if you can assign more stuff
if (!stk.empty() && S[stk.top()] == S[i]) {
stk.pop();
} else {
stk.push(i);
}
}
return stk.empty();
}
int main() {
if (fopen("match.in", "r")) {
freopen("match.in", "r", stdin);
freopen("match.out", "w", stdout);
}
cin >> S;
N = S.size();
if (!good("")) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < N; i++) {
ans += '(';
if (!good(ans)) {
ans.back() = ')';
}
}
cout << ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |