ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#468 | #90. 「NOIP2020」字符串匹配 | Pigsyy | 100 | 739ms | 8668kb | C++23 | 2.2kb | 2025-04-24 20:46:11 | 2025-04-24 20:46:12 |
answer
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#define ll long long
//#define int ll
using namespace std;
inline int read() {
int d = 0, f = 1;
char x = getchar();
while (x < '0' || x > '9') {
if (x == '-') {
f = -1;
}
x = getchar();
}
while (x >= '0' && x <= '9') {
d = (d << 1) + (d << 3) + (x - 48);
x = getchar();
}
return d * f;
}
int z[3000010], cnt1[50], cnt2[50], sum[50], n, cnt, now, res, tot, ret;
ll ans;
char s[3000010];
void Z_function(char *s, int len) {
z[0] = len - 1;
for (int i = 1, l = -1, r = -1; i < len; i++) {
if (i <= r)
z[i] = min(z[i - l], r - i + 1);
else
z[i] = 0;
while (i + z[i] < len && s[z[i]] == s[i + z[i]])
z[i]++;
if (i + z[i] > r)
l = i, r = i + z[i] - 1;
}
}
int T;
signed main() {
T = read();
while (T--) {
scanf("%s", s);
n = strlen(s);
memset(sum, 0, sizeof(sum));
memset(cnt1, 0, sizeof(cnt1));
memset(cnt2, 0, sizeof(cnt2));
ans = ret = cnt = 0;
for (int i = 0; i < n; i++)
cnt1[s[i] - 'a'] ^= 1;
for (int i = 0; i < 26; i++)
cnt += cnt1[i];
Z_function(s, n);
now = cnt, res = tot = 0;
for (int i = 1; i < n; i++) {
if (cnt1[s[i - 1] - 'a'])
tot -= sum[now--];
else
tot += sum[++now];
cnt1[s[i - 1] - 'a'] ^= 1;
int k = min(z[i], n - i - 1) / i + 1;
int c1 = k - (k >> 1), c2 = k >> 1;
ans += 1ll * tot * c1;
if (cnt2[s[i - 1] - 'a'])
res--;
else
res++;
cnt2[s[i - 1] - 'a'] ^= 1;
sum[res]++;
ans += 1ll * ret * c2;
if (res <= now)
tot++;
if (res <= cnt)
ret++;
}
printf("%lld\n", ans);
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 4
Accepted
time: 2ms
memory: 3468kb
input:
5 jjjjjjjjjj lllllllcyg mgmgmgmgmg nycqtnycqt tdutduwyga
output:
30 39 30 20 33
result:
ok 5 tokens
Test #2:
score: 4
Accepted
time: 0ms
memory: 3512kb
input:
5 hhhhhhhhhh iiiiiiibkw dgdgdgdgdg ffffffffyf ffzffzwsnw
output:
30 39 30 44 36
result:
ok 5 tokens
Test #3:
score: 4
Accepted
time: 1ms
memory: 3572kb
input:
5 rrrrrrrrrr iiiiiiiwzr dididididi wwwwzqcjvv mbymbyhgpc
output:
30 39 30 28 33
result:
ok 5 tokens
Test #4:
score: 4
Accepted
time: 2ms
memory: 3540kb
input:
5 cccccccccc lllllllket tututututu ccbuxccbux pnppnpnlwl
output:
30 39 30 26 32
result:
ok 5 tokens
Test #5:
score: 4
Accepted
time: 0ms
memory: 3456kb
input:
5 uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuubgmwdgmgwvqvkbcvih...
output:
6590 3585 2313 4359 4486
result:
ok 5 tokens
Test #6:
score: 4
Accepted
time: 1ms
memory: 3464kb
input:
5 llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllsuqtxhunfvotmxfqeb...
output:
6564 3560 2607 4517 4554
result:
ok 5 tokens
Test #7:
score: 4
Accepted
time: 2ms
memory: 3468kb
input:
5 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnkvpnzxdkdhjucliujh...
output:
6517 3560 2888 4965 4105
result:
ok 5 tokens
Test #8:
score: 4
Accepted
time: 2ms
memory: 3464kb
input:
5 ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppphytnmeyelaxegcuzq...
output:
6604 3560 2738 4804 4832
result:
ok 5 tokens
Test #9:
score: 4
Accepted
time: 2ms
memory: 3512kb
input:
5 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
output:
690923 325466 274377 538726 519032
result:
ok 5 tokens
Test #10:
score: 4
Accepted
time: 1ms
memory: 3516kb
input:
5 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
output:
688611 325466 278014 483768 516890
result:
ok 5 tokens
Test #11:
score: 4
Accepted
time: 2ms
memory: 3468kb
input:
5 rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr...
output:
690572 325466 282653 400319 517082
result:
ok 5 tokens
Test #12:
score: 4
Accepted
time: 3ms
memory: 3568kb
input:
5 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...
output:
689440 325466 278624 464740 501911
result:
ok 5 tokens
Test #13:
score: 4
Accepted
time: 4ms
memory: 3600kb
input:
5 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee...
output:
714173984 606072908 715934994 603558188 714547382
result:
ok 5 tokens
Test #14:
score: 4
Accepted
time: 10ms
memory: 3564kb
input:
5 vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv...
output:
713410981 603184777 713761452 714012718 603184777
result:
ok 5 tokens
Test #15:
score: 4
Accepted
time: 11ms
memory: 3528kb
input:
5 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee...
output:
3502305645 3504362490 3495279874 3496151003 2412446034
result:
ok 5 tokens
Test #16:
score: 4
Accepted
time: 18ms
memory: 3588kb
input:
5 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc...
output:
2419023179 3481792643 2416893029 3498851133 2414888013
result:
ok 5 tokens
Test #17:
score: 4
Accepted
time: 17ms
memory: 3584kb
input:
5 dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd...
output:
2414715183 2414053265 3498760311 2415418741 2412193453
result:
ok 5 tokens
Test #18:
score: 4
Accepted
time: 21ms
memory: 3668kb
input:
5 uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu...
output:
14044909130 13758605640 8021703699 4927122703 9489413350
result:
ok 5 tokens
Test #19:
score: 4
Accepted
time: 32ms
memory: 3672kb
input:
5 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
output:
14044820948 13781146461 7453935828 4673360128 9488558416
result:
ok 5 tokens
Test #20:
score: 4
Accepted
time: 20ms
memory: 3652kb
input:
5 rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr...
output:
14044830602 13805450160 7984918891 4873659736 9491430029
result:
ok 5 tokens
Test #21:
score: 4
Accepted
time: 19ms
memory: 5696kb
input:
5 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
14044823925 13789617860 7597752918 4978465106 9490927685
result:
ok 5 tokens
Test #22:
score: 4
Accepted
time: 154ms
memory: 8596kb
input:
5 ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
output:
903628883069 902178522904 467114641327 305284235317 620652587747
result:
ok 5 tokens
Test #23:
score: 4
Accepted
time: 138ms
memory: 8588kb
input:
5 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...
output:
903629704237 901055755987 504315438243 304698187610 620653531389
result:
ok 5 tokens
Test #24:
score: 4
Accepted
time: 141ms
memory: 8648kb
input:
5 cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc...
output:
904243010503 904253960505 904235088477 667900329073 904248230739
result:
ok 5 tokens
Test #25:
score: 4
Accepted
time: 136ms
memory: 8668kb
input:
5 hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...
output:
904261912252 904260123920 904263261760 667900331468 904241569561
result:
ok 5 tokens