1. 程式人生 > >poj(2406) kmp

poj(2406) kmp

多少 amp instead 沒有 數組 是否 present () 次方

題目鏈接: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;
}

poj(2406) kmp