[poj 2406] Power Strings KMP *nex

Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 40506 Accepted: 16854

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.
题意:输出字符串是否能等于字串^n
SOLUTION: 1.求nex【LEN】,可表示最后的nex即匹配最长的前缀后缀相等; eg:aaaa nex【5】=4;
如nex【4】=nex【5】-1。。。。。。
由nex性质,s1=s2,s2=s3……即循环皆len-nex【len】;

      则nex【len】在前有重复
      可判 if(len%(len-nex[len-1])!=0) ans=1;
      否则ans为商
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int nex[1000005];
char s[1000005];


inline void Kmp(char s[])
{
    int j=-1;
    for(int i=0;s[i];i++)
    {
        while(s[i]!=s[j+1]&&j!=-1) j=nex[j];
        if(s[i]==s[j+1]&&i!=0) j++;
        nex[i]=j; 
    }
}

int main()
{
    while(scanf("%s",s))
    {
        if(s[0]=='.') return 0;
         Kmp(s);
        int len=strlen(s);

        int ans=0;
        nex[len-1]++;
        // cout<<nex[len-1]<<endl;
        if(len%(len-nex[len-1])!=0) ans=1;
        else 
            {
                if(nex[len-1]==0) ans=1;
                else 
                ans=len/(len-nex[len-1]);
            }:
        printf("%d\n",ans);
    }
}

法2:后缀数组;
*height–height【rank【1】】判合法!

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char s[500005];
int wa[500005],wb[500005],sa[500005];
int height[500005];
int n=0;
int cnt[500005];
int r[500005];
int rm[500005];
int rank[500005];
inline void DA(int n,int m)
{
    int *x=wa,*y=wb;
    for(int i=0;i<m;i++) cnt[i]=0;
    for(int i=0;i<n;i++) cnt[x[i]=r[i]]++;
    for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
    for(int i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;
    for(int j=1,p=1;p<n;j*=2,m=p)
    {
        p=0;
        for(int i=n-j;i<n;i++) y[p++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(int i=0;i<m;i++) cnt[i]=0;
        for(int i=0;i<n;i++) cnt[x[y[i]]]++;
        for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
        for(int i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];
        swap(x,y);
        p=1;
        x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=((y[sa[i]]==y[sa[i-1]])&&(y[sa[i]+j]==y[sa[i-1]+j]))?p-1:p++;
    }
}
inline void GET_H(int n)
{
    for(int i=1;i<=n;i++) rank[sa[i]]=i;
    int k=0;
    int j=0;
    for(int i=0;i<n;height[rank[i++]]=k)
        for(k?k--:0,j=sa[rank[i]-1];r[j+k]==r[i+k];k++);
}
void RMQ()  
{  
     int i,k=rank[0];  
     rm[k]=1000000;  
     for (i=k-1;i>=0;i--)   
         if (height[i+1]<rm[i+1]) rm[i]=height[i+1];  
         else rm[i]=rm[i+1];  

     for (i=k+1;i<=n;i++)  
         if (height[i]<rm[i-1]) rm[i]=height[i];  
         else rm[i]=rm[i-1];           
}  

int work()
{
    for (int i=1;i<=n/2;i++)  
    {  
        if (n%i) continue;  
        if (rm[rank[i]]==n-i) return n/i;  
    }  
    return 1;
}
int main()
{
    while(scanf("%s",s))
    {
        if(s[0]=='.') return 0;
            n=strlen(s);
        for(int i=0;i<n;i++)
        r[i]=s[i]-'a'+1;
        r[n]=0;
        DA(n+1,128);
        GET_H(n);
        RMQ();
        printf("%d\n",work());
    }
} 

你可能感兴趣的:(KMP,poj)