做法求 hack

P3435 [POI2006] OKR-Periods of Words

huangrenheluogu @ 2023-10-08 15:57:53

rt。

我是用 P4931 的思路,对于每一个 i,求出最短的周期(tem),之后延申到比 i 小的最大值(tem *= ((i % tem) ? (i / tem) : (i / tem - 1));),然后累加答案。


by huangrenheluogu @ 2023-10-08 15:58:17

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, kmp[N], j, tem;
string s;
long long ans;
int main(){
    scanf("%d", &n);
    cin >> s;
    s = ' ' + s;
    for(int i = 2; i <= n; i++){
        while(s[i] != s[j + 1] && j) j = kmp[j];
        j += (s[i] == s[j + 1]);
        kmp[i] = j;
    }
    for(int i = 1; i <= n; i++){
        if(kmp[i] == 0) continue ;
        tem = i - kmp[i];
        tem *= ((i % tem) ? (i / tem) : (i / tem - 1));
        ans += 1ll * tem;
//      printf("%d %d\n", i, tem);
    }
    printf("%lld", ans);
    return 0;
}

by lightovernight @ 2023-10-12 19:23:03

12 acdacfacdacf 答案应该为48,你这个是42


by huangrenheluogu @ 2023-10-17 20:37:47

@lightovernight 谢谢大神


|