A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range 1..88, each representing a key on the piano. It is unfortunate but true that this representation of melodies ignores the notion of musical timing; but, this programming task is about notes and not timings. 
Many composers structure their music around a repeating &qout;theme&qout;, which, being a subsequence of an entire melody, is a sequence of integers in our representation. A subsequence of a melody is a theme if it:

  • is at least five notes long
  • appears (potentially transposed -- see below) again somewhere else in the piece of music
  • is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s)

Transposed means that a constant positive or negative value is added to every note value in the theme subsequence. 
Given a melody, compute the length (number of notes) of the longest theme. 
One second time limit for this problem's solutions!

Input

The input contains several test cases. The first line of each test case contains the integer N. The following n integers represent the sequence of notes. 
The last test case is followed by one zero. 

Output

For each test case, the output file should contain a single line with a single integer that represents the length of the longest theme. If there are no themes, output 0.

Sample Input

30
25 27 30 34 39 45 52 60 69 79 69 60 52 45 39 34 30 26 22 18
82 78 74 70 66 67 64 60 65 80
0

Sample Output

5

Hint

Use scanf instead of cin to reduce the read time.
 

题解:最大不重叠相似子串。

先相邻元素做差,得到字符串。
 
后缀数组:利用后缀数组的height数组的性质,字典序相邻的字符串的相似度是最大的。
然后二分答案len,找相邻的height数组大于等于len的最大长度,如果最大长度>=k-1;
则是满足题意的答案,否则输出0;
 
后缀自动机:后缀自动机是利用endpos等价类的性质,对于每一个等价类,我们分别求出该类的
最大长度所在的位置和最小长度所在的位置,然后如果这两个位置的差不小于longest[i],则满足题意,
在这些满足题意的值中去最大值即可。
 
 

参考代码:

 
SA(后缀数组)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=;
int n,s[maxn];
int rk[maxn],sa[maxn],height[maxn];
int x[maxn<<],y[maxn<<],c[maxn]; inline void get_SA(int m)
{
for(int i=;i<=m;++i) c[i]=;
for(int i=;i<=n;++i) ++c[x[i]=s[i]];
for(int i=;i<=m;++i) c[i]+=c[i-];
for(int i=n;i>=;--i) sa[c[x[i]]--]=i;
for(int k=;k<=n;k<<=)
{
int num=;
for(int i=n-k+;i<=n;++i) y[++num]=i;
for(int i=;i<=n;++i) if(sa[i]>k) y[++num]=sa[i]-k;
for(int i=;i<=m;++i) c[i]=;
for(int i=;i<=n;++i) ++c[x[i]];
for(int i=;i<=m;++i) c[i]+=c[i-];
for(int i=n;i>=;--i) sa[c[x[y[i]]]--]=y[i],y[i]=;
swap(x,y);
x[sa[]]=;
num=;
for(int i=;i<=n;++i)
x[sa[i]]=(y[sa[i]]==y[sa[i-]]&&y[sa[i]+k]==y[sa[i-]+k])?num:++num;
if(num==n) break;
m=num;
}
}
inline void get_height()
{
int k=;
for(int i=;i<=n;++i) rk[sa[i]]=i;
for(int i=;i<=n;++i)
{
if(rk[i]==) continue;
if(k) --k;
int j=sa[rk[i]-];
while(j+k<=n&&i+k<=n&&s[i+k]==s[j+k]) ++k;
height[rk[i]]=k;
}
}
bool check(int k)
{
int mx=-INF,mi=INF;
for(int i=;i<=n;++i)
{
if(height[i]>=k)
{
mx=max(mx,max(sa[i],sa[i-]));
mi=min(mi,min(sa[i],sa[i-]));
if(mx-mi>k) return true;
}
else mx=-INF,mi=INF;
}
return false;
} int main()
{
while(scanf("%d",&n) && n)
{
int pre,now; n--;
scanf("%d",&pre);
for(int i=;i<=n;++i) scanf("%d",&now),s[i]=now-pre+,pre=now;
get_SA();
get_height(); int l=,r=n>>;
while(l+<r)
{
int mid=l+r>>;
if(check(mid)) l=mid;
else r=mid;
}
int ans;
if(check(r)) ans=r;
else ans=l;
printf("%d\n",ans>=? ans+:);
} return ;
}

后缀自动机

/********* 后缀自动机做法 ***********/
#include<map>
#include<ctime>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define inf 1000000000
#define mod 1000000007
#define pa pair<int,int>
#define ll long long
using namespace std;
int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,a[];
struct sam{
int last,cnt,ans;
int l[],r[],mx[],fa[],a[][];
int q[],v[];
void init()
{
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(v,,sizeof(v));
memset(mx,,sizeof(mx));
memset(fa,,sizeof(fa));
memset(a,,sizeof(a));
last=cnt=;ans=;
}
void extend(int c)
{
int p=last,np=last=++cnt;mx[np]=mx[p]+;
l[np]=r[np]=mx[np];
while(!a[p][c]&&p)a[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else
{
int q=a[p][c];
if(mx[p]+==mx[q]) fa[np]=q;
else
{
int nq=++cnt;mx[nq]=mx[p]+;
memcpy(a[nq],a[q],sizeof(a[q]));
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
while(a[p][c]==q) a[p][c]=nq,p=fa[p];
}
}
}
void solve()
{
for(int i=;i<=cnt;i++) v[mx[i]]++;
for(int i=;i<=n;i++) v[i]+=v[i-];
for(int i=cnt;i;i--) q[v[mx[i]]--]=i;
for(int i=cnt;i;i--)
{
int p=q[i];
l[fa[p]]=min(l[fa[p]],l[p]);
r[fa[p]]=max(r[fa[p]],r[p]);
}
for(int i=;i<=cnt;i++)
ans=max(ans,min(mx[i],r[i]-l[i]));
if(ans<)puts("");
else printf("%d\n",ans+);
}
} sam;
int main()
{
while(scanf("%d",&n))
{
if(n==)break;
for(int i=;i<=n;i++) a[i]=read();n--;
for(int i=;i<=n;i++) a[i]=a[i+]-a[i]+;
sam.init();
for(int i=;i<=n;i++)
sam.extend(a[i]);
sam.solve();
}
return ;
}

POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串的更多相关文章

  1. POJ1743 Musical Theme(二分+后缀数组)

    题目大概是给n个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 这题是传说中楼教主男人八题之一,虽然已经是用后缀数组解决不可重叠最 ...

  2. poj 1743 后缀数组 求最长不重叠重复子串

    题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...

  3. Musical Theme - poj 1743(求最大不重叠重复子串)

    题目大意: * 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. * “主题”是整个音符序列的一个子串,它需要满 ...

  4. POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Tot ...

  5. 字符串的模板 Manacher kmp ac自动机 后缀数组 后缀自动机

    为何scanf("%s", str)不需要&运算 经常忘掉的字符串知识点,最好不加&,不加&最标准,指针如果像scanf里一样加&是错的,大概是未定 ...

  6. 【整理】如何选取后缀数组&&后缀自动机

    后缀家族已知成员         后缀树         后缀数组         后缀自动机         后缀仙人掌         后缀预言         后缀Splay ? 后缀树是后缀数 ...

  7. loj6173 Samjia和矩阵(后缀数组/后缀自动机)

    题目: https://loj.ac/problem/6173 分析: 考虑枚举宽度w,然后把宽度压位集中,将它们哈希 (这是w=2的时候) 然后可以写一下string=“ac#bc” 然后就是求这个 ...

  8. 利用后缀数组(suffix array)求最长公共子串(longest common substring)

    摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...

  9. poj 3693 后缀数组 重复次数最多的连续重复子串

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8669   Acc ...

  10. 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)

    议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...

随机推荐

  1. Electron 菜单切换主题与css替换 ts编写

    ////目标css<link rel="stylesheet" id="theme_css" href="路径"> ////ts ...

  2. nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...

  3. 理解Spark SQL(二)—— SQLContext和HiveContext

    使用Spark SQL,除了使用之前介绍的方法,实际上还可以使用SQLContext或者HiveContext通过编程的方式实现.前者支持SQL语法解析器(SQL-92语法),后者支持SQL语法解析器 ...

  4. 虚拟机和容器docker

    云计算中最主要的技术就是虚拟机,开源虚拟机已经kvm已经集成到Linux内核!针对虚拟机浪费资源(CPU.内存.存储等)较大的缺陷,google力推Docker容器和容器管理平台Kubernetes. ...

  5. ACE框架 基于共享内存的分配器 (算法设计)

    继承上一篇<ACE框架 基于共享内存的分配器设计>,本篇分析算法部分的设计. ACE_Malloc_T模板定义了这样一个分配器组件 分配器组件聚合了三个功能组件:同步组件ACE_LOCK, ...

  6. 用例建模Use Case Modeling

    我的工程实践选题为ESP32低功耗的实现,本项目基于ESP32嵌入式开发平台. 以此题为例,在理解项目需求的基础上进行用例建模,抽取Abstract use case,画出用例图,并确定每一个用例的范 ...

  7. 使用sklearn和caffe进行逻辑回归 | Brewing Logistic Regression then Going Deeper

    原文首发于个人博客https://kezunlin.me/post/c50b0018/,欢迎阅读! Brewing Logistic Regression then Going Deeper. Bre ...

  8. IPv6,无需操作就可升级?

    最近这段时间,5G 出现在你能看到的各种信息里,铺天盖地的宣传提醒着大家新一代互联网的到来.其实早在几年前 5G 就有所提及,可是为什么到现在才开始窜上热门呢?这就涉及到了 IPv6. 或许有不少朋友 ...

  9. [ch02-03] 梯度下降

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 2.3 梯度下降 2.3.1 从自然现象中理解梯度下降 ...

  10. 【Android - 问题解决】之ScrollView嵌套ListView时总是自动滑动到ListView顶部的问题

    最近做了一个项目,里面有一个ScrollView嵌套ListView的布局. 做出来之后发现,进入这个界面之后,总是自动滑动到ListView的顶部,而ScrollView中位于ListView上面的 ...