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) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...
随机推荐
- jdk 错误1316 指定账户已存在 与 jdk1.7安装和配置环境变量 与 jdk1.8与1.7版本的切换使用
问题: 安装JDK,提示错误信息:,指定的账号已存在. 原因: 安装JDK,相当于安装了一个软件,要使用系统的软件卸载功能卸载,不能只删除安装目录文件夹下的文件,如果只 ...
- Spring简单的示例
参考资料:https://how2j.cn/k/spring/spring-ioc-di/87.html.https://www.w3cschool.cn/wkspring/dgte1ica.html ...
- Cache地址映射
原理:程序访问局部性 在较短时间内由程序产生的地址往往集中在存储器逻辑地址空间的很小范围内 时间:在一小段时间内,最近被访问过的程序和数据很可能再次被访问 ...
- 一个ip, 两个域名, 两个ssl, 访问多个不同的项目
在前面一篇中说过, 入了好几个坑. 后来使用了nginx+tomcat配置的方式. 终于成功了. 因为头一次使用nginx, 不知道具体怎么操作, 于是我在操作的时候, 按照以下几个步骤执行的: 导航 ...
- pat 1027 Colors in Mars(20 分)
1027 Colors in Mars(20 分) People in Mars represent the colors in their computers in a similar way as ...
- Python3.7.1学习(七)mysql中pymysql模块详解(一)
pymysql是纯用Python操作MySQL的模块,其使用方法和MySQLdb几乎相同.此次介绍mysql以及在python中如何用pymysql操作数据库, 以及在mysql中存储过程, 触发器以 ...
- 【并发编程】Java对并发编程的支持历史
本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 本文转载,原文请点击链接 本章主要对Java并发(Con ...
- SCAU1143 多少个Fibonacci数--大菲波数【杭电-HDOJ-1715】--高精度加法--Fibonacci数---大数比较
/*******对读者说(哈哈如果有人看的话23333)哈哈大杰是华农的19级软件工程新手,才疏学浅但是秉着校科联的那句“主动才会有故事”还是大胆的做了一下建一个卑微博客的尝试,想法自己之后学到东西都 ...
- day 23 面向对象中类的成员 和嵌套
1.类的成员? 变量.方法.属性 class Foo: # 方法 def __init__(self,name): # 实例变量/字段 self.name = name # 方法 def func(s ...
- 关于Go defer的详细使用
先抛砖引玉defer的延迟调用:defer特性: . 关键字 defer 用于注册延迟调用. . 这些调用直到 return 前才被执.因此,可以用来做资源清理. . 多个defer语句,按先进后出的 ...