poj 2406 Power Strings kmp


Language:
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 30779 Accepted: 12821

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


题意:给定一个字符串长度为n,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值。

思路:利用kmp失配函数的性质,若 f [ n ] > 0 且 n % ( n- f [ n ]) == 0,则 R=n / ( n - f [ n ] ) 。详见程序:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=10000000+10;
char p[MAXN];
int f[MAXN];
int main()
{
    //freopen("text1.txt","r",stdin);
    while(scanf("%s",p)!=EOF)
    {
         if(p[0] == '.') continue;
        f[0]=f[1]=0;
        int n=strlen(p);
        for(int i=1;i<n;i++)
        {
            int j=f[i];
            while(j&&p[i]!=p[j]) j=f[j];
            f[i+1]=p[i]==p[j]?j+1:0;
        }
            if(f[n]>0&&n%(n-f[n])==0) printf("%d\n",n/(n-f[n]));
            else printf("1\n");
    }
    return 0;
}

其实此题原本还可以用后缀数组做的,但是估计后来数据加强了,卡后缀数组无论是Dc3还是sa。但是方法还是值得学习的。

思路:
穷举字符串 S 的长度 k,然后判断是否满足。判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k。在询问最长公共前缀的时候, suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[ rank[ 0 ] ]之间的最小值即可。整个做法的时间复杂度为 O(n), 详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1000000+100;
const int inf=0x3fffffff;
int n;
char s[MAXN];
int sa[MAXN],c[MAXN],t1[MAXN],t2[MAXN],rank[MAXN],height[MAXN];
int d[MAXN];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b] && r[a+l]==r[b+l];
}
void build_sa(int m)
{
    int i,k,p=0;
    int *x=t1,*y=t2;
    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;p<n;k<<=1,m=p)
    {
        for(p=0,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);
        for(p=1,x[sa[0]]=0,i=1;i<n;i++)
            x[sa[i]]=cmp(y,sa[i],sa[i-1],k)? p-1:p++;
    }
}
void calheight(int n)
{
    int k=0;
    for(int i=1;i<=n;i++) rank[sa[i]]=i;
    for(int i=0;i<n;i++)
    {
        if(k) k--;
        int j=sa[rank[i]-1];
        while(s[i+k]==s[j+k]) k++;
        height[rank[i]]=k;
    }
}
void RMQ_init(int n)
{
    for(int i=1;i<=n;i++) d[i]=height[i];
    int k=rank[0];
     d[k]=inf;
     for (int i=k-1;i>=0;i--)
         if (height[i+1]<d[i+1]) d[i]=height[i+1];
         else d[i]=d[i+1];
     for (int i=k+1;i<=n;i++)
         if (height[i]<d[i-1]) d[i]=height[i];
         else d[i]=d[i-1];
}
int lcp(int x)
{
    int l=rank[x];
    return d[l];
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(~scanf("%s",s) && s[0]!='.')
    {
        n=strlen(s);
        s[n++]=0;
        build_sa(200);
        calheight(n-1);
        RMQ_init(n-1);
        for(int i=1;i<=(n-1);i++)
        {
            if((n-1)%i)
                continue;
            int k=lcp(i);
            if(k==n-1-i)
            {
                printf("%d\n",(n-1)/i);
                break;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值