poj3693Maximum repetition substring【后缀数组+RMQ求重复最多连续子串】
来源:互联网 发布:淘宝粉星便利店靠谱么 编辑:程序博客网 时间:2024/04/29 19:58
Description
The repetition number of a string is defined as the maximum number R such that the string can be partitioned intoR 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
花费了一晚上本来要学linux和php的时间又钻研后缀数组,可算整明白点了,值~
对于数组的解释:x,y数组都是记录的以当前长度排列的暂时rank值,height[]数组的下标是排名,对于大多数后缀数组的题为何用rmq?为了求以j,k开头的最长公共子序列,min(height[rank[j]+1],height[rank[j]+2]……height[rank[k]]),因为是“公共”所以取min,因为是区间最小,所以用RMQ
这个题要求重复最多的连续子串,那么我们先假设一个重复子串单元为L,从数组开头开始遍历,每次拿出i,i+L求LCP,那么LCP包含了至少LCP/L个循环单元,还得记得i,i+L是按着的,重复次数还得加1.这还没完,还要处理余数的问题。处理之后所有可能重复最小单元长度值都放到a[]数组里,我们遍历sa数组,这个数组本来就是以字典序存储的,那么遇到的第一个符合条件的就输出
/*******************poj36932016.2.2110112K313MSG++3164B*******************/#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int MAXN=100010;int sa[MAXN];int t1[MAXN],t2[MAXN],c[MAXN];int rank[MAXN],height[MAXN];void build_sa(int s[],int n,int m){ 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]=s[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]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++; if(p>=n)break; m=p; }}void getHeight(int s[],int n){ int i,j,k=0; for(i=0;i<=n;i++)rank[sa[i]]=i; for(i=0;i<n;i++) { if(k)k--; j=sa[rank[i]-1]; while(s[i+k]==s[j+k])k++; height[rank[i]]=k; }}int mm[MAXN];int best[20][MAXN];void initRMQ(int n){ mm[0]=-1; for(int i=1;i<=n;i++) mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; for(int i=1;i<=n;i++)best[0][i]=i; for(int i=1;i<=mm[n];i++) for(int j=1;j+(1<<i)-1<=n;j++) { int a=best[i-1][j]; int b=best[i-1][j+(1<<(i-1))]; if(height[a]<height[b])best[i][j]=a; else best[i][j]=b; }}int askRMQ(int a,int b){ int t; t=mm[b-a+1]; b-=(1<<t)-1; a=best[t][a];b=best[t][b]; return height[a]<height[b]?a:b;}int lcp(int a,int b){ a=rank[a];b=rank[b]; if(a>b)swap(a,b); return height[askRMQ(a+1,b)];}char str[MAXN];int a[MAXN],r[MAXN],iCase;int main(){ //freopen("cin.txt","r",stdin); iCase=1; while(~scanf("%s",str)) { if(str[0]=='#') break; int n=strlen(str); for(int i=0;i<=n;i++) r[i]=str[i]; build_sa(r,1+n,128); getHeight(r,n); initRMQ(n); int cnt=0,maxn=0,step; for(int l=1;l<n;l++) { for(int i=0;i+l<n;i+=l) { int t1=lcp(i,i+l); int re=t1%l; step=t1/l+1; int j=i-(l-t1%l); if(j>=0&&re&&lcp(j,j+l)>=t1) step++; if(step>maxn) { maxn=step; cnt=0; a[cnt++]=l; } else if(step==maxn) { a[cnt++]=l; } } } int len=-1,st; for(int i=1;i<=n&&len==-1;i++)//i表示排名 { for(int j=0;j<cnt;j++) { if(lcp(sa[i],sa[i]+a[j])>=(maxn-1)*a[j]) { len=a[j]; st=sa[i]; break; } } } str[maxn*len+st]=0; printf("Case %d: %s\n",iCase++,str+st); } return 0;}
- poj3693Maximum repetition substring【后缀数组+RMQ求重复最多连续子串】
- POJ 3693 Maximum repetition substring (后缀数组+RMQ 求重复最多的连续子串)
- POJ 3693 Maximum repetition substring (后缀数组+RMQ 求重复最多的连续子串)
- Maximum repetition substring+POj+后缀数组之求重复次数最多的连续重复子串
- POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)
- poj 1743Maximum repetition substring(后缀数组+RMQ+重复次数最多的连续重复子串))
- HDU 2459 Maximum repetition substring(后缀数组+RMQ-重复次数最多的子串)
- POJ 3693 Maximum repetition substring(后缀数组[重复次数最多的连续重复子串])
- Poj 3693 & Hdu 2459 Maximum repetition substring (08合肥Online 后缀数组+RMQ 重复次数最多的连续重复子串)
- POJ3693Maximum repetition substring【后缀数组】
- POJ 3693 Maximum repetition substring 后缀数组求重复次数最多子串
- POJ 题目 3693 Maximum repetition substring(后缀数组+RMQ+枚举求最小字典序的重复次数最多的子串)
- poj 3693/hdu 2459 Maximum repetition substring spoj 687. Repeats ( 后缀数组 重复次数最多的连续重复子串)
- POJ 3693 Maximum repetition substring (求重复次数最多的连续子串,4级)
- POJ 3693 Maximum repetition substring(后缀数组求最长重复子串)
- SPOJ 687 Repeats (后缀数组+RMQ 重复次数最多的连续重复子串)
- poj 3693 重复次数最多的连续重复子串(后缀数组+RMQ)
- 【POJ】3693 Maximum repetition substring 【后缀数组——求最长连续重复字串】
- Java排序--》希尔排序(增量排序)
- 【C语言】文件定位读取
- oracle rac理解和用途扩展(转)
- neuq oj 1052: 谭浩强C语言(第三版)习题6.8 C语言
- 解密回声消除技术之二(应用篇)
- poj3693Maximum repetition substring【后缀数组+RMQ求重复最多连续子串】
- 从头认识Spring-3.3 简单的AOP日志实现-增加检查订单功能
- TCP/IP、Http、Socket的区别
- PAT--1013 Battle Over Cities(并查集)
- 利用python求解八数码难题
- 《MVC + EF+ WCF 》——批量删除数据
- HDU1075 What Are You Talking About
- Windows基础-使用XAudio2播放音频(本质是WASAPI)
- 基于百度API的开源自动翻译.srt文件软件的实现的几个技术细节总结附源代码