QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#276049 | #5065. Beautiful String | rageOfThunder# | TL | 1ms | 3732kb | C++14 | 1.2kb | 2023-12-05 15:29:17 | 2023-12-05 15:29:18 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using __gnu_pbds::cc_hash_table;
using ll = long long;
const int mod = 1e9 + 9;
const int N = 5e3;
const int B = 131;
int n;
char s[N + 5];
int pw[N + 5], h[N + 5];
int f[N + 5][N + 5];
cc_hash_table<int, int> cnt;
int query(int l, int r) { return (h[r] + (ll)(mod - pw[r - l + 1]) * h[l - 1]) % mod; }
ll ans;
void solve() {
scanf("%s", s + 1), n = strlen(s + 1);
pw[0] = 1;
for (int i = 1; i <= n; ++i) pw[i] = (ll)pw[i - 1] * B % mod;
for (int i = 1; i <= n; ++i) h[i] = ((ll)h[i - 1] * B + s[i] - '0' + 1) % mod;
cnt.clear();
for (int i = n; i; --i) {
for (int j = i - 1; j; --j) f[j][i - 1] = cnt[query(j, i - 1)];
for (int j = i; j <= n; ++j) ++cnt[query(i, j)];
}
ans = 0;
for (int i = 1; i <= n; ++i) {
int cur = 0;
for (int len = 1; i + len - 1 <= n; ++len) {
ans += (ll)cur * f[i][i + len - 1];
if (len <= i - 1 && query(i - len, i - 1) == query(i, i + len - 1)) ++cur;
}
}
printf("%lld\n", ans);
}
int T;
int main() {
for (scanf("%d", &T); T; --T) solve();
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3732kb
input:
2 114514 0000000
output:
1 3
result:
ok 2 number(s): "1 3"
Test #2:
score: -100
Time Limit Exceeded
input:
11 79380 2483905227 37902399703986576270 39991723655814713848046032694137883815354365954917 5541883522667841718787744915558298207485830622522715211018760095628594390563630840464643840093757297 56530485554219245862513973750218715585679416120445975390556326891488719311495909340757506478400624741858999...