题目大意:

* 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题。 * “主题”是整个音符序列的一个子串,它需要满足如下条件: * 1.长度至少为5个音符 * 2.在乐曲中重复出现(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值。) * 3.重复出现的同一主题不能有公共部分。

 

分析:也是看的论文才做的这道题,学了4天的后缀数组,终于A掉一道题了,啥也不说了,都是眼泪(怎么做可以看看论文,说的很详细)。

 

代码如下:

===============================================================================================================

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = 2e4+;
const int BaseNum = ; struct SuffixArr
{
int text[MAXN], tempx[MAXN], tempy[MAXN];
int sum[MAXN], rank[MAXN], sa[MAXN], heigh[MAXN];
int *x, *y, N, MaxId; void Insert(int mus[], int len)
{
N = len, MaxId = ;
x = tempx, y=tempy; for(int i=; i<N; i++)
{
x[i] = text[i] = mus[i];
y[i] = i;
}
}
void BaseSort()
{
for(int i=; i<MaxId; i++)
sum[i] = ;
for(int i=; i<N; i++)
sum[ x[ y[i] ] ] += ;
for(int i=; i<MaxId; i++)
sum[i] += sum[i-];
for(int i=N-; i>=; i--)
sa[ --sum[ x[ y[i] ] ] ] = y[i];
}
bool OK(int i, int len)
{
if(sa[i]+len>N || sa[i-]+len>N)
return false;
if(y[sa[i]] != y[sa[i-]] || y[sa[i]+len] != y[sa[i-]+len])
return false; return true;
}
void Build_Sa()
{
BaseSort(); for(int len=; len<N; len<<=)
{
int id = ; for(int i=N-len; i<N; i++)
y[id++] = i;
for(int i=; i<N; i++)if(sa[i]>=len)
y[id++] = sa[i] - len; BaseSort();
swap(x, y); x[ sa[] ] = id = ; for(int i=; i<N; i++)
{
if(OK(i, len) == true)
x[ sa[i] ] = id;
else
x[ sa[i] ] = ++id;
} MaxId = id+; if(MaxId >= N)break;
}
}
void GetHeight()
{
for(int i=; i<N; i++)
rank[ sa[i] ] = i; for(int k=,i=; i<N; i++)
{
if(!rank[i])
{
k = heigh[] = ;
continue;
}
if(k)k--; int pre = sa[ rank[i]- ]; while(text[pre+k] == text[i+k])
k++;
heigh[rank[i]] = k;
}
}
}; struct SuffixArr suf;
int music[MAXN]; bool Find(int k)
{
int Min, Max; Min = Max = suf.sa[]; for(int i=; i<suf.N; i++)
{
if(suf.heigh[i] < k)
Min = Max = suf.sa[i];
else
{
Min = min(Min, suf.sa[i]);
Max = max(Max, suf.sa[i]); if(Max-Min > k)
return true;
}
} return false;
} int main()
{
int i, N; while(scanf("%d", &N), N)
{
for(i=; i<N; i++)
{
scanf("%d", &music[i]);
if(i)music[i-] = music[i-]-music[i]+BaseNum;
}
music[N-] = ; suf.Insert(music, N);
suf.Build_Sa();
suf.GetHeight(); int L=, R=N/, ans=-; while(L <= R)
{
int Mid = (L+R)>>; if(Find(Mid) == true)
{
L = Mid + ;
ans = Mid;
}
else
R = Mid - ;
} printf("%d\n", ans+);
} return ;
}

Musical Theme - poj 1743(求最大不重叠重复子串)的更多相关文章

  1. POJ 1743 Musical Theme ( 后缀数组 && 最长不重叠相似子串 )

    题意 : 给 n 个数组成的串,求是否有多个“相似”且不重叠的子串的长度大于等于5,两个子串相似当且仅当长度相等且每一位的数字差都相等. 分析 :  根据题目对于 “ 相似 ” 串的定义,我们可以将原 ...

  2. Musical Theme POJ - 1743(后缀数组+二分)

    求不可重叠最长重复子串 对于height[i]定义为sa[i]和 sa[i-1]的最长公共前缀 这个最长公共前缀的值肯定是最大的 证明: 设rank[j] < rank[k], 则不难证明后缀j ...

  3. Musical Theme POJ - 1743 后缀数组

    A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the ...

  4. poj 1961 (求字符串中的重复子串)

    Sample Input 3aaa12aabaabaabaab0Sample Output Test case #12 23 3 Test case #22 2 //aa有2个a6 2 //aabaa ...

  5. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)

    永恒的大牛,kuangbin,膜拜一下,Orz 链接:http://www.cnblogs.com/kuangbin/archive/2013/04/23/3039313.html Musical T ...

  6. poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)

    题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...

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

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

  8. ACdream 1430——SETI——————【后缀数组,不重叠重复子串个数】

    SETI Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statist ...

  9. hdu_2668 Daydream O(n)求最长不重复子串

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668 Daydream Time Limit: 2000/1000 MS (Java/Others)  ...

随机推荐

  1. linux命令sed学习笔记

    sed其实就是两个主要的知识点,那就是“怎么选择”和“怎么操作”!

  2. boost::thread 线程锁

    1.boost锁的概述: boost库中提供了mutex类与lock类,通过组合可以轻易的构建读写锁与互斥锁. 2.mutex对象类(主要有两种): 1.boost::mutex(独占互斥类) --& ...

  3. C++ 类访问控制(public/protected/private)

    第一:private, public, protected 访问标号的访问范围. private:只能由1.该类中的函数.2.其友元函数访问. 不能被任何其他访问,该类的对象也不能访问. protec ...

  4. PHP实现中文字串截取无乱码的方法

    直接使用PHP函数substr截取中文字符可能会出现乱码,主要是substr可能硬生生的将一个中文字符“锯”成两半.解决办法: 1.使用mbstring扩展库的mb_substr截取就不会出现乱码了. ...

  5. jquery 中 form的使用

    纠结了一下 form 表单的提交响应事件,因为在表单中,form标签会让浏览器自动提交,而我一直写的是 $(".btn").click(function()) 提交按钮的响应事件, ...

  6. yarn源代码

    Modules-------YARN consists of multiple modules. The modules are listed below as per the directory s ...

  7. 一个c++给程序打log的单例模式类

    开发过程中需要给程序打log. 所以照着网上写了个单例模式的log类 #ifndef MISCLOGWRITER_H_ #define MISCLOGWRITER_H_ #include <io ...

  8. codeforces 361 C - Mike and Chocolate Thieves

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Bad ...

  9. Label设置行间距--b

    内容摘要 UILabel显示多行文本 UILabel设置行间距 解决单行文本 & 多行文本显示的问题 场景描述 众所周知,UILabel显示多行的话,默认行间距为0,但实际开发中,如果显示多行 ...

  10. c# 把 颜色值Hex 转换为 Color

    原文: http://abujj.me/archives/695 Assuming you mean the HTML type RGB codes (called Hex codes, such a ...