QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#460293 | #8475. Palindrome Strings | propane | AC ✓ | 191ms | 228196kb | C++20 | 4.7kb | 2024-07-01 13:19:01 | 2024-07-01 13:19:01 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cassert>
using namespace std;
using LL = long long;
// rad[i]表示以i为回文中心的最长回文串的长度
// rad[i](i 为偶数) 奇数长度回文串中心 i / 2 + 1 (1 based)
// rad[i](i 为奇数) 偶数回文串中心 i / 2 + 1 后面的空隙 (1 based)
// 判断[l, r] (1 based) 是否是回文串 : rad[l + r - 2] >= r - l + 1;
template<typename S>
vector<int> manacher(S s, bool calc_even = true) {
if (calc_even) {
int n = (int) s.size();
assert(n > 0);
s.resize(2 * n - 1);
for(int i = n - 1; i >= 0; i--) {
s[2 * i] = s[i];
}
auto d = *min_element(begin(s), end(s));
for(int i = 0; i < n - 1; i++) {
s[2 * i + 1] = d;
}
}
int n = (int) s.size();
vector<int> rad(n);
{
int i = 0, j = 0;
while(i < n) {
while(i - j >= 0 && i + j < n && s[i - j] == s[i + j]) ++j;
rad[i] = j;
int k = 1;
while(i - k >= 0 && i + k < n && k + rad[i - k] < j) {
rad[i + k] = rad[i - k];
++k;
}
i += k, j -= k;
}
}
if (calc_even) {
for(int i = 0; i < n; i++) {
if(((i ^ rad[i]) & 1) == 0) rad[i]--;
}
}
else {
for(auto &&x: rad) x = 2 * x - 1;
}
return rad;
}
const int maxn = 2e6 + 5;
struct SuffixAutomaton{
struct Node{
int len, fa;
int ch[26];
}node[maxn];
int tot = 1, last = 1;
LL f1[maxn], f2[maxn];
int c[maxn], id[maxn];
SuffixAutomaton(){
clear();
}
int new_node(){
++tot;
node[tot].len = node[tot].fa = 0;
memset(node[tot].ch, 0, sizeof node[tot].ch);
f1[tot] = f2[tot] = 0;
return tot;
}
void clear(){
tot = 0;
last = new_node();
}
void extend(int c){
int p = last, np = last = new_node();
node[np].len = node[p].len + 1;
for(; p && !node[p].ch[c]; p = node[p].fa) node[p].ch[c] = np;
if (!p) node[np].fa = 1;
else{
int q = node[p].ch[c];
if (node[q].len == node[p].len + 1) node[np].fa = q;
else{
int nq = new_node();
node[nq] = node[q], node[nq].len = node[p].len + 1;
node[q].fa = node[np].fa = nq;
for(; p && node[p].ch[c] == q; p = node[p].fa) node[p].ch[c] = nq;
}
}
// f[last] += 1;
}
void topsort(){
for(int i = 1; i <= tot; i++) c[i] = 0;
for(int i = 1; i <= tot; i++) c[node[i].len]++;
for(int i = 1; i <= tot; i++) c[i] += c[i - 1];
for(int i = tot; i >= 1; i--) id[c[node[i].len]--] = i;
for(int i = tot; i >= 2; i--){
f1[node[id[i]].fa] += f1[id[i]];
f2[node[id[i]].fa] += f2[id[i]];
}
}
// clear & build
void build(const string &s){
clear();
for(auto c : s) extend(c - 'a');
}
}sam;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
string s;
cin >> s;
auto rad = manacher(s);
vector<int> d(n + 1);
for(int i = 0; i < rad.size(); i += 2){
int pos = i / 2 + 1;
d[pos] += 1;
int r = (rad[i] + 1) / 2;
if (pos + r <= n) d[pos + r] -= 1;
}
for(int i = 1; i < rad.size(); i += 2){
int pos = i / 2 + 1;
d[pos + 1] += 1;
int r = (rad[i] + 1) / 2;
if (pos + 1 + r <= n) d[pos + 1 + r] -= 1;
}
for(int i = 1; i <= n; i++){
d[i] += d[i - 1];
}
s = " " + s;
for(int i = n; i >= 1; i--){
sam.extend(s[i] - 'a');
sam.f1[sam.last] += 1;
sam.f2[sam.last] += d[i - 1];
}
sam.topsort();
while(m--){
string t;
cin >> t;
const int s = t.size();
LL ans = 0;
auto rad = manacher(t);
t = " " + t;
auto check = [&](int l, int r){
if (l > r) return true;
return rad[l + r - 2] >= r - l + 1;
};
int p = 1;
for(int i = 1; i <= s; i++){
p = sam.node[p].ch[t[i] - 'a'];
if (p == 0) break;
if (check(i + 1, s)){
ans += sam.f1[p];
}
}
if (p != 0) ans += sam.f2[p];
cout << ans << '\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3500kb
input:
8 3 icpccamp p c pc
output:
4 7 4
result:
ok 3 number(s): "4 7 4"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
10 3 bbbabbbbbb baaaa abb bb
output:
10 4 31
result:
ok 3 number(s): "10 4 31"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
10 4 baababaaba aaaaa a ab aa
output:
8 18 17 11
result:
ok 4 number(s): "8 18 17 11"
Test #4:
score: 0
Accepted
time: 0ms
memory: 5332kb
input:
10000 1000 aabababbbaaabbbabbbbbbbbbaaabaabbbabbaababbbaaabbaabbbbbabababbbbbbbabbabaabaababbabaaababbbbaaabaababaaaaabbbbbbabbbababaababbbbabbbbabbabbbbbbabbaababbbbbbabaabababbbaabaaabbbbaaaabaaababbaaabbbbabababbaaabababababaababaabbaaabbaabababbbaabbbabaabbbbbbbbaabbaaabbbaabbaabbbabaabababbbbbb...
output:
300 18 197 198 181 21 6 5 21440 9 283 23680 8422 56163 35120 56163 35120 56163 35120 35120 56163 56163 56163 56163 56163 56163 56163 35120 35120 56163 56163 35120 56163 35120 35120 35120 35120 35120 56163 35120 35120 35120 56163 56163 35120 56163 35120 56163 35120 35120 56163 56163 56163 56163 35120...
result:
ok 1000 numbers
Test #5:
score: 0
Accepted
time: 0ms
memory: 7140kb
input:
20000 2000 bbbaaaaaababbbaababbaaaababaaabbaabbbbaaabbbaaababaaaaabbbbbbbaaaaabaaabbabababbbbbbbbaabaabbbbabaaabbbababaaabbabaabbbabbbbaaaababaabbbabbaaaababababbbbaabaababbbbbbbbabbaaaaaaaaaabbbbbaaaaaabbababbbaabbaabaaaabbabababaaabaabbabaabaaaabbbbbababaababbbbbababaaabaabbbbaaabaaababaabaaabbbab...
output:
1212 6 834 6 114 62 62 2309 89183 0 100016 90112 169223 169657 169657 169657 169223 169657 169223 169223 169657 169223 169223 169657 169657 169657 169657 169223 169223 169657 169657 169657 169223 169657 169657 169223 169657 169223 169657 169657 169657 169657 169223 169223 169657 169223 169657 169657...
result:
ok 2000 numbers
Test #6:
score: 0
Accepted
time: 3ms
memory: 9016kb
input:
30000 3000 aaaabbbaaaabaaaabaabaaaaaabaabbabaaaaaaaabbaabbaaaabbbbbababbbbbaabbabbabbbaaaababaaaabbbababaaabaabbbaabaababaabbababaaabbaaababbbaaaaaababaabbbaaabbabbbbaababbababaaaaaaaabbbaaaaabbabbbabbabbbaabbaabaaaaabbbbaabbbabbaabaabbaaaaaaaabbabbbbabaababbbabaaaabbbbbbbaabbbaabaaaaababbbbaabaabaa...
output:
90 10172 41 272796 99 20374 47982 64339 3142 198 859 167521 48 93 30100 399 93 20190 64339 3146 185181 2841 48 211 30004 859 11 46357 768 64339 210045 5076 3792 162 63 203 9 34220 137 3 399 2709 289073 4308 26270 289073 209 64339 64339 10520 38330 2755 10925 97 2748 26138 3320 3228 3154 3825 43 1725...
result:
ok 3000 numbers
Test #7:
score: 0
Accepted
time: 7ms
memory: 13584kb
input:
60000 6000 abaaabbbaaabbbbaabbbbbaaabaaaaabaaabaaaababaabbbbabbbaaabababbbababaababbbbaaabbaaaaaaabaaaabaaaaaaababaababaabbaaababaababaaabbbabbabaaaabbaabbbababbbbaabababbbbaabababaaababbbababaabbaaaaaabbbbbbbbabaaaabbbbaabaabaaabaababbaabbaabaabbbbbabbabbbbabbabbbbaaabbbbaaabbbababaabbabbbaaaabbbaa...
output:
306 296 289 289 247 6 459 373 14834 2363 443 14565 14220 4181 2130 0 0 1081983 1052603 84292 111982 111982 1081983 1081983 111982 1081983 1081983 1081983 111982 1081983 1081983 1081983 1081983 1081983 1081983 1081983 1081983 111982 111982 1081983 111982 1081983 1081983 111982 1081983 1081983 1081983...
result:
ok 6000 numbers
Test #8:
score: 0
Accepted
time: 16ms
memory: 26688kb
input:
100000 10000 babbabbbbabbbbbbaabaaabbabbabbaaabaaaaaabaabbbbbbababbaaaaabbabbbababaaaaaabbababbaababbabbbbabaabbaabbbabbbbbbabbaaabbaaaaabbabaababbabaaabaabbbabbaaabaaabababbbbbaabaabbbbabbaabbbbbaabaaabbbbbbbbabbbbbbaabaabbbbaaaabaabbbbbbbbbbaabbabaabbaabbaaaaaabbbaaabbabbbaaabababbaabaabaaaababaaa...
output:
5 32 37 318 8 481 226276 478914 50487 717062 490267 490267 490267 490267 490267 490267 490267 717062 717062 717062 490267 717062 717062 717062 717062 717062 490267 717062 490267 717062 490267 490267 490267 490267 490267 717062 490267 717062 717062 717062 490267 717062 717062 717062 717062 490267 490...
result:
ok 10000 numbers
Test #9:
score: 0
Accepted
time: 23ms
memory: 46104kb
input:
200000 20000 bbaababaabaababbaaaaabbabaabbbbbbaaaaabbbbaabbaabbbaaaaaaabbabbbababbbaabababbbbabbabbbabbaabaabbaaaabaaabaabbaaaaabbaaaabaaababababbabbaababbbababaababbaabaabaaaabaaabbabaaaabababbabaaaabbabbbbaaabbabaaabbbbbbabababbababbaaabbbabbaaaaaaaaababaaabbaaaaabaabbaaaaaaabbaaabaabbbbbbbbbbbabb...
output:
238781 1211940 8405 849154 259585 17350 177472 50750 93220 55963 328912 242379 449236 1915 34985 91319 18087 45363 849154 640 123841 9778 3158 82043 849154 120751 128 328912 53022 53022 45108 849154 744 60541 319083 379704 52988 877813 118341 379704 228832 228832 39886 2377 386954 126842 5273 8444 1...
result:
ok 20000 numbers
Test #10:
score: 0
Accepted
time: 111ms
memory: 116576kb
input:
500000 50000 aabbbbaabbaabaaabbbbaaaaaaabaabaaaabbbbbabbbbbabaabbbabaaabaaaababaaaababbabbbbbaabbababbbbbbaaaabaaaaabbabaaabababbbbbbababbbabaaabbaaaabaababbbaaaabbabaabaaabaabbbaabbabbabbaaaabbbbababbabbbaaaaabaaaaaaabaaaabaaabbaabaaaabaabaabbbaabbaaabbbabbabbaaababbabbbbbbbaaaabaabbbabababaabaabab...
output:
253 42 200 3720 20 5 13205 3578 3506 84 49 4592 35 5614 35 84528 85656 89730 75979 9884 806454 8394154 1720728 1720728 1720728 1720728 8394154 8394154 1720728 1720728 1720728 1720728 1720728 8394154 8394154 1720728 1720728 8394154 8394154 1720728 1720728 8394154 8394154 8394154 1720728 8394154 83941...
result:
ok 50000 numbers
Test #11:
score: 0
Accepted
time: 73ms
memory: 148668kb
input:
1000000 50000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 5...
result:
ok 50000 numbers
Test #12:
score: 0
Accepted
time: 101ms
memory: 213244kb
input:
1000000 50000 aabbabababbabaabababaabaabbbbbabaabaabbbbbbaababaababaabbabbaabaaabaabaaabbaaababaabbbaaaaababbaaaaaabaaaaabbaabbbaaababaabbaabbaaaaaababababbbaababaaabbaaaabbbaabbbbbaaaabababaaabbabbabaaababaabbaaabbaabaababaabaaabbabbabababaababbbbabbaabbbabbababbbbbbbaaaaabaababababbaababbbbaabbaab...
output:
453582 3691 29106 114479 1795 131831239 275852 16751 58921 41 14559 64836 7714 56334 339711 131996707 3434 597893 2021 13999 3394 28 29474 2297 223 15958 1012337 56 57070 308 656760 58595 1934 1040797 0 911007 312293 939 3782 0 6224 132522558 1469 58985 16094 68026 76048 341200 70 680572 7141 132522...
result:
ok 50000 numbers
Test #13:
score: 0
Accepted
time: 191ms
memory: 170448kb
input:
1000000 50000 gpkkgebfroojkwdrldwydyslbndoqzfnfpsntbmucbptxqpmtabzhnvbkrpdlbntjqxqaqanrgcrvfjtjffemhwxjsqaysfrwkksutrgfrzhyvrvzhadcerhulyqticyyolrcavmodmbwfiiztrdneqteahjawvvtcrhyfethwkfrdmcjfudmnnydmvtjsiwlbtjjscluokjmejrjtsvfftlkmgurrincjptopmiqxmfdmwvplbczazwniykbayyawwilcetzmornbjwputymqfkxjzjfj...
output:
3 35 627583 102598 4 3 773730 246180 410408 197004 201623 229772 3 425353 197004 427227 3 86214 29 77295 415011 4 3 3 3 4 143558 14230 62775 604 229772 93706 65530 3 3 32 228349 4 180620 754960 77295 235128 77295 410392 3 93705 41878 229772 278357 27959 135383 3 3 3 81914 3 377624 4 197004 197005 77...
result:
ok 50000 numbers
Test #14:
score: 0
Accepted
time: 79ms
memory: 152328kb
input:
1000000 80000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 5...
result:
ok 80000 numbers
Test #15:
score: 0
Accepted
time: 161ms
memory: 193132kb
input:
1000000 80000 bbbaaababbabbbbaabbaaababaabaababbaaaaabbabaaabbbbbaaaaababbaabbabbbbaaaaaaababbbbabaabbaababababbabaabbaabbabbaaabaaaabaaaabaabaaaabbbaaaababababababbabbaababbbaaabbababbaaaaabaaabbaaabbabaababababaabaabaababbbbababbabbaaabbbbbabababbabbbbaababbabbaaaaaaaaababaaabaaaababaabbaaaaababba...
output:
5 6187 1631 3770 306 54 2805 2240 10196 2400 6002 1498910 1517405 36 12228 1622220 1535229 1632153 12314 12865460 867575 57710 4042522 301786 12865460 15927056 138406043 15927056 138406043 15927056 15927056 138406043 15927056 138406043 15927056 138406043 15927056 138406043 138406043 15927056 1592705...
result:
ok 80000 numbers
Test #16:
score: 0
Accepted
time: 119ms
memory: 161836kb
input:
1000000 80000 oidqopacrekstvsdtehxmnftwdzkiadmtozoqosxslggaxllsvxtvvifeolzpxkmvczffvqhesfsfvhrjnwfgssgbxrhrosfyihzueyyajlkzbetsmalnjzvfcniatydwwxyvtstohtllqglhoblranwxygzgocghqzitbvmtwjoroztohhcdqkqjlmkpemobklvwtampfufwjfxxxmvzjheckiaxqqpewztkpntgemnvrigycqwrhgifbwntolgwgzexsdvjasxgwsplofvvccdqvhblh...
output:
3 1076 1021 1238 1275 3 8960 1168 130414 1072 4800 48601 204362 10457 1025979 1025979 337 1808318 14435961 14435961 14435961 414518 14435961 414518 14435961 3647240 24538 14435961 414518 3647240 14435961 31741 1808318 14435961 14435961 3647240 14435961 14435961 14435961 14435961 14435961 14435961 14...
result:
ok 80000 numbers
Test #17:
score: 0
Accepted
time: 96ms
memory: 148776kb
input:
1000000 100000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 500000500000 5...
result:
ok 100000 numbers
Test #18:
score: 0
Accepted
time: 155ms
memory: 228196kb
input:
1000000 100000 baababbbbbabaabaaaaaaaaaaababbaaabbabbaabbbbbbabbaabaaaabaababababbbaabaabaabbaabaabbbbaaaaaabababaababbabbbbabbbaaabbaabbbabbaaabbbabaabbaabababbaaabbbababababbaaababbbbaaaabaabababbabababbaabaabbbabababbabaabbbaaabaabbbaaabbbabbaaabaaabaaabaaabaabbabababbabbbaaababbababaababaababaaa...
output:
528492 4081 1000552 3830 4844288 754126 3066616 130136 317839 1120765 19773 418846 1000561 50560 1076075 190499 2316299 57518 183185 18314 1920 14804 284548 496 683767 424224 1597916 468144 1066244 1052028 1000000 14573 561427 884989 3648 48198 52708 424224 5635904 505383 1120765 106290 596899 14771...
result:
ok 100000 numbers
Test #19:
score: 0
Accepted
time: 150ms
memory: 162916kb
input:
1000000 100000 zmxzthulqsoskialeouxnnwclmqkpjdzydjphfwyxazjanaigofbikqdwuepmsjdinnafffdwknmiohwisluowkkdjgywobvhegdpxdizryoovlbasghqcwpzmnhhrnxzkrcckpobarlegivwraueghjsrektnhtwlkewutlbxqxkxgswefylnruevdcaknzijvclriyxnoqukjourmvsobpcynccqthvhicacgtctzkgzhxmepjrjtossxctxjpljsvbzexvfnjliuntxnqxvuuhrzwz...
output:
84369 272920 496688 3 22 562224 364659 4 3 48931 140556 100998 496688 256536 399585 3 4 21 496688 132364 589721 54466 3 563781 346909 68230 3 3 72326 3 115980 3 305688 12908 137862 463920 51019 3 3 3 256536 500838 24 272920 3 3 322092 562224 3 463920 240152 3 321036 961084 155270 496688 594992 14548...
result:
ok 100000 numbers