POJ 3693 Maximum repetition substring

刚开始一直WA,一直以为是自己往前延展的时候写错了,后来才发现是ST写错了

+-*/的优先级要比位运算优先级高,以后碰上不清楚优先级的运算一定要加括号

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 100010
char s[N];
int r[N],wa[N],wb[N],wv[N],ws[N],sa[N],Rank[N],height[N];
bool cmp(int *r,int a,int b,int l){
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m){
    int i,j,p,*x=wa,*y=wb;
    for(i=0;i<m;i++) ws[i]=0;
    for(i=0;i<n;i++) ws[x[i]=r[i]]++;
    for(i=1;i<m;i++) ws[i]+=ws[i-1];
    for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    for(j=1,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<n;i++) wv[i]=x[y[i]];
        for(i=0;i<m;i++) ws[i]=0;
        for(i=0;i<n;i++) ws[wv[i]]++;
        for(i=1;i<m;i++) ws[i]+=ws[i-1];
        for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
        swap(x,y);
        for(p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
    return;
}
void calheight(int *r,int *sa,int n) {
	int i,j,k=0;
	for(i=1;i<=n;i++) Rank[sa[i]]=i;//i是排名
	for(i=0;i<n;height[Rank[i++]]=k)
		for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
	//for(i=1;i<=n;i++) printf("height[%d]: %d\n",i,height[i]);
	return;
}
int dp[N][25];
void init_RMQ(int n){
	int i,j;
	for(i=1;i<=n;i++) dp[i][0]=height[i];//排名因为在最后面添了个0,所以取值在1-n
	for(j=1;(1<<j)<=n;j++) {//1<<j表长度
		for(i=n;i;i--){//正序?逆序?
			dp[i][j]=dp[i][j-1];
			if(i+(1<<(j-1))<=n) dp[i][j]=min(dp[i][j],dp[i+(1<<(j-1))][j-1]);//注意这里位运算一定要加括号, +-*/比位运算优先级高
		}
	}
}
int query_RMQ(int l,int r){//查询最长公共前缀
	int a=Rank[l],b=Rank[r];
	if(a>b) swap(a,b);//因为这里取了Rank,排名不一定l的在前面了
	a++;//根据height数组的定义需要加1
	int k=log(b-a+1.0)/log(2.0);//b-a+1:长度
	return min(dp[a][k],dp[b-(1<<k)+1][k]);
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int cas=1;
	while(scanf("%s",s)){
		if (s[0]=='#') break;
		int n=strlen(s);
		int l=0;
		int i;
		for(i=0;i<n;i++) r[l++]=s[i];
		r[l]=0;
		da(r,sa,l+1,129);
		calheight(r,sa,l);
		init_RMQ(l);
		printf("Case %d: ",cas++);
		int ans=1;//方便第一次赋值
		int post=0;
		int max_len=1,k,re,add,p;
		int len,j;
		for(len=1;len<=n/2;len++){
			for(i=0;i+len<n;i+=len){
				if(r[i]!=r[i+len]) continue;
				k=query_RMQ(i,i+len);
				re=k/len+1;//重复次数
				p=i;
				add=i-(len-k%len);//循环次数+1的点 

				for(j=i-1;j>=0&&j+len>i&&r[j]==r[j+len];j--) {//这里不能写成j>=left,因为后缀里还有可能有多余的,但不构成循环,所以直接往前面延伸len-1个字符就可以了
					//因为这里加了r[j]==r[j+len],所以只要循环能继续下去,那么lcp一直是递加的,当lcp能凑齐len的整数倍时,就循环次数+1
					if(j==add) {
						re++;
						p=add;
					}
					else if(Rank[j]<Rank[p]){
						p=j;
					}
				}
				if(re>ans) {
					ans=re;post=p;max_len=len;
				}
				else if(re==ans&&Rank[p]<Rank[post]){
					post=p;max_len=len;
				}
			}
		}

		if(ans==1) printf("%c",r[sa[1]]);
		else for(i=post;i<max_len*ans+post;i++) printf("%c",r[i]);
		printf("\n");
	}
	return 0;
}
时间: 2024-07-30 12:07:55

POJ 3693 Maximum repetition substring的相关文章

poj 3693 Maximum repetition substring(后缀数组)

题目链接:poj 3693 Maximum repetition substring 题目大意:求一个字符串中循环子串次数最多的子串. 解题思路:对字符串构建后缀数组,然后枚举循环长度,分区间确定.对于一个长度l,每次求出i和i+l的LCP,那么以i为起点,循环子串长度为l的子串的循环次数为LCP/l+1,然后再考虑一下从i-l+1~i之间有没有存在增长的可能性. #include <cstdio> #include <cstring> #include <vector>

POJ 3693 Maximum repetition substring (后缀数组)

题目大意: 求出字典序最小,重复次数最多,的子串. 思路分析: RMQ + height 数组可以求出任意两个后缀的lcp 我们枚举答案字符串的重复的长度. 如果这个字符串的长度为 l ,而且这个字符串出现过两次或两次以上 那么你会发现在原串中  str[0] str[l] str[2*l] ....肯定有相邻的两个被包含在重复的串中. 我们求出这两个相邻的后缀的lcp 我们上面仅仅说的是被包含在重复的串中,但并不一定就是以 str[0], str[l],str[2*l]....为起点的. 那我

POJ 3693 Maximum repetition substring(后缀数组神题)

POJ 3693 Maximum repetition substring 题目链接 题意:给定一个字符串,求出其子串中,重复次数最多的串,如果有相同的,输出字典序最小的 思路:枚举长度l,把字符串按l分段,这样对于长度为l的字符串,肯定会包含一个分段位置,这样一来就可以在每个分段位置,往后做一次lcp,求出最大匹配长度,然后如果匹配长度有剩余,看剩余多少,就往前多少位置再做一次lcp,如果匹配出来长度更长,匹配次数就加1,这样就可以枚举过程中保存下答案了 这样问题还有字典序的问题,这个完全可以

POJ 3693 Maximum repetition substring (寻找重复次数最多的连续子串)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9083   Accepted: 2782 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

POJ 3693 Maximum repetition substring(最多重复次数的子串)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10461   Accepted: 3234 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same cons

POJ 3693 Maximum repetition substring(后缀数组+RMQ)

Maximum repetition substring 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&quo

poj 3693 Maximum repetition substring(有点麻烦的后缀数组)

Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6638   Accepted: 2007 Description The repetition number of a string is defined as the maximum number R such that the string can be partitioned into R same conse

POJ 3693 Maximum repetition substring(后缀数组求最长重复子串)

题目大意:和spoj687类似,就是当长度相同是需要输出一个最小的字典序的序列. 解体思路:这次需要枚举所有的从i到d = i-L/i (d = i-L%i)的位置,然后记录保证最大值的同时,求出来字典序最小的. Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7418   Accepted: 2217 Description The repetition numb

POJ - 3693 Maximum repetition substring(后缀数组求重复次数最多的连续重复子串)

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

POJ 3693 Maximum repetition substring(后缀数组+ST表)

[题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+l]前缀匹配得到的最长长度为r, 枚举所有的l和i,得到r,那么答案就是r/l+1的最大值. 计算任意后缀的最长公共前缀可以利用后缀数组+ST表来解决, 两个后缀的最长公共前缀就是他们名次之间的h数组的最小值. 显然,枚举i和l的复杂度达到了O(n2),是没有办法完成统计的, 我们发现每个区段只会存