POJ3693 Maximum repetition substring 后缀数组
来源:互联网 发布:软件开发环境 编辑:程序博客网 时间:2024/04/24 10:15
题目链接:POJ3693
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
//// main.cpp// POJ3693//// Created by teddywang on 2016/10/1.// Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int maxn=100010;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int str[],int sa[],int ranks[],int height[],int n,int m){ n++; int i,j,p,*x=t1,*y=t2; for(i=0;i<m;i++) c[i]=0; for(i=0;i<n;i++) c[x[i]=str[i]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i; for(j=1;j<=n;j<<=1) { p=0; for(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++) c[i]=0; for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<m;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; if(p>=n) break; m=p; } int k=0; n--; for(i=0;i<=n;i++)ranks[sa[i]]=i; for(i=0;i<n;i++) { if(k)k--; j=sa[ranks[i]-1]; while(str[i+k]==str[j+k])k++; height[ranks[i]]=k; }}int ranks[maxn],height[maxn];char str[maxn],s1[maxn];int r[maxn],sa[maxn];int pre[maxn],vis[maxn];int RMQ[maxn],mm[maxn];int dp[maxn][20];void Rmq_Init(int n){ int m=floor(log(n+0.0)/log(2.0)); for(int i=1;i<=n;i++) dp[i][0]=height[i]; for(int i=1;i<=m;i++) { for(int j=n;j;j--){ dp[j][i]=dp[j][i-1]; if(j+(1<<(i-1))<=n) dp[j][i]=min(dp[j][i],dp[j+(1<<(i-1))][i-1]); } }}int Rmq_Query(int l,int r){ int a=ranks[l],b=ranks[r]; if(a>b) swap(a,b); a++; int m=floor(log(b-a+1.0)/log(2.0)); return min(dp[a][m],dp[b-(1<<m)+1][m]);}int main(){ int kase=1; while(scanf("%s",str)) { if(str[0]=='#') break; int len=strlen(str); for(int i=0;i<len;i++) r[i]=str[i]; r[len]=0; da(r,sa,ranks,height,len,128); Rmq_Init(len); int cnt=1,mmax=1,a[maxn]; a[0]=1; for(int i=1;i<len;i++) { for(int j=0;i+j<len;j+=i) { int r=Rmq_Query(j, j+i); int steps=r/i+1; int k=j-(i-r%i); if(k>=0&&r%i) if(Rmq_Query(k, k+i)>=r) steps++; if(steps>mmax) { mmax=steps; cnt=0; a[cnt++]=i; } else if(steps==mmax) a[cnt++]=i; } } int n=-1,st=0; for(int i=1;i<=len&&n==-1;i++) {a\ for(int j=0;j<cnt;j++) { int l=a[j]; if(Rmq_Query(sa[i], sa[i]+l)>=(mmax-1)*l) { n=l; st=sa[i]; break; } } } printf("Case %d: ",kase++); for(int i=st,j=0;j<n*mmax;j++,i++) printf("%c",str[i]); printf("\n"); }}
- POJ3693 Maximum repetition substring 后缀数组
- poj3693 Maximum repetition substring 后缀数组+RMQ
- POJ3693:Maximum repetition substring(后缀数组+RMQ)
- poj3693 Maximum repetition substring (后缀数组+rmq)
- POJ3693 Maximum repetition substring 后缀数组
- POJ3693 Maximum repetition substring (后缀数组)
- Maximum repetition substring(poj3693,后缀数组+RMQ)
- 【POJ3693】Maximum repetition substring 后缀数组恶心题
- 【POJ3693】Maximum repetition substring【后缀数组】【ST表】
- [POJ3693]Maximum repetition substring(后缀数组+st)
- poj3693 Maximum repetition substring
- poj3693 Maximum repetition substring
- poj3693 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
- Linux系统下如何查看及修改文件读写权限
- 静态页面制作(三)
- 优先队列的链表实现
- easyUI中class="easyui-combobox"默认选值
- Cannot fetch index base URL https://pypi.python.org/simple/
- POJ3693 Maximum repetition substring 后缀数组
- 几种常见的排序算法(C++)
- 大话设计模式(八)抽象工厂模式进化
- servlet学习总结(二)—图片下载和读取属性文件
- UIApplicationLaunchOptionsRemoteNotificationKey
- 用rem来做响应式开发
- 知识点二:文件的输入和输出
- hdu 5074
- Java之正则表达式