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

POJ3693:Maximum repetition substring(后缀数组+RMQ)

时间:2015-06-01 22:36:06      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:后缀数组   poj   

Description

The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1.

Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.

Input

The input consists of multiple test cases. Each test case contains exactly one line, which
gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.

The last test case is followed by a line containing a ‘#‘.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input

ccabababc
daabbccaa
#

Sample Output

Case 1: ababab
Case 2: aa

Source




题意:求重复次数最多的连续重复子串,并且要求字典序最小的

还是论文里的题目

分析:

容易想到的就是枚举长度为L,然后看长度为L的字符串最多连续出现几次。

长度为L的串重复出现,那么st[0],st[l],st[2*l]……st[k*l]中肯定有两个连续的出现在字符串中。不然肯定长度不超过2*L啊。那么我们就枚举连续的两个,然后从这两个字符前后匹配,看最多能匹配多远。

即以st[i*l],st[i*l+l]前后匹配,这里是通过查询suffix(i*l),suffix(i*l+l)的最长公共前缀

通过rank值能找到i*l,与i*l+l的排名,我们要查询的是这段区间的height的最小值,通过RMQ预处理

达到查询为0(1)的复杂度,设LCP长度为M, 则答案显然为M / L + 1, 但这只是以i*l和i*l+l为起点的情况, 不过有一点是可以确定的。如果目标子串包含了

i*l和i*l+l。那么 i*l一定是和i*l+l匹配的。因为目标串中p一定和p+l匹配。这样才能满足子串长度为l。先在要解决的就是起点不在这两个位置上怎么办了。

 得到M/L+1我们可以试着把答案变大。如果M%L!=0我们可以把长度补齐到L的整数倍。即在前面增加(L-M%L)的字符.看能不能使答案变大。为什么这样做是可以的呢?因为我们要使啊、答案变大往后扩展肯定不行了。因为后面已经不匹配了。但是我们为什么扩展 (L-M%L)这么多就行了呢。比这个小肯定是不行的。因为还是没到L的整数倍。比这个多能行的话。去这个值一定能行。因为p是和p+L匹配的。既然取得比这个多。大不了往右平移几个还是能使 M%L得到匹配。那为什么只扩展一个长度L。不扩展多个呢。因为你是枚举每个i*l和i*l+l。你扩展2个或两个以上就是前面的 i*l和i*l+l的情况了。这一步完成后我们只能得到度数最大长度可能的取值。剩下的工作就是找字典序最小了。 通过sa数组进行枚举,取到的第一组,肯定是字典序最小的。

#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
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank[N],height[N],s[N],a[N];
char str[N],str1[N],str2[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
    return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包含末尾添加的0
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++)  wsf[i]=0;
    for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;
    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
    for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;
    p=1;
    j=1;
    for(; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++)  y[p++]=i;
        for(i=0; i<n; i++)  if(sa[i]>=j)  y[p++]=sa[i]-j;
        for(i=0; i<n; i++)  wv[i]=x[y[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--)  sa[--wsf[wv[i]]]=y[i];
        t=x;
        x=y;
        y=t;
        x[sa[0]]=0;
        for(p=1,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
    }
}
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 Log[N];
int best[20][N];

void setLog(int n)
{
    Log[0] = -1;
    for(int i=1; i<=n; i++)
    {
        Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1 ;
    }
}
void RMQ(int n) {//初始化RMQ
    for(int i = 1; i <= n ; i ++) best[0][i] = height[i];
    for(int i = 1; i <= Log[n] ; i ++) {
        int limit = n - (1<<i) + 1;
        for(int j = 1; j <= limit ; j ++) {
            best[i][j] = min(best[i-1][j] , best[i-1][j+(1<<i>>1)]);
        }
    }
}
int lcp(int a,int b) {//询问a,b后缀的最长公共前缀
    a = rank[a];    b = rank[b];
    if(a > b) swap(a,b);
    a ++;
    int t = Log[b - a + 1];
    return min(best[t][a] , best[t][b - (1<<t) + 1]);
}
int ans[N],len,cas = 1;
int main()
{
    int i,j,k,n,l;
    setLog(N-1);
    W(~scanf("%s",str))
    {
        if(str[0]=='#') break;
        n = strlen(str);
        UP(i,0,n-1)
        s[i] = str[i];
        s[n] = 0;
        getsa(s,sa,n+1,300);
        getheight(s,n);
        RMQ(n);
        int maxn = -1;
        len = 0;
        UP(l,1,n-1)
        {
            for(i=0;i+l<n;i+=l)
            {
                k = lcp(i,i+l);
                int m = k/l+1;
                int t = l-k%l;
                t = i-t;
                if(t>=0 && k%l)
                {
                    if(lcp(t,t+l)>=k) m++;
                }
                if(m>maxn)
                {
                    len = 0;
                    ans[len++] = l;
                    maxn = m;
                }
                else if(m == maxn)
                {
                    ans[len++] = l;
                }
            }
        }
        int start,flag = 0;
        UP(i,1,n)
        {
            if(flag)
            break;
            UP(j,0,len-1)
            {
                int tem = ans[j];
                if(lcp(sa[i],sa[i]+tem)>=(maxn-1)*tem)
                {
                    start = sa[i];
                    l = tem*maxn;
                    flag = 1;
                    break;
                }
            }
        }
        printf("Case %d: ",cas++);
        UP(i,0,l-1)
        printf("%c",str[start+i]);
        printf("\n");
    }
}



POJ3693:Maximum repetition substring(后缀数组+RMQ)

标签:后缀数组   poj   

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

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