首页 > 其他 > 详细

BZOJ 1511: [POI2006]OKR-Periods of Words KMP

时间:2020-03-26 00:34:20      阅读:116      评论:0      收藏:0      [点我收藏+]

KMP 的 fail 指针指向的位置是具有循环节性质的. 

code:

#include <bits/stdc++.h>   
#define ll long long 
#define N 1000020 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std; 
int n,nxt[N];  
char a[N]; 
int main() 
{
    // setIO("input");              
    scanf("%d%s",&n,a+1);   
    ll cnt=0;   
    for(int i=2,j=0;i<=n;++i) 
    {
        for(;j&&a[j+1]!=a[i];)j=nxt[j]; 
        if(a[j+1]==a[i]) ++j;      
        nxt[i]=j;  
    }
    for(int i=2,j=2;i<=n;++i,j=i) 
    { 
        while(nxt[j]) j=nxt[j];   
        if(nxt[i]) nxt[i]=j;        
        cnt+=i-j;  
    }
    printf("%lld\n",cnt); 
    return 0;   
}

  

BZOJ 1511: [POI2006]OKR-Periods of Words KMP

原文:https://www.cnblogs.com/guangheli/p/12571450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!