1 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int n, nxt[maxn], f[maxn]; char s[maxn]; void cal_nxt() { nxt[1] = 0; for (int i = 2, j = 0; i <= n; i++) { while (j && s[j+1] != s[i]) j = nxt[j]; if (s[j+1] == s[i]) j++; nxt[i] = j; } } void cal_f() { for (int i = 2; i <= n; i++) { int j = nxt[i]; if (!j) f[i] = 0; else if (!nxt[j]) f[i] = j; else f[i] = f[j]; } } int main() { scanf("%d%s", &n, s+1); cal_nxt(); cal_f(); long long res = 0; for (int i = 1; i <= n; i++) if (f[i]) res += i - f[i]; printf("%lld\n", res); return 0; }
- 1
信息
- ID
- 11
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 3
- 已通过
- 3
- 上传者