A - Musical Theme + 二分
#include <bits/stdc++.h>
using namespace std; #define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 20005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank1[N],height[N],s[N],a[N],n;
char str1[N],str2[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包含末尾添加的0
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) wsf[i]=;
for(i=; i<n; i++) wsf[x[i]=r[i]]++;
for(i=; i<m; i++) wsf[i]+=wsf[i-];
for(i=n-; i>=; i--) sa[--wsf[x[i]]]=i;
p=;
j=;
for(; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++) y[p++]=i;
for(i=; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=; i<n; i++) wv[i]=x[y[i]];
for(i=; i<m; i++) wsf[i]=;
for(i=; i<n; i++) wsf[wv[i]]++;
for(i=; i<m; i++) wsf[i]+=wsf[i-];
for(i=n-; i>=; i--) sa[--wsf[wv[i]]]=y[i];
t=x;
x=y;
y=t;
x[sa[]]=;
for(p=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)? p-:p++;
}
}
void getheight(int *r,int n)//n不保存最后的0
{
int i,j,k=;
for(i=; i<=n; i++) rank1[sa[i]]=i;
for(i=; i<n; i++)
{
if(k)
k--;
else
k=;
j=sa[rank1[i]-];
while(r[i+k]==r[j+k])
k++;
height[rank1[i]]=k;
}
}
int ans;
int fun(int k)
{
int i,maxn,minn;
maxn = minn = sa[];
UP(i,,n)
{
if(height[i]>=k && i<n)
{
minn = min(minn,sa[i]);
maxn = max(maxn,sa[i]);
continue;
}
if(maxn-minn>=k) return ;
maxn = minn = sa[i];
}
return ;
} int main()
{
int i,j,k;
W((~scanf("%d",&n),n))
{
UP(i,,n-)
{
scanf("%d",&s[i]);
}
UP(i,,n-)
{
s[i] = s[i+]-s[i]+;
}
s[--n] = ;
getsa(s,sa,n+,);
getheight(s,n);
int l = ,r = n;
W(l<=r)
{
int mid = (l+r)/;
if(fun(mid))
{
ans = mid;
l=mid+;
}
else r = mid-;
}
ans++;
printf("%d\n",ans<?:ans);
} return ;
}
A - Musical Theme + 二分的更多相关文章
- POJ 1743 Musical Theme 二分+后缀数组
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)no ...
- 【POJ1743】 Musical Theme (二分+后缀数组)
Musical Theme Description A musical melody is represented as a sequence of N (1<=N<=20000)note ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj1743 Musical Theme【后缀数组】【二分】
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 35044 Accepted: 11628 D ...
- POJ-1743 Musical Theme,后缀数组+二分!
Musical Theme 人生第一道后缀数组的题,采用大众化思想姿势极其猥琐. 题意:给你n个 ...
- Poj 1743 Musical Theme (后缀数组+二分)
题目链接: Poj 1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现 ...
- Poj 1743 Musical Theme(后缀数组+二分答案)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 28435 Accepted: 9604 Descri ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...
随机推荐
- mongodb学习之:条件操作符
在前面的章节中我们已经有用到了条件操作符,这里我们再重点介绍下.MongoDB中条件操作符有: (>) 大于 - $gt (<) 小于 - $lt (>=) 大于等于 - $gte ...
- wait()和notify()
从https://www.cnblogs.com/toov5/p/9837373.html 可以看到他的打印是一片一片的,这边博客介绍怎么避免掉 使用notify 和 wait的时候 要注意 是在sy ...
- CI 模型公用查询函数
/** * 多字段条件查询数据 * @param array $val array("name" => $value).name为要操作的字段,value为要操作的值 * @ ...
- 【转】澄清P问题、NP问题、NPC问题
首先,原文链接.(这篇文章让我第一次有了感谢腾讯,感谢微信,感谢微信公众号的冲动.总之,非常感谢作者的分享.) 然后:结论图如下 担心万一哪天原网站把这篇文章下线,所以原文内容复制过来. 澄清P问题. ...
- Can't locate Log/Dispatch.pm in @INC
记录一下配置mha的时候遇到的错误,使用perl模块发送邮件的时候报以下错误: # masterha_check_ssh --conf=/data/mha/app1.cnf Can't locate ...
- 洛谷P3385判负环——spfa
题目:https://www.luogu.org/problemnew/show/P3385 两种方法,dfs和bfs: 一开始写的dfs,要把dis数组初值赋成0,这样从一个连着负边的点开始搜: 在 ...
- HDU1496(巧妙hash)
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- python -m json.tool 中文乱码 Format JSON with python
现在以 json 为数据传输格式的 RESTful 接口非常流行.为调试这样的接口,一个常用的办法是使用 curl 命令: curl http://somehost.com/some-restful- ...
- 【原】Cache Buffer Chain 第四篇
作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/p/3873357.html [测试1]低 ...
- StackOverFlow页面不正常,因为CDN被墙了
190.93.247.58 cdn.sstatic.net 198.252.206.140 sstatic.net http://stackoverflow.com/