poj 2406 Power Strings KMP匹配

对于数组s[0~n-1],计算next[0~n](多计算一位)。

考虑next[n],假设t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1。

这样考虑:

比如字符串"abababab",

a  b a b a b a b *

next     -1 0 1 2 3 4 5 6  7

考虑这样的模式匹配,将"abababab#"当做主串,"abababab*"当做模式串,于是进行匹配到n(n=8)时,出现了不匹配:

主串      abababab#

模式串   abababab*

于是模式串需要回溯到next[*]=7,这之前的主串和模式串对应相等,于是需要模式串向右滚动的位移是d=n-next[n]=2,即:

123456789

主串      abababab#

模式串       abababab*

于是可以看出,s[0~1]=s[3~4]=s[5~6]=s[7~8]。

所以位移d=n-next[n]可以看作是构成字符串s的字串(如果n%d==0,存在这样的构成),相应的重复次数也就是n/d。

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int len;
int next[1000005];
char s[1000005];
int kmp_next()
{
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j])
        {
            i++;j++;
            next[i]=j;

        }
        else
            j=next[j];
    }
    int x=i-next[i];//碰到不匹配的向前滚动的位移
    if(len%x==0)
        return x;
    else
        return len;
}

int main()
{

    while(scanf("%s",s)!=EOF)
    {
        if(s[0]=='.')
            break;
        len=strlen(s);
        int ans=kmp_next();
        ans=len/ans;
        printf("%d\n",ans);
    }
    return 0;
}

poj 2406 Power Strings KMP匹配,布布扣,bubuko.com

时间: 2024-08-02 11:03:36

poj 2406 Power Strings KMP匹配的相关文章

poj 2406 Power Strings(KMP&amp;思维)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31093   Accepted: 12974 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ 2406 Power Strings KMP运用题解

本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都觉得是可以利用next数组的,但是自己想了很久没能这么简洁地总结出来,也只能查查他人代码才恍然大悟,原来可以这么简单地区求一个周期字符串的最小周期的. 有某些大牛建议说不应该参考代码或者解题报告,但是这些大牛却没有给出更加有效的学习方法,比如不懂KMP,难倒不应该去看?要自己想出KMP来吗?我看不太可能有哪位大牛可以直接自己"重新创造出KMP"来吧. 好吧,不说"创造KMP

poj 2406 Power Strings(kmp的nxt数组找最小循环节)

题目链接:poj 2406 Power Strings 题意: 给你一个字符串,让你找出这个字符串的最大循环次数,及最小循环节. 题解: 用kmp的nxt数组搞搞,L=j-nxt[j],为前缀j的最小循环节. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define F(i,a,b) for(int i=(a);i<=(b);++i) 5 using namespace std;

POJ - 2406 Power Strings (KMP循环节)

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

poj 2406 Power Strings KMP

Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative inte

poj 2406 Power Strings(KMP求循环次数)

题目链接:http://poj.org/problem?id=2406 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, e

poj 2406 Power Strings(kmp循环节)

题目链接:http://poj.org/problem?id=2406 题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]. 例如:      a    b    a    b    a    b next:-1   0    0    1    2    3    4 next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n]; 1 #include <iostream> 2

poj 2406 Power Strings 后缀数组解法

连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们还是用后缀数组做一做,巩固后缀数组的能力. 对于一个串,如果能写出a^n这种形式,我们可以暴力枚举循环节长度L,那么后缀suffix(1)和suffix(1 + L)的LCP应该就是 lenstr - L.如果能满足,那就是,不能,就不是. 这题的话da算法还是超时,等我学了DC3再写上来. 其实这

POJ 2406 Power Strings (求字符串循环节出现的次数)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44217   Accepted: 18449 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "