poj 2406 kmp求连续重复子串的个数

Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 33119 Accepted: 13775

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 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.

Source

#include<stdio.h>
#include<string.h>
#define N 1000005
char str[N];
int next[N];
void getnext(char *str)
{
    int i,n,j;
    n=strlen(str);
    next[0]=-1; j=-1; i=0;
    while(i<n)
    {
        if(j==-1||str[i]==str[j])
        {
            i++; j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}
int main()
{
    int n;
    while(scanf("%s",str)!=EOF)
    {
        if(strcmp(str,".")==0)
            break;
        n=strlen(str);
        getnext(str);
        if(n%(n-next[n])==0)
            printf("%d\n",n/(n-next[n]));
        else
            printf("1\n");
    }
    return 0;
}

用后缀数组写的超时啦。 大哭
/*做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足。判断的时候,
先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共
前缀是否等于 n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以 RMQ
问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到
height[rank[1]]之间的最小值即可。整个做法的时间复杂度为 O(n)。*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 1000005
int t1[N],t2[N],x[N],c[N],sa[N],s[N],rank[N],height[N],b[N];
char str[N];
void build_sa(int *s,int n,int m)
{
	int *x=t1,*y=t2,i,k;
	for(i=0;i<m;i++) c[i]=0;
	for(i=0;i<n;i++) c[x[i]=s[i]]++;
	for(i=1;i<m;i++) c[i]+=c[i-1];
	for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
	for(k=1;k<=n;k<<=1)
	{
		int p=0;
		for(i=n-k;i<n;i++) y[p++]=i;
		for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
		for(i=0;i<m;i++) c[i]=0;
		for(i=0;i<n;i++) c[x[y[i]]]++;
		for(i=1;i<m;i++) c[i]+=c[i-1];
		for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
		swap(x,y);
		p=1; x[sa[0]]=0;
        for(i=1;i<n;i++)
			x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
		if(p>=n)
			break;
		m=p;
	}
}
void getheight(int n)
{
	int i,k=0,j;
	for(i=0;i<=n;i++)
		rank[sa[i]]=i;
	for(i=0;i<n;i++)
	{
		if(k) k--;
		j=sa[rank[i]-1];
		while(s[j+k]==s[i+k])  k++;
		height[rank[i]]=k;
	}
}
int main()
{
    int n,m,mi,i,k;
    while(scanf("%s",str)!=EOF)
    {
        n=strlen(str);
        for(i=0;str[i]!='\0';i++)
            s[i]=str[i];
        s[n]=0;
        build_sa(s,n+1,200);
        getheight(n);
        //for(i=1;i<=n;i++)
           // printf("high=%d\n",height[i]);
        m=rank[0]; mi=height[m];
       // printf("m=%d mi=%d\n",m,mi);
        for(i=m;i>1;i--)
        {
            //printf("%d %d\n",i,height[i]);
            if(height[i]<mi)
                mi=height[i];
           // printf("mi=%d\n",mi);
            b[i-1]=mi;
        }
        mi=height[m+1];
        for(i=m+1;i<=n;i++)
        {
            if(height[i]<mi)
                mi=height[i];
             b[i]=mi;
        }
        for(k=1;k<=n;k++)
        {
            m=rank[k];
            if(b[m]==n-k)
                break;
        }
        printf("%d\n",n/k);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值