zoukankan      html  css  js  c++  java
  • poj 2406 Power Strings (后缀数组 || KMP)

    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 28859   Accepted: 12045

    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

      1 //42704K    2829MS    C++    2709B    2013-12-18 13:31:43
      2 /*
      3 
      4     题意:
      5         给出一个由某个子串重复R次组成的字符串,求R的最大值
      6     
      7     后缀数组:
      8         KMP应该会简单些,因为此处要练习后缀数组,故用后缀数组。首先考虑用DA的话会TLE,
      9     因为其时间复杂度为O(nlgn),数据太大(n=2000000)。
     10         此处用dc3,dc3的算法没研究,只是套用其模板,dc3算法会比DA快些,在此处勉勉强强
     11     的过了。先用dc3求出后缀数组,然后再求height数组,再穷举字符串长度len。求k能整除len 
     12     且suffix(1) 与 suffix(k+1) 最长前缀等于len-k。
     13         在求最长公共前缀时,由于suffix(1)是固定的,利用height数组的特性,求出height数组
     14     中每一个数到height[rank[0]]间的最小值即可。  
     15  
     16 */
     17 #include<stdio.h>
     18 #include<string.h>
     19 #define N 2000005
     20 #define F(x) ((x)/3+((x)%3==1?0:tb))
     21 #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
     22 int wa[N],wb[N],wv[N],ws[N];
     23 int rank[N],height[N];    
     24 int sa[N],r[N];
     25 char c[N];
     26 int lcp[N]; //记录到height[rank[0]]的最小值 
     27 int Max(int a,int b)
     28 {
     29     return a>b?a:b;
     30 } 
     31 int Min(int a,int b)
     32 {
     33     return a<b?a:b;
     34 }
     35 int cmp(int *y,int a,int b,int l)
     36 {
     37     return y[a]==y[b]&&y[a+l]==y[b+l];   
     38 }
     39 
     40 int c0(int *y,int a,int b)
     41 {
     42     return y[a]==y[b]&&y[a+1]==y[b+1]&&y[a+2]==y[b+2];
     43 }
     44 int c12(int k,int *y,int a,int b)
     45 {
     46     if(k==2) return y[a]<y[b]||y[a]==y[b]&&c12(1,y,a+1,b+1);
     47     else return y[a]<y[b]||y[a]==y[b]&&wv[a+1]<wv[b+1];
     48 }
     49 void sort(int *r,int *a,int *b,int n,int m)
     50 {
     51     int i;
     52     for(i=0;i<n;i++) wv[i]=r[a[i]];
     53     for(i=0;i<m;i++) ws[i]=0;
     54     for(i=0;i<n;i++) ws[wv[i]]++;
     55     for(i=1;i<m;i++) ws[i]+=ws[i-1];
     56     for(i=n-1;i>=0;i--) b[--ws[wv[i]]]=a[i];
     57     return;
     58 }
     59 void dc3(int *r,int *sa,int n,int m)
     60 {
     61     int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
     62     r[n]=r[n+1]=0;
     63     for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
     64     sort(r+2,wa,wb,tbc,m);
     65     sort(r+1,wb,wa,tbc,m);
     66     sort(r,wa,wb,tbc,m);
     67     for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
     68         rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
     69     if(p<tbc) dc3(rn,san,tbc,p);
     70         else for(i=0;i<tbc;i++) san[rn[i]]=i;
     71     for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
     72     if(n%3==1) wb[ta++]=n-1;
     73     sort(r,wb,wa,ta,m);
     74     for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
     75     for(i=0,j=0,p=0;i<ta && j<tbc;p++)
     76         sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
     77     for(;i<ta;p++) sa[p]=wa[i++];
     78     for(;j<tbc;p++) sa[p]=wb[j++];
     79     return;
     80 }
     81 void get_height(int n)
     82 {
     83     int i,j,k=0;
     84     for(i=0;i<=n;i++) rank[sa[i]]=i;
     85     for(i=0;i<n;height[rank[i++]]=k)
     86         for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
     87     return; 
     88 }
     89 int main(void)
     90 {
     91     while(scanf("%s",c)!=EOF)
     92     {
     93         if(strcmp(c,".")==0) break;
     94         int n=strlen(c);
     95         for(int i=0;i<n;i++)
     96             r[i]=c[i]+1;
     97         r[n]=0;
     98         dc3(r,sa,n+1,256);
     99         get_height(n);
    100         //for(int i=0;i<n;i++) printf("%d %d %d
    ",i,rank[i],height[i]);
    101         memset(lcp,0,sizeof(lcp));
    102         lcp[rank[0]]=N;
    103         for(int i=rank[0]-1;i>=0;i--) lcp[i]=Min(lcp[i+1],height[i+1]);
    104         for(int i=rank[0]+1;i<=n;i++) lcp[i]=Min(lcp[i-1],height[i]);
    105         //for(int i=0;i<=n;i++) printf("%d %d %d
    ",rank[i],height[i],lcp[i]);
    106         for(int k=1;k<=n;k++)  //遍历所有值 
    107             if(n%k==0 && lcp[rank[k]]==n-k){
    108                 printf("%d
    ",n/k);
    109                 break;
    110             }
    111     }
    112     return 0;
    113 }
    114 /*
    115 abcd
    116 aaaa
    117 ababab
    118 */
  • 相关阅读:
    关于各种好玩的神奇函数
    模板——AC自动机
    模板——造数据
    VIM常用操作
    springboot注解
    面试题
    Linux常用命令
    Zookeeper
    对cpu与load的理解及线上问题处理思路
    top
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3480212.html
Copyright © 2011-2022 走看看