• poj3693


    Maximum repetition substring
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9067   Accepted: 2772

    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的串重复出现,那么str[0],str[l],str[2*l]……中肯定有两个连续的出现在字符串中。

    那么就枚举连续的两个,然后从这两个字符前后匹配,看最多能匹配多远。

    即以str[i*l],str[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, 但这不一定是最好的, 因为答案的首尾不一定再我们枚举的位置上. 我的解决方法是, 我们考虑M % L的值的意义, 我们可以认为是后面多了M % L个字符, 但是我们更可以想成前面少了(L - M % L)个字符! 所以我们求后缀j * L - (L - M % L)与后缀(j + 1) * L - (L - M % L)的最长公共前缀。

    即把之前的区间前缀L-M%L即可。

    然后把可能取到最大值的长度L保存,由于 题目要求字典序最小,通过sa数组进行枚举,取到的第一组,肯定是字典序最小的。

    AC代码:

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    using namespace std;
    #define N 100010
    int wa[N],wb[N],wv[N],ww[N];
    int sa[N],lcp[N],rank[N],f[N][21];
    char str[N];
    inline bool cmp(int *r,int a,int b,int len){
    	return r[a]==r[b]&&r[a+len]==r[b+len];
    }
    void construct_sa(int n,int m){
    	int i,j,p,*x=wa,*y=wb,*t;n++;
    	for(i=0;i<m;i++) ww[i]=0;
    	for(i=0;i<n;i++) ww[x[i]=str[i]]++;
    	for(i=1;i<m;i++) ww[i]+=ww[i-1];
    	for(i=n-1;i>=0;i--) sa[--ww[x[i]]]=i;
    	for(j=p=1;p<n;j<<=1,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<m;i++) ww[i]=0;
    		for(i=0;i<n;i++) ww[wv[i]=x[y[i]]]++;
    		for(i=1;i<m;i++) ww[i]+=ww[i-1];
    		for(i=n-1;i>=0;i--) sa[--ww[wv[i]]]=y[i];
    		for(t=x,x=y,y=t,x[sa[0]]=0,p=i=1;i<n;i++)
    			x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    	}
    }
    void construct_lcp(int n){
    	for(int i=0;i<=n;i++) rank[sa[i]]=i;
    	int h=0;
    	lcp[0]=0;
    	for(int i=0;i<n;i++){
    		int j=sa[rank[i]-1];
    		if(h>0) h--;
    		for(;j+h<n&&i+h<n;h++) if(str[i+h]!=str[j+h]) break;
    		lcp[rank[i]-1]=h;
    	}
    }
    void st(int n){
    	for(int i=1;i<=n;i++) f[i][0]=lcp[i-1];
    	for(int i=1;(1<<i)<=n;i++){
    		for(int j=1;j+(1<<i)-1<=n;j++){
    			f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]);
    		}
    	}
    }
    int query(int i,int j){
    	i=rank[i];j=rank[j];
    	if(i>j) swap(i,j);i++;
    	int k=log(j-i+1)/log(2);
    	return min(f[i][k],f[j-(1<<k)+1][k]);
    }
    int tmp[N],kkk;
    bool vis[N];
    int slove(int n){
    	int ans=0,sum;
    	kkk=0;
    	memset(vis,0,sizeof(vis));
    	for(int len=1;len<=n;len++){
    		for(int i=0;i+len<=n;i+=len){
    			int t=query(i,i+len);
    			sum=t/len+1;
    			int pos=i-(len-t%len);
    			if(pos>=0&&t%len!=0) if(query(pos,pos+len)>=(len-t%len)) sum++;
    			if(sum>ans) ans=sum;
    		}
    	}
    	for(int len=1;len<=n;len++){
    		for(int i=0;i+len<=n;i+=len){
    			int t=query(i,i+len);
    			sum=t/len+1;
    			int pos=i-(len-t%len);
    			if(pos>=0&&t%len!=0) if(query(pos,pos+len)>=(len-t%len)) sum++;
    			if(sum==ans&&vis[len]==0){
    				vis[len]=1;
    				tmp[kkk++]=len;
    			}
    		}
    	}
    	return ans;
    }
    int main(){
    	int cas=0;
    	while(scanf("%s",str)==1){
    		if(str[0]=='#') break;
    		int len=strlen(str);
    		construct_sa(len,200);
    		construct_lcp(len);
    		st(len);
    		int ans=slove(len);
    		printf("Case %d: ",++cas);
    		int pos=0,leng=0,flag=0;
    		for(int i=1;i<=len;i++){
    			for(int j=0;j<kkk;j++){//枚举可以使重复次数到达ans的长度
    				int t=query(sa[i],sa[i]+tmp[j]);
    				if(t/tmp[j]+1==ans){//再次计算sa[i]开始的重复次数
    					pos=sa[i];leng=tmp[j];flag=1;break;//满足即跳出
    				}
    			}
    			if(flag) break;
    		}
    		for(int i=pos,j=0;j<leng*ans;j++,i++) printf("%c",str[i]);
    		printf("
    ");
    	}
    	return 0;
    }
    

      

      

  • 相关阅读:
    进行编译时提示'error: unrecognized command line option "-std=gnu11"'如何处理?
    linux shell中如何批量添加一行内容到某些文件的末尾?
    linux内核中的电源管理接口
    linux内核中i2c驱动中slave模式接口的调用
    git clone时报错“Failed to connect to 127.0.0.1 port 2453: Connection refused”如何处理?
    第 8 章 输入框和导航组件
    第 7 章 图标菜单按钮组件
    第 6 章 辅组类和响应式工具
    第 5 章 栅格系统
    第 4 章 表单和图片
  • 原文地址:https://www.cnblogs.com/shenben/p/5765823.html
Copyright © 2020-2023  润新知