POJ 1743. Musical Theme

Description

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.

题意大概就是取差值求一遍不重复的字串,唯一就是至少长度为5,而且重复字串不能连续(因为连续了说明原数组重叠了)。

#include <iostream>
#include <cstring>
#include <cstdio>
const int N = 20000 + 7;
using namespace std;
int n,r[N],wb[N],wss[N],wv[N],sa[N],rank[N],li[N];

int cmp(int *r,int a,int b,int l)
{
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void Da(int n,int m)
{
    int i,j,p,*x = rank, *y = wb, *t;
    for(i = 0; i < m; ++i) wss[i] = 0;
    for(i = 0; i < n; ++i) ++wss[x[i]=r[i]];
    for(i = 1; i < m; ++i) wss[i] += wss[i-1];
    for(i = n-1; i >= 0; --i ) sa[--wss[x[i]]] = i;
    for(j = p = 1; p < n; j <<= 1, m = p)
    {
        for(p = 0,i = n-j; i < n; ++i) y[p++] = i;
        for(i = 0; i < n; ++i) if(sa[i] >= j) y[p++] = sa[i] - j;
        for(i = 0; i < n; ++i) wv[i] = x[y[i]];
        for(i = 0; i < m; ++i) wss[i] = 0;
        for(i = 0; i < n; ++i) ++wss[wv[i]];
        for(i = 1; i < m; ++i) wss[i] += wss[i-1];
        for(i = n-1; i >= 0; --i) sa[--wss[wv[i]]] = y[i];
        for(t = x, x = y, y = t, p = i = 1, x[sa[0]] = 0; i < n; ++i)
        x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    }
}

int f[N], hi[N];
void cal_height( )
{
    int i,j,k = 0;
    for(i = 1; i <= n; ++i) f[sa[i]] = i;
    for(i = 0; i < n; hi[f[i++]] = k)
    for(k?k--:0,j = sa[f[i]-1]; r[i+k] == r[j+k]; ++k);
}

bool check(int len)
{
    int minx = n + 1 , maxx = -1,i;
    for(i = 2; i <= n; ++i)
    {
        if(hi[i] >= len)
        {
            minx = min(minx,min(sa[i],sa[i-1]));
            maxx = max(maxx,max(sa[i],sa[i-1]));
        }
        else
        {
            if(minx + len < maxx) return true;
            minx = n + 1, maxx = -1;
         }
    }
    if(minx + len < maxx) return true; return false;
}

int  binary_search( )
{
    int L = 1, R =  ( n >> 1 )+ 1, mid, ret = -1;
    while(L <= R)
    {
        mid = ( L + R ) >> 1;
        if(check(mid)) L = mid + 1, ret = max(ret , mid);
        else R = mid - 1;
    }
    return ret;
}

int main()
{
    while(~scanf("%d",&n) && n)
    {
        int i; memset(r,0,sizeof(r));
        for(i = 0; i < n; ++i) scanf("%d",r+i);
        for(i = 0; i < n; ++i) r[i] = r[i+1] - r[i] + 100; ; n--; r[n] = 0;
        Da(n+1,201);
        cal_height( );
        int ans = binary_search()+1;
        if( ans < 5) puts("0");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-21 14:31:28

POJ 1743. Musical Theme的相关文章

POJ 1743 Musical Theme (后缀数组)

题目大意: 刚才上88个键弹出来的音符. 如果出现重复的,或者是高一个音阶的重复的都算. 思路分析: 具体可以参考训练指南222. height数组表示按照排序后的sa最近的两个后缀的最长前缀. 将height 分块.然后二分答案,二分答案之后去判断是否满足. 要考虑到不重合,还有大于5. 所以二分的时候要从5开始,然后判断的时候要加一个 up - down >len #include <cstdio> #include <iostream> #include <alg

Poj 1743 Musical Theme (后缀数组+二分)

题目链接: Poj  1743 Musical Theme 题目描述: 给出一串数字(数字区间在[1,88]),要在这串数字中找出一个主题,满足: 1:主题长度大于等于5. 2:主题在文本串中重复出现(或者经过调转出现,调转是主题同时加上或者减去同一个整数) 3:重复主题不能重叠 解题思路: 求调转重复出现的子串,那么主题之间的差值一定是不变的.可以求文本串s中相邻两个数的差值,重新组成一个新的文本串S,然后找S后缀串中最长公共不重叠前缀.rank相邻的后缀串,公共前缀一定最长,但是有可能重叠.

poj 1743 Musical Theme(男人八题&amp;后缀数组第一题)

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17298   Accepted: 5939 Description 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

POJ 1743 Musical Theme 后缀数组 最长重复不相交子串

Musical ThemeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1743 Description 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

poj 1743 Musical Theme(后缀数组)

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30544   Accepted: 10208 Description 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

Poj 1743——Musical Theme——————【后缀数组模板题】

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22499   Accepted: 7679 Description 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

POJ 1743 Musical Theme【SAM】

POJ1743 Musical Theme 要找长度\(\ge 5\)且出现次数\(\ge 2\)并且第一次出现和最后一次出现不重叠的最长子串. 题目条件中,如果对于两个串,在一个串的每个数上都加上相同的数之后可以得到另一个串,那么这个两个串可以被是相同的. 首先我们先得到差分数组,然后要求的就是差分数组中长度\(\ge 4\)且出现次数\(\ge 2\)并且第一次出现和最后一次出现不重叠的最长子串 我们需要知道的是每个等价类中终点的最左端和最右端的位置,即(\(firstpos,lastpos

POJ 1743 Musical Theme(后缀数组+二分答案)

[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, 并且长度不小于5的最长的曲段,主旋律出现的时候并不是完全一样的, 可能经过了升调或者降调,也就是说, 是原来主旋律所包含的数字段同时加上或者减去一个数所得, 当然,两段主旋律之间也是不能有重叠的,现在请你求出这首曲子主旋律的长度, 如果不存在请输出0. [题解] 首先要处理的是升调和降调的问题,由

POJ - 1743 Musical Theme (后缀数组求不可重叠最长重复子串)

Description 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 music