poj(2406) kmp

题目链接:https://vjudge.net/problem/POJ-2406

kmp学习:https://blog.csdn.net/starstar1992/article/details/54913261/

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 integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

题目大意:问你一个子串的最多多少次方能构成完整的串

思路:其实这题本人是没有想到next数组能直接求出来的 ,后来看了一下题解,大概意思如下:

比如:  ababab,显然最后一位的next数组是3(这里以0开始),所以0~3位和2~5位是一样的,所以0~1和2~3是一样的,2~3和4~5是一样的,所以最多次方数就是3个

比如:ababa,显然最后一位的next数组是3,所以0~2位和2~4位是一样的,所以0~1和2~3是一样的,但是还剩下一位,所以最大只能是1

综上所述,其实就是求最后一位的next数组,然后总长减去它,看总长是否能够整除这个值,能的话直接输出整除的值,不能的话,答案就是1

看一下证明:

-----------------------------

-----------------------------

k    m        x      j       i

由上,next【i】=j,两段红色的字符串相等(两个字符串完全相等),s[k....j]==s[m....i]

设s[x...j]=s[j....i](xj=ji)

则可得,以下简写字符串表达方式

kj=kx+xj;

mi=mj+ji;

因为xj=ji,所以kx=mj,如下图所示

-------------

-------------

k   m        x     j

看到了没,此时又重复上面的模型了,kx=mj,所以可以一直这样递推下去

所以可以推出一个重要的性质len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]);

证明来源:https://blog.csdn.net/hp_satan/article/details/18085919

看代码:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=1e8+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn];
int next[maxn];
void cal_next()
{
    next[0]=-1;
    int k=-1;
    int len=strlen(a);
    for(int i=1;i<len;i++)
    {
        while(k>-1&&a[k+1]!=a[i])
        {
            k=next[k];
        }
        if(a[k+1]==a[i]) k++;
        next[i]=k;
    }
}
int main()
{
    //while(cin>>a)
    while(scanf("%s",a)!=EOF)
    {
        if(a[0]==‘.‘) break;
        cal_next();
        int len=strlen(a);
        if(len%(len-next[len-1]-1)==0) printf("%d\n",len/(len-next[len-1]-1));
        else printf("1\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caijiaming/p/9652254.html

时间: 2024-08-30 02:22:37

poj(2406) kmp的相关文章

POJ 2406 KMP/后缀数组

题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就能求出循环次数. #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<str

Power Strings (poj 2406 KMP)

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

POJ 2406 KMP 循环节

给一个字符串.求这个串的最小的循环节的长度. 好像.num = len/(len-next[len]) 就是循环节的长度.如果 len%(len-next[len]) ==0 就是 说字符串长度刚好是循环节长度的整数倍.不然的话.说明没有最小循环节. 证明嘛.这里好像还是蛮靠谱的.http://blog.csdn.net/euler_m/article/details/6281903 但是..还是没有看懂..~~~~(>_<)~~~~暂且记住吧,, 附代码: 1 #include<std

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)时,出现了不匹配: 主串   

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&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求循环次数)

题目链接: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