poj1743-----Musical Theme

本文介绍了一种算法,用于从一系列音符中找出重复出现的音乐主题,这些主题至少包含五个音符,并且在音乐中至少出现两次,可能经过转调处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Musical Theme
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 24390 Accepted: 8218

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

 
 
 
 

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

    1.长度至少为5个音符。

    2.在乐曲中重复出现。(可能经过转调,“转调”的意思是主题序列中每个音符都被加上或减去了同一个整数值)

    3.重复出现的同一主题不能有公共部分。

 
这种瘠薄题,打错都错模板上了,本题先把数组转换成相邻的差,问题转变为寻找相同段,长度大于等于5,而且无相交部分,我们可以二分答案,将height值大于等于mid的分成同一组,同一组内,若是mx_sa和mn_sa差距大于x,那么这便是没有相交的一组答案。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 char s[500005];
 7 int num[500005],wa[500005],ws[500005],wb[500005],h[500005],n;
 8 int wv[500005],rank[500005],sa[500005];
 9 bool cmp(int *r,int a,int b,int l){
10     return r[a]==r[b]&&r[a+l]==r[b+l];
11 }
12 void da(int *r,int *sa,int n,int m){
13     int *t,*x=wa,*y=wb,i,j,k,p;
14     for (i=0;i<m;i++) ws[i]=0;
15     for (i=0;i<n;i++) x[i]=r[i];
16     for (i=0;i<n;i++) ws[x[i]]++;
17     for (i=1;i<m;i++) ws[i]+=ws[i-1];
18     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
19     for (j=1,p=1;p<n;j*=2,m=p){
20         for (p=0,i=n-j;i<n;i++) y[p++]=i;
21         for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;
22         for (i=0;i<m;i++) ws[i]=0;
23         for (i=0;i<n;i++) wv[i]=x[y[i]];
24         for (i=0;i<n;i++) ws[wv[i]]++;
25         for (i=1;i<m;i++) ws[i]+=ws[i-1];
26         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
27         for (t=x,x=y,y=t,i=1,x[sa[0]]=0,p=1;i<n;i++){
28             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
29         }
30     }
31 }
32 void cal(int *r,int n){
33     int i,j,k=0;
34     for (int i=1;i<=n;i++) rank[sa[i]]=i;
35     for (int i=0;i<n;h[rank[i++]]=k){
36         for (k?k--:0,j=sa[rank[i]-1];r[j+k]==r[i+k];k++);
37     }
38 }
39 bool check(int x){
40     int mn=sa[0],mx=sa[0];
41     for (int i=0;i<=n;i++)
42      if (h[i]>=x){
43         mx=std::max(mx,sa[i]);
44         mn=std::min(mn,sa[i]);
45         if (mx-mn>x) return 1;
46     }else
47     mx=mn=sa[i];
48     return 0;
49 }
50 void work(){
51     int l=0,r=n,ans=0;
52     while (l<=r){
53         int mid=(l+r)/2;
54         if (check(mid)) ans=mid,l=mid+1;
55         else r=mid-1;
56     }
57     if (ans<4) ans=0;
58     else ans++;
59     printf("%d\n",ans);
60 }
61 int main(){
62     while (~scanf("%d",&n)){
63         if (n==0) break;
64         for (int i=0;i<n;i++) scanf("%d",&num[i]);
65         for (int i=0;i<n-1;i++) num[i]=num[i+1]-num[i]+90;
66         n--;num[n]=0;
67         da(num,sa,n+1,200);
68         cal(num,n);
69         work();
70     }
71 }

 

 
 

转载于:https://www.cnblogs.com/qzqzgfy/p/5336572.html

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值