标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8067 | Accepted: 2463 |
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
思路先求重复次数最多的子串的重复次数,重复次数一样的存下他的循环节长度,然后枚举sa,先符合的一定是最小字典序的
ac代码
Problem: 3693 User: kxh1995 Memory: 10500K Time: 422MS Language: C++ Result: Accepted
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) using namespace std; char str[103030]; int sa[103030],Rank[103030],rank2[103030],height[103030],c[103030],*x,*y,s[103030],ans[100010]; int n; void cmp(int n,int sz) { int i; memset(c,0,sizeof(c)); for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<sz;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; } void build_sa(char *s,int n,int sz) { x=Rank,y=rank2; int i,j; for(i=0;i<n;i++) x[i]=s[i],y[i]=i; cmp(n,sz); int len; for(len=1;len<n;len<<=1) { int yid=0; for(i=n-len;i<n;i++) { y[yid++]=i; } for(i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; cmp(n,sz); swap(x,y); x[sa[0]]=yid=0; for(i=1;i<n;i++) { if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len]) x[sa[i]]=yid; else x[sa[i]]=++yid; } sz=yid+1; if(sz>=n) break; } for(i=0;i<n;i++) Rank[i]=x[i]; } void getHeight(char *s,int n) { int k=0; for(int i=0;i<n;i++) { if(Rank[i]==0) continue; k=max(0,k-1); int j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } } int minv[103010][20],lg[103030]; void init_lg() { int i; lg[1]=0; for(i=2;i<102020;i++) { lg[i]=lg[i>>1]+1; } } void init_RMQ(int n) { int i,j,k; for(i=1;i<=n;i++) { minv[i][0]=height[i]; } for(j=1;j<=lg[n];j++) { for(k=0;k+(1<<j)-1<=n;k++) { minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]); } } } int lcp(int l,int r) { l=Rank[l]; r=Rank[r]; if(l>r) swap(l,r); l++; int k=lg[r-l+1]; return min(minv[l][k],minv[r-(1<<k)+1][k]); } int main() { int c=0; while(scanf("%s",str)!=EOF) { int i,j,k; if(str[0]=='#') break; n=strlen(str); build_sa(str,n+1,128); getHeight(str,n); init_lg(); init_RMQ(n); int maxn=0,num=0; for(i=1;i<n;i++) { for(j=0;j+i<n;j+=i) { int k=lcp(j,j+i); int now=k/i; int tj=j-(i-k%i); if(tj>=0) { if(lcp(tj,tj+i)>=i-k%i) now++; } if(now>maxn) { num=0; ans[num++]=i; maxn=now; } else if(now==maxn) ans[num++]=i; } } int flag=0,len,st; for(i=1;i<=n;i++) { if(flag) break; for(j=0;j<num;j++) { int temp=ans[j]; if(lcp(sa[i],sa[i]+temp)>=maxn*temp) { st=sa[i]; len=temp*(maxn+1); flag=1; } } } printf("Case %d: ",++c); for(i=0;i<len;i++) { printf("%c",str[st+i]); } printf("\n"); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 题目 3693 Maximum repetition substring(后缀数组+RMQ+枚举求最小字典序的重复次数最多的子串)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48001597