QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#242934 | #793. Palindromic Partitions | Camillus# | 60 | 8ms | 5908kb | C++17 | 2.8kb | 2023-11-07 18:53:25 | 2024-07-04 02:23:06 |
Judging History
answer
#include "bits/stdc++.h"
using ll = long long;
using namespace std;
struct node {
int go[26];
int suff = -1;
int len = 0;
node() {
fill(go, go + 26, -1);
}
};
vector<node> tree;
int new_node() {
tree.emplace_back();
return (int)tree.size() - 1;
}
int add(int a, int x) {
int b = new_node();
tree[b].suff = 0;
tree[b].len = tree[a].len + 1;
for (; a != -1 && tree[a].go[x] == -1; a = tree[a].suff) {
tree[a].go[x] = b;
}
if (a != -1) {
int c = tree[a].go[x];
if (tree[c].len == tree[a].len + 1) {
tree[b].suff = c;
} else {
int d = new_node();
tree[d].suff = tree[c].suff;
for (int i = 0; i < 26; i++) {
tree[d].go[i] = tree[c].go[i];
}
tree[d].len = tree[a].len + 1;
tree[c].suff = d;
tree[b].suff = d;
for (; a != -1 && tree[a].go[x] == c; a = tree[a].suff) {
tree[a].go[x] = d;
}
}
}
return b;
}
void solve() {
tree.resize(0);
string s;
cin >> s;
int n = (int)s.size();
int x = new_node();
for (int i = 0; i < n; i++) {
x = add(x, s[i] - 'a');
}
s = "$" + s;
vector<int> suff(n + 1);
for (int i = 1; i <= n; i++) {
suff[i] = tree[suff[i - 1]].go[s[i] - 'a'];
}
vector<vector<int>> g(tree.size());
for (int u = 1; u < tree.size(); u++) {
g[tree[u].suff].push_back(u);
}
vector<int> tin(tree.size());
vector<int> tout(tree.size());
int timer = 0;
auto dfs = [&](auto &&dfs, int u) -> void {
tin[u] = timer++;
for (int v : g[u]) {
dfs(dfs, v);
}
tout[u] = timer++;
};
dfs(dfs, 0);
auto is_ancestor = [&](int u, int v) -> bool {
return tin[u] <= tin[v] && tout[v] <= tout[u];
};
int l = 1, r = n;
int ans = 0;
while (l <= r) {
bool ok = false;
int u = 0;
for (int k = 1; l + k - 1 < r - k + 1; k++) {
u = tree[u].go[s[l + k - 1] - 'a'];
if (is_ancestor(u, suff[r]) && is_ancestor(u, suff[l + k - 1])) {
l += k;
r -= k;
ans += 2;
ok = true;
break;
}
}
if (!ok) {
ans += 1;
break;
}
}
cout << ans << '\n';
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 15
Accepted
Test #1:
score: 15
Accepted
time: 1ms
memory: 3748kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaabaaaaaaaaaaaaaab aaaaaaaaaaaaabxaaaaaaaaaaaaab baaaaaaaaaaaaaabaaaaaaaaaaaaaa aabbaabbaabbaaaaaaaabbaabbaabb ababababababababababababababab abcabcabcabcabcabcabcabcabcabc abcabcabcabcabccbacbacbacbacba qntshryhcojkqnlmqeob...
output:
30 29 2 3 2 12 15 10 30 3
result:
ok 10 lines
Test #2:
score: 15
Accepted
time: 1ms
memory: 3724kb
input:
10 aaabbbbaabbbbbbbaaaaabbb aaaababbbaabbabbbaaaaababbb aaabbbbaaaaaaaabbbba aaaaaaabbbbbbbaaaaaaabbbbbbb babbababbabbababbabab abbabaabbaababba bonobo palindrome deemed level
output:
10 5 10 2 17 16 3 1 5 5
result:
ok 10 lines
Test #3:
score: 15
Accepted
time: 0ms
memory: 3484kb
input:
5 abba aabaab x aa ab
output:
4 2 1 2 1
result:
ok 5 lines
Test #4:
score: 15
Accepted
time: 0ms
memory: 3496kb
input:
10 aabbaabbaabbaaaaaaaabbaabbaabb aabbaabbaabbaaaaabbaabbaabb aabbaabbaabbaaaaabbaabbaabb aabbaabbaabbaaaaaaaabbaabbaabb aabbaabbaabbaaaaaaaabbaabbaabb aabbaabbaabbaaaaabbaabbaabb aabbaabbaabbaaaaaaaabbaabbaabb aabbaabbaabbaaaaaabbaabbaabb aabbaabbaabbaaaaaaabbaabbaabb aabbaabbaabbaaaaaaabbaabbaabb
output:
12 9 9 12 12 9 12 10 11 11
result:
ok 10 lines
Test #5:
score: 15
Accepted
time: 0ms
memory: 3584kb
input:
10 abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba abbabaabbaababba
output:
16 16 16 16 16 16 16 16 16 16
result:
ok 10 lines
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 20
Accepted
time: 1ms
memory: 3588kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
300 299 2 3 2 30 150 100 300 11
result:
ok 10 lines
Test #7:
score: 20
Accepted
time: 1ms
memory: 3576kb
input:
6 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbabbbbababaaabbababbbaabbabbabbaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaababbbababbababbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbabbbbababaaabbababbbaabbabbabbaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
3 9 7 2 177 256
result:
ok 6 lines
Test #8:
score: 20
Accepted
time: 0ms
memory: 3632kb
input:
10 aaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbbaaaaaaaabbbbbbbb aaaaaaaab...
output:
47 32 31 33 22 46 46 24 34 46
result:
ok 10 lines
Test #9:
score: 20
Accepted
time: 1ms
memory: 3808kb
input:
10 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababba abbabaabbaababbabaababbaabbabaabbaababba...
output:
256 256 256 256 256 256 256 256 256 256
result:
ok 10 lines
Subtask #3:
score: 25
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #10:
score: 25
Accepted
time: 4ms
memory: 5892kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
10000 9999 2 3 2 100 5000 3333 9996 495
result:
ok 10 lines
Test #11:
score: 25
Accepted
time: 6ms
memory: 5908kb
input:
6 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
3 9 18 2 5169 4096
result:
ok 6 lines
Test #12:
score: 25
Accepted
time: 4ms
memory: 5324kb
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaa...
output:
258 282 116 109 287 192 167 251 148 146
result:
ok 10 lines
Test #13:
score: 25
Accepted
time: 8ms
memory: 5192kb
input:
10 abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabb...
output:
4096 4096 4096 4096 4096 4096 4096 4096 4096 4096
result:
ok 10 lines
Subtask #4:
score: 0
Memory Limit Exceeded
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #14:
score: 0
Memory Limit Exceeded
input:
10 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1000000 999999 2 3 2 1000 500000 333333 999996 48937