码迷,mamicode.com
首页 > 编程语言 > 详细

POJ2406:Power Strings(后缀数组DC3)

时间:2015-06-01 14:43:30      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:poj   后缀数组   

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.

题意:求字符串最多的循环次数

论文里的分析
算法分析: 


    做法比较简单,穷举字符串S的长度k,然后判断是否满足。判断的时候, 


先看字符串L的长度能否被k整除,再看suffix(1)和suffix(k+1)的最长公共 


前缀是否等于n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以RMQ 


问题没有必要做所有的预处理,只需求出height数组中的每一个数到 


height[rank[1]]之间的最小值即可。整个做法的时间复杂度为O(n)。


这道题我开始使用的倍增算法,但是一直超时,然后使用DC3算法,还是需要2.5S的时间,所以后缀数组应该不是这道题最好的算法,不过现在是为了学习后缀数组,所以无所谓了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wsf[N],wa[N],wb[N],wv[N],sa[N],rank[N],height[N],f[N];
int s[N],a[N];
char str[N],str1[N],str2[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int c0(int *r,int a,int b)
{
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b)
{
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i=0; i<n; i++) wv[i]=r[a[i]];
    for(i=0; i<m; i++) wsf[i]=0;
    for(i=0; i<n; i++) wsf[wv[i]]++;
    for(i=1; i<m; i++) wsf[i]+=wsf[i-1];
    for(i=n-1; i>=0; i--) b[--wsf[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m)
{
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i;
    sort(r+2,wa,wb,tbc,m);
    sort(r+1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0; i<tbc; i++) san[rn[i]]=i;
    for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    sort(r,wb,wa,ta,m);
    for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0; i<ta && j<tbc; p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(; i<ta; p++) sa[p]=wa[i++];
    for(; j<tbc; p++) sa[p]=wb[j++];
    return;
}
void getheight(int *r,int n)//n不保存最后的0
{
    int i,j,k=0;
    for(i=1; i<=n; i++)  rank[sa[i]]=i;
    for(i=0; i<n; i++)
    {
        if(k)
            k--;
        else
            k=0;
        j=sa[rank[i]-1];
        while(r[i+k]==r[j+k])
            k++;
        height[rank[i]]=k;
    }
}

int rm[N];

void RMQ(int n)
{
    int k = rank[0];
    rm[k] = N;
    int i;
    DOWN(i,k-1,0)
    {
        if(height[i+1]<rm[i+1]) rm[i]=height[i+1];
        else rm[i]=rm[i+1];
    }
    UP(i,k+1,n)
    {
        if(height[i]<rm[i-1]) rm[i]=height[i];
        else rm[i]=rm[i-1];
    }
}

int solve(int n)
{
    int i;
    UP(i,1,n/2)
    {
        if(n%i) continue;
        if(rm[rank[i]]==n-i) return n/i;
    }
    return 1;
}

int main()
{
    int n,len,i,j,k;
    W(~scanf("%s",str))
    {
        if(str[0]=='.')
            break;
        len = strlen(str);
        UP(i,0,len-1)
        s[i]=str[i];
        s[len] = 0;
        dc3(s,sa,len+1,300);
        getheight(s,len);
        RMQ(len);
        printf("%d\n",solve(len));
    }
}


POJ2406:Power Strings(后缀数组DC3)

标签:poj   后缀数组   

原文地址:http://blog.csdn.net/libin56842/article/details/46310425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!