# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
612469 |
2022-07-29T15:36:10 Z |
drdilyor |
Match (CEOI16_match) |
C++17 |
|
1 ms |
340 KB |
#include <bits/stdc++.h>
#ifdef ONPC
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
#define sz(a) ((int)(a).size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll INFL = 1e18;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.first >> p.second; }
const int N = 2e5, LOGN = 17, MOD = 1e9+7;
string solve(const string& s) {
int n = s.size();
string res(n, '?');
vector<array<int, 26>> count(n);
for (int i = 0; i < n; i++) {
if (i) count[i] = count[i-1];
count[i][s[i]-'a']++;
}
auto valid = [&](int i, int j) {
for (int k = 0; k < count[i].size(); k++) {
if ((count[j][k] - count[i-1][k]) % 2)
return false;
}
int depth = 0;
for (; i <= j; i++) {
if (res[i] =='(') depth++;
if (res[i] == ')') depth--;
if (depth < 0) return false;
}
return depth == 0;
};
for (int i = 0; i < n; i++) {
if (res[i] != '?') continue;
int depth = 0;
for (int j = n-1; j > i; j--) {
if (s[i] != s[j]) continue;
if (res[j] != '?') continue;
if (valid(i+1, j-1)) {
res[i] = '(';
res[j] = ')';
break;
}
}
if (res[i] == '?') {
return "-1";
}
}
return res;
}
string solve_2(const string& s) {
int n = s.size();
string res(n, '?');
function<bool(int)> brute = [&](int i) {
if (i < n) {
res[i] = '(';
if (brute(i+1)) return true;
res[i] = ')';
return brute(i+1);
} else {
vector<int> matching(n, -1);
vector<int> cur;
for (int i = 0; i < n; i++) {
if (res[i] == '(') {
cur.push_back(i);
} else {
if (cur.empty()) return false;
matching[i] = cur.back();
matching[cur.back()] = i;
cur.pop_back();
}
}
if (!cur.empty()) return false;
for (int i = 0; i < n; i++) {
if (s[i] != s[matching[i]]) return false;
int depth = 0;
for (int j = i+1; j < matching[i]; j++) {
if (res[j] == '(') depth++;
else depth--;
if (depth < 0) return false;
}
if (depth != 0) return false;
}
return true;
}
};
return brute(0) ? res : "-1";
}
signed main() {
int t = 1;
#ifdef ONPC
cin >> t;
#endif
while (t-- && cin) {
string s; cin >> s;
if (!cin) break;
#ifdef ONPC
cout << "Case " << s << '\n';
string res1 = solve(s), res2 = solve_2(s);
cout << res1 << endl;
if (res1 != res2) {
cout << res2 << endl;
cout << "Case " << s << '\n';
assert(false);
}
#else
cout << solve(s);
#endif
}
return 0;
}
/*
█████ █████ ███ ████
▒▒███ ▒▒███ ▒▒▒ ▒▒███
███████ ████████ ███████ ████ ▒███ █████ ████ ██████ ████████
███▒▒███ ▒▒███▒▒███ ███▒▒███ ▒▒███ ▒███ ▒▒███ ▒███ ███▒▒███▒▒███▒▒███
▒███ ▒███ ▒███ ▒▒▒ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒▒▒
▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███ ▒███
▒▒████████ █████ ▒▒████████ █████ █████ ▒▒███████ ▒▒██████ █████
▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒███ ▒▒▒▒▒▒ ▒▒▒▒▒
███ ▒███
▒▒██████
▒▒▒▒▒▒
*/
Compilation message
match.cpp: In lambda function:
match.cpp:27:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::array<int, 26>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int k = 0; k < count[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~
match.cpp: In function 'std::string solve(const string&)':
match.cpp:42:13: warning: unused variable 'depth' [-Wunused-variable]
42 | int depth = 0;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |