POJ 2406 Power Strings(后缀数组[连续重复子串])

此文章可以使用目录功能哟↑(点击上方[+])

题集链接→Waterloo local 2002.07.01

 POJ 2406 Power Strings

Accept: 0    Submit: 0
Time Limit: 3000 MS    Memory Limit : 65536 K

 Problem 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.

 Problem Idea

解题思路:

【题意】

如果一个字符串L是由某个字符串S重复R次而得到的, 则称L是一个连续重复串。 R是这个字符串的重复次数。
现在给你一个连续重复串L,问R最大为多少

【类型】
后缀数组[连续重复子串]
【分析】

此题的解法有很多种,但由于博主最近在学后缀数组,故以后缀数组的方法来求解

"连续重复子串"解法(摘自罗穗骞的国家集训队论文):

穷举字符串S的长度k,然后判断是否满足。判断的时候,先看字符串L的长度能否被k整除,再看suffix(1)和suffix(k+1)的最长公共前缀是否等于n-k。在询问最长公共前缀的时候, suffix(1)是固定的,所以RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为O(n)。

接着,我们来好好解释一下这种解法

穷举字符串S的长度k,然后判断是否满足,判断条件有两个:

①字符串L的长度能否被k整除;

这个条件很显然,若是不能被k整除,又如何能够由此字符串翻转k次得到

②suffix(1)和suffix(k+1)的最长公共前缀是否等于n-k。

这个条件其实也蛮好理解的,具体见下图


而计算suffix(1)和suffix(1+k)的最长公共前缀,一般可采用RMQ预处理,但是本题如果采用RMQ的话,会导致MLE,毕竟是1000000*20的数组,而且后缀数组还花去了一部分开销

由于区间一端已经固定(即suffix(1)),所以我们可以花O(n)时间预处理出 height 数组中的每一个数到height[rank[1]]之间的最小值

ps:此题卡倍增算法(O(nlogn)),故需使用DC3算法(O(n))才可以过

【时间复杂度&&优化】
O(n)

题目链接→POJ 2406 Power Strings

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 10;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
const int MAXN = 1000005;
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MAXN],wb[MAXN],wv[MAXN],ws_[MAXN];
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++) ws_[i]=0;
    for(i=0; i<n; i++) ws_[wv[i]]++;
    for(i=1; i<m; i++) ws_[i]+=ws_[i-1];
    for(i=n-1; i>=0; i--) b[--ws_[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++];
}
//各个参数的作用和前面的倍增算法一样,不同的地方是r数组和sa数组的
//大小都要是3*n,这为了方便下面的递归处理,不用每次都申请新的内存空间
int Rank[MAXN], height[MAXN], sa[3*MAXN], r[3*MAXN];
void calheight(int *r,int *sa,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++)Rank[sa[i]]=i;
    for(i=0; i<n; height[Rank[i++]]=k)
        for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
}
char s[MAXN];
int query[MAXN];
int main()
{
    int n,i;
    while(scanf("%s",s)&&strcmp(s,".")!=0)
    {
        n=strlen(s);
        for(i=0; i<n; i++)
            r[i]=s[i];
        r[i]=0;
        dc3(r,sa,n+1,128);
        calheight(r,sa,n);
        query[Rank[0]]=inf;
        for(i=Rank[0]-1; i>=1; i--)
            query[i]=min(height[i+1],query[i+1]);
        for(i=Rank[0]+1; i<=n; i++)
            query[i]=min(height[i],query[i-1]);
        for(i=1; i<=n; i++)
            if(n%i==0&&query[Rank[i]]==n-i)
                break;
        printf("%d\n",n/i);
    }
    return 0;
}
菜鸟成长记

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值