P3435 [POI2006]OKR-Periods of Words [Kmp, next数组]

OKRPeriods of WordsOKR-Periods of Words

题目描述见链接 .


color{red}{正解部分}

题目转化 为: 对每个前缀串, 求出其 最短公共前缀后缀, 答案即为 总长度 - 公共长度 .

自然而然地想到 KmpKmpnext[]next[] 数组, 但是 KmpKmp 中的 next[]next[] 数组 表示的是 最长公共前缀后缀, 考虑怎么转化,

不断执行 next[i]=next[next[i]]next[i] = next[next[i]], 直到 next[i]next[i] 将要等于 00, 这时 next[i]next[i] 就是 最短公共前缀后缀 了 .


color{red}{实现部分}

#include<bits/stdc++.h>
#define reg register
typedef long long ll;

const int maxn = 1e6 + 5;

int N;
int nxt[maxn];

char S[maxn];

void Get_next(){
        nxt[0] = -1;
        int i = 0, t = -1;
        while(i < N){
                if(t == -1 || S[i] == S[t]) nxt[++ i] = ++ t;
                else t = nxt[t];
        }
}

int main(){
        scanf("%d", &N); scanf("%s", S); Get_next();
        ll Ans = 0; nxt[0] = 0;
        for(reg int i = 1; i <= N; i ++){
                if(!nxt[i]) continue ;     
                while(nxt[nxt[i]]) nxt[i] = nxt[nxt[i]];
                if(nxt[i] < i) Ans += i - nxt[i];
        }
        printf("%lld
", Ans);
        return 0;
}
原文地址:https://www.cnblogs.com/zbr162/p/11822412.html