KMP

一、KMP:
时间复杂度O(N+M)。

next数组求法:
(1)初始化next(1)= j = 0,假设next(1----i-1)已经求出,下面求解next(i)。
(2)不断尝试扩展匹配长度j,如果扩展失败(下一个字符不相等),令 j 变为next(j),直至 j 为0(应该开始从头开始匹配)。
(3)如果能够扩展成,匹配长度 j 就增加1。next(i)的值就是 j。

f 数组与next数组的求解过程基本一致。

//A是否为B的子串
//获取A的next数组
//A与B进行匹配,求解 f 数组
nt[1]=0;
for(int i=2,j=0;i<=n;i++)
{
    while(j>0&&a[i]!=a[j+1]) j=nt[j];
    if(a[i]==a[j+1]) j++;
    nt[i]=j;
}

for(int i=1,j=0;i<=m;i++)
{
    while(j>0&&(j==n||b[i]!=a[j+1])) j=nt[j];
    if(b[i]==a[j+1]) j++;
    f[i]=j;
    if(f[i]==n) sum++;
}

一般来说上述代码已经够用,但是还有点小优化。

//失配时处理达到了最优
nt[1]=0;
for(int i=2,j=0;i<=n;i++)
{
    while(j>0&&a[i]!=a[j+1]) j=nt[j];
    if(a[i]==a[j+1]) j++;

    if(j==0||a[i+1]!=a[j+1])
        nt[i]=j;
    else nt[i]=nt[j];
}


   
   
二、例题:POJ - 1961 Period

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.
Output
For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4

   如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元。使K最大的字符串T称为S的最小循环元,此时K称最大循环次数。
   现在给定一个长度为N的字符串S,对S的每一个前缀S(1–i),如果它的最大循环次数大于1,则输出该前缀的最小循环元长度和最大循环次数。

   引理:S(1----i)具有长度为len < i 的循环元的充要条件是 len 能整除 i 并且S(len + 1----i) = S(1----i-len)

   根据引理:当 i -next(i)能整除 i 时,S(1----i-next(i))就是S(1----i)的最小循环元。它的最大循环次数就是 i / ( i-next(i))。其中 i - next(i) 能整除 i 的条件是为了保证循环元每次重复的完整性。

   进一步地,如果 i - next( next(i) )能整除 i ,那么 S(1----i - next( next(i) ))就是S(1----i)的次小循环元。以此类推可以找出s(1----i)的所有循环元。

   一个字符串的任意循环元的长度必然是最小循环元长度的倍数。

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;

const int maxn=1000010;
char a[maxn];
int nt[maxn];
int n,t;
int main(void)
{
    while(scanf("%d",&n),n)
    {
        scanf("%s",a+1);
        nt[1]=0;
        for(int i=2,j=0;i<=n;i++)
        {
            while(j>0&&a[i]!=a[j+1]) j=nt[j];
            if(a[i]==a[j+1]) j++;
            nt[i]=j;
        }

        printf("Test case #%d\n",++t);
        for(int i=2;i<=n;i++)
        {
            if(i%(i-nt[i])==0&&i/(i-nt[i])>1)
                printf("%d %d\n",i,i/(i-nt[i]));
        }
         putchar('\n');
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们这里说的KMP不是拿来放电影的(虽然我很喜欢这个软件),而是一种算法。KMP算法是拿来处理字符串匹配的。换句话说,给你两个字符串,你需要回答,B串是否是A串的子串(A串是否包含B串)。比如,字符串A="I'm matrix67",字符串B="matrix",我们就说B是A的子串。你可以委婉地问你的MM:“假如你要向你喜欢的人表白的话,我的名字是你的告白语中的子串吗?” 解决这类问题,通常我们的方法是枚举从A串的什么位置起开始与B匹配,然后验证是否匹配。假如A串长度为n,B串长度为m,那么这种方法的复杂度是O (mn)的。虽然很多时候复杂度达不到mn(验证时只看头一两个字母就发现不匹配了),但我们有许多“最坏情况”,比如,A= "aaaaaaaaaaaaaaaaaaaaaaaaaab",B="aaaaaaaab"。我们将介绍的是一种最坏情况下O(n)的算法(这里假设 m<=n),即传说中的KMP算法。 之所以叫做KMP,是因为这个算法是由Knuth、Morris、Pratt三个提出来的,取了这三个人的名字的头一个字母。这时,或许你突然明白了AVL 树为什么叫AVL,或者Bellman-Ford为什么中间是一杠不是一个点。有时一个东西有七八个人研究过,那怎么命名呢?通常这个东西干脆就不用人名字命名了,免得发生争议,比如“3x+1问题”。扯远了。 个人认为KMP是最没有必要讲的东西,因为这个东西网上能找到很多资料。但网上的讲法基本上都涉及到“移动(shift)”、“Next函数”等概念,这非常容易产生误解(至少一年半前我看这些资料学习KMP时就没搞清楚)。在这里,我换一种方法来解释KMP算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值