• POJ 1743 Musical Theme (后缀数组)


    Musical Theme
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 13852   Accepted: 4810

    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.
    思路:后缀数组,最长不重叠字串,二分判断合法性,论文上有
    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <cstring>
    #include <string>
    using namespace std;
    
    const int maxn=50000;
    int wss[maxn],wa[maxn],wb[maxn],wv[maxn];
    int r[maxn],sa[maxn],rank[maxn],h[maxn];
    int n,m,t,maxx,minn,he,mid;
    
    
    void close()
    {
    	exit(0);
    }
    
    int cmp(int *r,int x,int y,int l)
    {
    	return (r[x]==r[y] && r[x+l]==r[y+l]);
    }
    
    void da(int n,int m,int *ws)
    {
    	int i,j,*t,*x=wa,*y=wb,p;
    	for (i=0;i<m;i++) ws[i]=0;
    	for (i=0;i<n;i++) ws[x[i]=r[i]]++;
    	for (i=1;i<m;i++) ws[i]+=ws[i-1];
    	for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
    	for (j=1,p=1;p<n;j*=2,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++) ws[i]=0;
    		for (i=0;i<n;i++) ws[wv[i]]++;
    		for (i=1;i<m;i++) ws[i]+=ws[i-1];
    		for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
    		for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
    			x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
    	}
    }
    
    void callheight(int n)
    {
    	int i,j,k=0;
    	for (i=1;i<=n;i++) rank[sa[i]]=i;
    	for (i=0;i<n;h[rank[i++]]=k)
    		for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    }
    
    bool check(int k)
    {
    	minn=sa[1];maxx=sa[1];
    	for (int i=2;i<n;i++)
    	{
    		if (h[i]>=k)
    		{
    			minn=min(minn,sa[i]);
    			maxx=max(maxx,sa[i]);
    			if (maxx-minn>=k)
    				return true;
    		}
    		else
    		{
    			minn=maxx=sa[i];
    		}
    	}
    	return false;
    }
    
    void work()
    {
    	he=0;t=n;
    	while (he<=t)
    	{
    //		printf("he:%d mid:%d t:%d\n",he,mid,t);
    		mid=(he+t)>>1;
    		if (check(mid))
    			he=mid+1;
    		else t=mid-1;
    	}
    	if (t>=4)
    		printf("%d\n",t+1);
    	else printf("0\n");
    }
    
    void init ()
    {
    //freopen("theme.out","w",stdout);
        while(scanf("%d",&n)!=EOF)
    	 {
    		 if (n==0) break;
          	 for (int i=0;i<n;i++)
           		 scanf("%d",&r[i]);
           	 for (int i=0;i<n;i++)
           		 r[i]=r[i+1]-r[i]+100;
    			 r[--n]=0;
    			 da(n+1,200,wss);
    			 callheight(n);
    			 work();
    	 }
    }
    
    int main ()
    {
    	init();
    	close();
    	return 0;
    }
    
     
  • 相关阅读:
    pixi.js 简单交互事件(点击、缩放、平移)
    w3school
    并发(Concurrent)与并行(Parallel)的区别
    CTime获得当前电脑时间
    NX二次开发-UFUN获得体的表面积,体积,重心等UF_MODL_ask_mass_props_3d
    Jvm故障问题排查以及Jvm调优总结
    jmap命令详解----查看JVM内存使用详情
    jstack命令解析
    年轻带Young GC算法示意图
    【JVM】jstat命令详解---JVM的统计监测工具
  • 原文地址:https://www.cnblogs.com/cssystem/p/2962061.html
Copyright © 2020-2023  润新知