POJ1743 Musical Theme (后缀数组 & 后缀自动机)最大不重叠相似子串
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 last test case is followed by one zero.
Output
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
题解:最大不重叠相似子串。
参考代码:
#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 (后缀数组 & 后缀自动机)最大不重叠相似子串的更多相关文章
- POJ1743 Musical Theme(二分+后缀数组)
题目大概是给n个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 这题是传说中楼教主男人八题之一,虽然已经是用后缀数组解决不可重叠最 ...
- poj 1743 后缀数组 求最长不重叠重复子串
题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. “主题”是整个音符序列的一个子串,它需要满足如下条件:1 ...
- Musical Theme - poj 1743(求最大不重叠重复子串)
题目大意: * 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题. * “主题”是整个音符序列的一个子串,它需要满 ...
- POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Tot ...
- 字符串的模板 Manacher kmp ac自动机 后缀数组 后缀自动机
为何scanf("%s", str)不需要&运算 经常忘掉的字符串知识点,最好不加&,不加&最标准,指针如果像scanf里一样加&是错的,大概是未定 ...
- 【整理】如何选取后缀数组&&后缀自动机
后缀家族已知成员 后缀树 后缀数组 后缀自动机 后缀仙人掌 后缀预言 后缀Splay ? 后缀树是后缀数 ...
- loj6173 Samjia和矩阵(后缀数组/后缀自动机)
题目: https://loj.ac/problem/6173 分析: 考虑枚举宽度w,然后把宽度压位集中,将它们哈希 (这是w=2的时候) 然后可以写一下string=“ac#bc” 然后就是求这个 ...
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- poj 3693 后缀数组 重复次数最多的连续重复子串
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8669 Acc ...
- 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)
议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...
随机推荐
- Apache Spark 3.0 预览版正式发布,多项重大功能发布
2019年11月08日 数砖的 Xingbo Jiang 大佬给社区发了一封邮件,宣布 Apache Spark 3.0 预览版正式发布,这个版本主要是为了对即将发布的 Apache Spark 3. ...
- IDEA中WEB项目本地调试和发布的配置分开配置
一个Web项目,开发的时候设置了一些本地内容,比如IP地址,还有本地目录等.开发完成后,要发布到服务器上时,这些本地相关的配置,就需要配置成服务器上IP或目录. 原先的做法就是部署打包的时候,把相关的 ...
- Look into Bitmap images
What's a Bitmap image? I'm not going to explain the differences between raster and vector images, no ...
- [JQuery] JQuery学习笔记
1.2019年10月20日14:43:48 学习HOW2J. 2.JQuery是一个javascript的框架,是对javascript的一种封装, 通过JQuery可以非常方便的操作html的元素 ...
- vue项目iframe的传值问题
前言 项目需要,我需要引入一个已经封装好的浏览器插件.插件只能以html的方式调用, 所以.我把插件的使用封装了一个html页面.vue项目则利用iframe的方式引入. 到这里我就遇到了一个问题,那 ...
- 详解JavaScript错误捕获和上报流程
怎么捕获错误并且处理,是一门语言必备的知识.在JavaScript中也是如此. 那怎么捕获错误呢?初看好像很简单,try-catch就可以了嘛!但是有的时候我们发现情况却繁多复杂. Q1: 同步可以t ...
- Test Complete 的自动化测试 --- 三角形
Test Complete 的自动化测试 --- 三角形 PS:工具:Test Complete , OS:win 10 这里做三角形的测试与上一篇博客做计算器的测试大致一样,都是对.exe的执行文件 ...
- ubunit 16 安装pip
pip是一个用来安装和管理python包的工具.已经内置到python2.7.9和python3.4及其以上的版本里. python2.7执行: sudo apt-get install python ...
- 在lldb调试中调用c++函数
在lldb调试时,调用oc对象的方法不足为奇,因为msgSend是有原型导出的,oc对象的方法都运行期绑定的,绑定信息都在objc_class中.只要在调试中[receiver sel]之类,lldb ...
- 你的文章里为什么不放源码Github链接了
"你的文章里为什么不放源码Github链接了?",一个读者这么问我 我把这张图发给了他,这是我之前放文章中Demo源码的Github仓库 他一脸疑惑,问我怎么了 经常使用Githu ...