【后缀数组】【poj1743】音乐主题

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 16197 Accepted: 5585

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.

Source




= =读入看错。。调了半天。。

 

【基本思路】求一段字串的最长不可重叠的重复子串+差分预处理+二分答案

关于差分> >如果两端序列,各自的每个数之间的差值相等,那么必定可以通过加减来使序列一变成序列二,注意答案要加一

 

const maxn=20005;
varrank,sa,trk,tsa,sum,h,s:array[0..maxn]of longint;
    i,j,m,n,p,l,r,mid:longint;
 
 
function check(ans:longint):boolean;
var mina,maxa,i:longint;
begin
  mina:=sa[1]; maxa:=sa[1];
  for i:=2 to n do
    begin
      ifh[i]>=ans then  //每组的H[I]>=ans 进行分组
        begin
          ifsa[i]
          ifsa[i]>maxa then maxa:=sa[i];
        end
         elsebegin
                ifmaxa-mina>ans then exit(true);//找到一组就退掉
                mina:=sa[i];maxa:=sa[i];
              end;
    end;
    if maxa-mina>ansthen exit(true);
    exit(false);
end;
 
begin
// assign(input,'input.txt'); reset(input);
 //assign(output,'output.txt');rewrite(output);
 repeat
   readln(n);
   if n=0 then break;
   for i:=1 to n doread(s[i]);
   if (n<10) then
      begin
        writeln(0);
        continue;
      end;
   fillchar(sum,sizeof(sum),0);
     for i:=2 to ndo
       begin
        s[i-1]:=s[i]-s[i-1]+100;
        trk[i-1]:=s[i-1];
        inc(sum[trk[i-1]]);
       end;
    dec(n);
    m:=200;
    for i:=2 to m dosum[i]:=sum[i]+sum[i-1];
    for i:=n downto 1do
      begin
       sa[sum[trk[i]]]:=i;
       dec(sum[trk[i]]);
      end;
    rank[sa[1]]:=1;
    p:=1;
    for i:=2 to n do
      begin
        iftrk[sa[i]]<>trk[sa[i-1]] then inc(p);
        rank[sa[i]]:=p;
      end;
    m:=p; j:=1;
    while m
      begin
        move(rank,trk,sizeof(rank));//rk——>>trk
        fillchar(sum,sizeof(sum),0);
        p:=0;
        fori:=n-j+1 to n do
          begin
            inc(p);
            tsa[p]:=i;
          end;
        fori:=1 to n do
          ifsa[i]>j then
            begin
              inc(p);
              tsa[p]:=sa[i]-j;
            end;
        fori:=1 to n do
          begin
            rank[i]:=trk[tsa[i]];
            inc(sum[rank[i]]);
          end;
        fori:=2 to m do inc(sum[i],sum[i-1]);
        fori:=n downto 1 do
        begin
          sa[sum[rank[i]]]:=tsa[i];
          dec(sum[rank[i]]);
        end;
        rank[sa[1]]:=1;
        p:=1;
        fori:=2 to n do
          begin
            if(trk[sa[i]]<>trk[sa[i-1]])or(trk[sa[i]+j]<>trk[sa[i-1]+j])  theninc(p);
            rank[sa[i]]:=p;
          end;
        m:=p;j:=j*2;
     end;
 
     h[1]:=0;p:=0;
     for i:=1 to ndo
       begin
         ifrank[i]=1 then continue;
         j:=sa[rank[i]-1];
         while(i+p<=n)and(j+p<=n)and(s[i+p]=s[j+p]) do inc(p);
         h[rank[i]]:=p;
         ifp>0 then dec(p);
       end;
 
     l:=0; r:=n;
     while l<=rdo
       begin
         mid:=(l+r)shr 1;
         ifcheck(mid) then l:=mid+1 else r:=mid-1;
       end;
    if l<5 thenwriteln(0) else writeln(l);  //  l保证ans+1 参考别人的  灰常机智的打法
 until false;
 // close(input); close(output);
end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值