poj 1743 Musical Theme【二分+SA】
差分,然后二分长度mid,判断是把height按照min不小于mid分组,取最大最小的sa位置看是否>=mid即可,注意差分后最后答案要+1


#include<iostream>
#include<cstdio>
using namespace std;
const int N=50005;
int n,a[N],r[N],wa[N],wb[N],wsu[N],wv[N],sa[N],rk[N],he[N];
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
bool cmp(int r[],int a,int b,int l)
{
	return r[a]==r[b]&&r[a+l]==r[b+l];
}
void saa(int r[],int n,int m)
{
	int *x=wa,*y=wb;
	for(int i=0;i<=m;i++)
		wsu[i]=0;
	for(int i=1;i<=n;i++)
		wsu[x[i]=r[i]]++;
	for(int i=1;i<=m;i++)
		wsu[i]+=wsu[i-1];
	for(int i=n;i>=1;i--)
		sa[wsu[x[i]]--]=i;
	for(int j=1,p=1;j<n&&p<n;j<<=1,m=p)
	{
		p=0;
		for(int i=n-j+1;i<=n;i++)
			y[++p]=i;
		for(int i=1;i<=n;i++)
			if(sa[i]>j)
				y[++p]=sa[i]-j;
		for(int i=1;i<=n;i++)
			wv[i]=x[y[i]];
		for(int i=0;i<=m;i++)
			wsu[i]=0;
		for(int i=1;i<=n;i++)
			wsu[wv[i]]++;
		for(int i=1;i<=m;i++)
			wsu[i]+=wsu[i-1];
		for(int i=n;i>=1;i--)
			sa[wsu[wv[i]]--]=y[i];
		swap(x,y);
		p=1;
		x[sa[1]]=1;
		for(int i=2;i<=n;i++)
			x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p:++p;
	}
	for(int i=1;i<=n;i++)
		rk[sa[i]]=i;
	for(int i=1,j,k=0;i<=n;he[rk[i++]]=k)
		for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
	// for(int i=1;i<=n;i++)
	// {cerr<<he[i]<<endl;
		// for(int j=sa[i];j<=n;j++)
			// cerr<<r[j]<<" ";
		// cerr<<endl;
	// }
}
bool ok(int w)
{//cerr<<w<<endl;
	for(int i=2,la;i<=n;i=la+1)
	{
		int mn=sa[i-1],mx=sa[i-1];
		la=i;
		while(he[la]>=w)
			mn=min(mn,sa[la]),mx=max(mx,sa[la]),la++;
		// cerr<<mn<<" "<<mx<<endl;
		if(mx-mn>=w)
			return 1;
	}
	return 0;
}
int main()
{
	while(scanf("%d",&n)&&n)
	{
		for(int i=1;i<=n;i++)
			a[i]=read();
		for(int i=1;i<n;i++)
			r[i]=a[i+1]-a[i]+88;
		saa(r,n-1,200);
		int l=0,r=n,ans=0;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(ok(mid))
				l=mid+1,ans=mid;
			else
				r=mid-1;
		}//cerr<<ans<<endl;
		printf("%d\n",ans>=4?ans+1:0);
	}
	return 0;
}
												
											poj 1743 Musical Theme【二分+SA】的更多相关文章
- POJ 1743 Musical Theme 二分+后缀数组
		
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...
 - Poj  1743 Musical Theme (后缀数组+二分)
		
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
 - POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
		
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
 - poj 1743 Musical Theme(最长重复子串 后缀数组)
		
poj 1743 Musical Theme(最长重复子串 后缀数组) 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复 ...
 - POJ 1743 Musical Theme 【后缀数组 最长不重叠子串】
		
题目冲鸭:http://poj.org/problem?id=1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Su ...
 - POJ 1743 Musical Theme (字符串HASH+二分)
		
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
 - Poj 1743 Musical Theme(后缀数组+二分答案)
		
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
 - POJ 1743 Musical Theme 后缀数组 最长重复不相交子串
		
Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description ...
 - poj 1743 Musical Theme (后缀数组+二分法)
		
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16162 Accepted: 5577 De ...
 - Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】
		
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22499 Accepted: 7679 De ...
 
随机推荐
- weexapp  开发流程(一)开发环境配置
			
1.创建项目 weexpack create weexapp 2.安装必要插件 npm i jwt-simple vue-resource vue-router vuex vuex-router-sy ...
 - SQL server 子查询的应用
			
设置主键关联表格 时间函数的应用
 - JS基础——构造函数VS原型
			
JS是一种基于对象的语言.在使用过程中不免遇到复制对象的问题.但通常我们採用的直接赋值'obj1=obj2'这样的做法会出现数据覆盖问题. 也就是对象引用过程中引用地址一致.导致对象数据被改动的问题. ...
 - 服务器----1U、2U、3U、4U
			
U是一种表示服务器外部尺寸的单位,是unit的缩略语,详细的尺寸由作为业界团体的美国电子工业协会(EIA)所决定. 之所以要规定服务器的尺寸,是为了使服务器保持适当的尺寸以便放在铁质或铝质的机架上.机 ...
 - vim note (1)
			
'vim' go into the vim mode 'i' 'a' 's' is means insert mode 'v' is means visual mode 'esc' is mea ...
 - WTF
			
WTF ,luna黑色主题比较sublime 还是差点!
 - MyCAT简单入门配置
			
MyCAT简单入门配置 安装jdk 建议1.7以上 安装mysql 安装MyCAT Mycat 源码:https://github.com/MyCATApache/Mycat-Server Mycat ...
 - redis.Pool 配置
			
http://blog.csdn.net/xiaohu50/article/details/51606349
 - YTU 2421: C语言习题 矩形法求定积分
			
2421: C语言习题 矩形法求定积分 时间限制: 1 Sec 内存限制: 128 MB 提交: 354 解决: 234 题目描述 写一个用矩形法求定积分的通用函数,分别求 (说明: sin,co ...
 - 两种 NIO 实现:Selector 与 Epoll
			
[总结]两种 NIO 实现:Selector 与 Epoll 时间2012-11-17 08:38:42 开源中国新闻原文 http://my.oschina.net/ielts0909/blog/ ...