poj2406 连续重复子串

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 41110   Accepted: 17099

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.



/*
poj2406 连续重复子串

给你一个字符串,请问最多能由任意字符串重复多少次得到

最开始用的是DA算法
想的是枚举长度然后进行一下判断即可,只需要判断Rank[0]和Rank[k]是否为n-k
(因为如果相等,0~k = k+1~2*k+1 .... 递推下去 整个串是0~k的子串不断
 重复得到)
但是MLE.估计是处理RMQ时有问题,而且看了别人报告才发现这并不是最优方法。
因为我们求的是所有数到Rank[0],所以直接从rank[0]的位置往两边扫一遍就行了
但是DA算法nlog(n)好像会TLE- -
于是乎重新去搞了DC3的算法,才搞定 2600ms

而且感觉kmp更适合这个(看比人代码很短的样子)

hhh-2016-03-13 18:11:02
*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef long double ld;
#define lson (i<<1)
#define rson ((i<<1)|1)
const int maxn = 2000001;

#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)=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= 0;i--)
    {
        if(height[i+1] < rm[i+1]) rm[i] = height[i+1];
        else rm[i] = rm[i+1];
    }

    for(int i = to+1;i <= len;i++)
    {
        if(height[i] < rm[i-1]) rm[i] = height[i];
        else rm[i] = rm[i-1];
    }
}

int solve(int len)
{
    for(int i = 1;i <= len/2;i++)
    {
        if(len % i) continue;
        if(rm[Rank[i]] == len-i) return len/i;
    }
    return 1;
}

int main()
{
    while(scanf("%s",ts) != EOF)
    {
        if(ts[0] == '.')
            break;
        int len = strlen(ts);
        for(int i = 0; i < len; i++)
            str[i] = ts[i];
        str[len] = 0;

        dc3(str,sa,len+1,300);
        getheight(str,len);
        iniRMQ(len);
        printf("%d\n",solve(len));
    }
    return 0;
}


你可能感兴趣的:(数据结构)