求教为什么这样错了

P3435 [POI2006] OKR-Periods of Words

iao113 @ 2017-12-05 19:36:56

我觉得和题解是一个意思啊

‘’‘cpp

#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn = 1000010;
using namespace std;
int k;
long long ans;
char s[maxn];
long long fail[maxn];
void getNext();
int main()
{
    scanf("%d%s", &k, s);
    getNext();
    ans = 0;
    for (int i = 1; i <= k; i++)
    {
        long long j = i / (i - fail[i]) * (i - fail[i]);
        if (i % (i - fail[i]) == 0)
            j = j - i + fail[i];
        ans += j;
    }
    printf("%lld\n", ans);
    return 0;
}
void getNext()
{
    fail[0] = fail[1] = 0;
    for (int i = 1; i < k; i++)
    {
        long long j = fail[i];
        while (j && s[i] != s[j])
            j = fail[j];
        fail[i + 1] = (s[i] == s[j] ? j + 1 : 0);
    }
}
’‘’

|