Power Strings--KMP

本文详细解析了KMP算法的原理及Next数组的应用,并通过一个具体问题展示了如何利用KMP算法来解决字符串周期性的判断问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://cn.vjudge.net/problem/POJ-2406

上面是比赛链接。

题目意思很明确,问最多是多少个子串连接而成的?

这个需要用到KMP,很好的理解KMP的Next数组。Next数组里面存了当匹配失败时应该跳到的位置,所以,最后一个数的部分匹配值 ,判断 n%(n-Next[n])为0 就可以。

Next[len]代表某位字符的部分匹配值,根据部分匹配值可以得到周期为:长度-Next[n]。在最后判断能否被长度整除即可.


#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>

using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)

int Next[MAXSIZE];
char str[MAXSIZE];
void getNext(int n)
{
    int  i=0,j=-1;
    Next[0]=-1;
    while(i<n)
    {
        if(j==-1||str[i]==str[j])
        {
            j++,i++;
            Next[i]=j;
        }
        else j=Next[j];
    }
}


int main(int argc,char *argv[])
{
    while(scanf("%s",str)!=EOF)
    {
        if(str[0]=='.') break;
        int len=strlen(str);
        //printf("len=%d\n",len);
        getNext(len);
//        for(int i=0;i<=len;i++)
//        {
//            printf("%d ",Next[i]);
//        }
//        printf("\n");
        if(len%(len-Next[len])==0) printf("%d\n",len/(len-Next[len]));
        else printf("1\n");
    }
    return 0;
}

/**************************************************/
/**             Copyright Notice                 **/
/**  writer: wurong                              **/
/**  school: nyist                               **/
/**  blog  : http://blog.youkuaiyun.com/wr_technology  **/
/**************************************************/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值