Luogu P4391 [BOI2009]Radio Transmission 无线传输


Luogu P4391 [BOI2009]Radio Transmission 无线传输

解析

  • KMP找最大循环节的长度
  • ans = n - next[n]

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
int l,p[1000005];
char s[1000005];
void kmp()
{
    p[1]=0;
    int j=0;
    for(int i=1;i<l;i++)
    {
        while(j>0&&s[i+1]!=s[j+1]) j=p[j];
        if(s[i+1]==s[j+1]) j++;
        p[i+1]=j;
    }
    return;
}
int main()
{
    scanf("%d",&l);
    cin>>(s+1);
    kmp();
    printf("%d\n",l-p[l]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Hawking-llfz/p/11508962.html