POJ 3693 Maximum repetition substring
来源:互联网 发布:淘宝复制宝贝有影响吗 编辑:程序博客网 时间:2024/06/05 06:52
lcp也上场了。。。
断断续续写了几天。。。。最后被后缀数组一套带走了。。。。。
就当是后缀数组的模板吧。。。
[Submit] [Go Back] [Status]
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
ccabababcdaabbccaa#
Sample Output
Case 1: abababCase 2: aa
Source
[Submit] [Go Back] [Status]
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <algorithm>using namespace std;const int maxn=1000012;int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn];char str[maxn];bool cmp(int* r,int a,int b,int l,int n){ if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true; return false;}void radix_sort(int n,int sz){ for(int i=0;i<sz;i++) c[i]=0; for(int i=0;i<n;i++) c[x[y[i]]]++; for(int i=1;i<sz;i++) c[i]+=c[i-1]; for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];}void get_sa(char c[],int n,int sz=128){ x=rank,y=rank2; for(int i=0;i<n;i++) x[i]=c[i],y[i]=i; radix_sort(n,sz); for(int len=1;len<n;len<<=1) { int yid=0; for(int i=n-len;i<n;i++) y[yid++]=i; for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; radix_sort(n,sz); swap(x,y); x[sa[0]]=yid=0; for(int i=1;i<n;i++) { x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid; } sz=yid+1; if(sz>=n) break; } for(int i=0;i<n;i++) rank[i]=x[i];}void get_h(char str[],int n){ int k=0; h[0]=0x3f3f3f3f; for(int i=0;i<n;i++) { if(rank[i]==0) continue; k=max(k-1,0); int j=sa[rank[i]-1]; while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++; h[rank[i]]=k; }}int dp[maxn][20],Log[maxn];void RMQ_init(int n){ for(int i=0;i<n;i++) dp[i][0]=h[i]; for(int i=1;i<=Log[n];i++) { for(int j=0;j+(1<<i)-1<n;j++) { dp[j][i]=min(dp[j][i-1],dp[j+(1<<(i-1))][i-1]); } }}int lcp(int l,int r){ l=rank[l];r=rank[r]; if(l>r) swap(l,r); int a=l+1,b=r; int k=Log[b-a+1]; return min(dp[a][k],dp[b-(1<<k)+1][k]);}int main(){ int cas=1; Log[0] = -1;for(int i=1;i<=maxn;i++) {Log[i]=(i&(i-1))?Log[i-1]:Log[i-1] + 1 ;} while(scanf("%s",str)!=EOF&&str[0]!='#') { int n=strlen(str); get_sa(str,n); get_h(str,n); RMQ_init(n); int l,r,t,k,maxx=-1,a; for(l=1;l<n/2;l++) { for(int i=0;i+l<n;i+=l) { k=lcp(i,i+l); r=k/l+1; t=l-k%l; t=i-t; if(t>=0&&k%l!=0) { if(lcp(t,t+l)>=k) r++; } if(r>maxx) { a=0; maxx=r; ans[a++]=l; } else if(r==maxx) { ans[a++]=l; } } } int pos; int st,b=0,c; for(int i=0;i<n&&!b;i++) { if(b) break; for(int j=0;j<a;j++) { int tl=ans[j]; if(lcp(sa[i],sa[i]+tl)>=(maxx-1)*tl) { st=sa[i]; pos=i; l=tl*maxx; b=1; break; } } } printf("Case %d: ",cas++); for (int i=0;i<l;i++) putchar(str[st+i]); putchar(10); } return 0;}
- poj 3693 Maximum repetition substring
- POJ 3693 Maximum repetition substring
- POJ 3693 Maximum repetition substring
- poj 3693 Maximum repetition substring
- POJ 3693 Maximum repetition substring
- POJ 3693 Maximum repetition substring
- POJ 3693Maximum repetition substring
- poj 3693 Maximum repetition substring
- Maximum repetition substring POJ
- Maximum repetition substring POJ
- poj 3693 Maximum repetition substring //后缀数组
- poj 3693 Maximum repetition substring (后缀数组)
- poj 3693 Maximum repetition substring(后缀数组)
- poj 3693 Maximum repetition substring(后缀数组)
- 【后缀数组】【poj 3693】Maximum repetition substring
- POJ 3693 Maximum repetition substring(好题)
- POJ 3693 Maximum Repetition Substring 后缀数组
- 「POJ 3693」Maximum repetition substring
- getsockopt
- 基于jrtplib的rtp数据发送
- curl使用
- 3G模块拨号上网设置APN,拨号号码,帐号和密码
- 【CSDN常见问题解答】JSON和XML格式互相转换
- POJ 3693 Maximum repetition substring
- maven jetty 热部署 Files are locked on Windows and can't be replaced
- 用于showdialog的js源码
- VirtualBox 修改UUID实现虚拟硬盘复制
- PowerDesigner显示注释问题
- 单链表的就地逆转
- PHP根据中国天气获取本地程式天气
- Oracle学习随笔
- ContentProvider(1):杂言