8/5(后缀数组应用/板板)

题目描述

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can’t predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns – 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。
输入格式

Line 1: Two space-separated integers: N and K

Lines 2…N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
输出格式

Line 1: One integer, the length of the longest pattern which occurs at least K times
输入输出样例
输入 #1

8 2
1
2
3
2
3
2
3
1

输出 #1

4
在这里插入图片描述这个是对这个样例的一个剖析。
确定某个子串至少出现了k次,只需要取连续的k个后缀数组进行判断lcp,lcp就是至少出现k次的字符串。
就举例子说明吧:比如排序为3,4,5的后缀,他们的lcp为23。所以23至少出现了3次。
1、取k个后缀,因为他们的lcp在原串中的位置是不一样的,所以lcp至少出现了k次。
2、其次是取连续的,假设lcp已经存在了,含有lcp的后缀一定是连续的。这个可以反证。

不难理解。
题目要求的是至少出现k次的最长子串的长度。
那么根据1、要比较k个后缀lcp,根据2、这k个后缀得是连续的。
然后维护lcp的最大长度即可。

虽然是模板题,但是还是要学习其思想。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long ll;
const int N=2e4+5;
int  str[N];
int ht[N<<1],sa[N<<1],rk[N<<1],cnt[N<<1],oldrk[N<<1],id[N<<1];


void get_sa(int n){//传入长度 
	int m=max(300,n);//开始是拿ascll码排名 ,下面开始基数排序 
	for(int i=1;i<=n;i++) cnt[rk[i]=str[i]]++;
	for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];//前缀和 
	for(int i=n;i>0;i--) sa[cnt[rk[i]]--]=i; //排名为cnt[rk[i]]--的子串的指针标号 
	for(int w=1;w<n;w<<=1){//做两次基数排序是按照两个关键字排序的 
       memset(cnt,0,sizeof(cnt));
       for(int i=1;i<=m;i++) id[i]=sa[i];//储存上一个sa数组 
       for(int i=1;i<=n;i++) cnt[rk[id[i]+w]]++;
       for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
       for(int i=n;i>0;i--) sa[cnt[rk[id[i]+w]]--]=id[i];//上一次排在第i位的子串(子串用其首地址表示)根据第二个关键字排在第几 
	   memset(cnt,0,sizeof(cnt));
	   for(int i=1;i<=m;i++) id[i]=sa[i];//保证从排名后的往前面扫,保证在这次扫的过程中,第二个关键字相同时,第一次排名后的在后面 
	   for(int i=1;i<=n;i++) cnt[rk[id[i]]]++;
       for(int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
       for(int i=n;i>0;i--) sa[cnt[rk[id[i]]]--]=id[i];// 
	   memcpy(oldrk,rk,sizeof(rk));
	   for(int i=1,p=0;i<=n;i++){//注意细节i<n 
	   	if(oldrk[sa[i]]==oldrk[sa[i-1]]&&oldrk[sa[i]+w]==oldrk[sa[i-1]+w]){//会出现排名不一样,但是两个关键字都同的清况 
	   		rk[sa[i]]=p;
		   }
		else{
			rk[sa[i]]=++p;
		}   
	   } 
   }
}

void get_ht(int n){
	int k,i;
	for(i=1,k=0;i<=n;i++){
		if(k) k--;
		while(str[i+k]==str[sa[rk[i]-1]+k]) k++;
		ht[rk[i]]=k;
	}
}

int newcmp(int i,int j){//i后缀倒j后缀 
	int minn=ht[i+1]; 
	for(int z=i+1;z<=j;z++)
		minn=min(minn,ht[z]);	
	return minn;
}

int  solve(int n,int k){
	 int maxx=0;
	 for(int i=1;i<=n-k+1;i++)
	 	maxx=max(maxx,newcmp(i,i+k-1));	 
	return maxx;	
}
int main(){
	int n,k;
	cin>>n>>k;
	getchar();
	for(int i=1;i<=n;i++){
      scanf("%d",&str[i]);
 	  }
    get_sa(n);
    get_ht(n);
	if(k!=1){
    int ans=solve(n,k);
	cout<<ans<<endl;
	}
	else
	cout<<"1"<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值