Musical Theme

poj1743:http://poj.org/problem?id=1743

题意:

题解:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 using namespace std;
  5 const int maxn=20010;
  6 char str[maxn];
  7 int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
  8 int f[maxn];
  9 int cmp(int* r,int a,int b,int l)
 10 {
 11     return r[a]==r[b]&&r[a+l]==r[b+l];
 12 }
 13 //n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排序时子串的长度
 14 void DA(int* r,int* sa,int n,int m)
 15 {
 16     int i,j,p,*x=wa,*y=wb,*t;
 17     ///对R中长度为1的子串进行基数排序
 18     for(i=0; i<m; i++)wn[i]=0;
 19     for(i=0; i<n; i++)wn[x[i]=r[i]]++;
 20     for(i=1; i<m; i++)wn[i]+=wn[i-1];
 21     for(i=n-1; i>=0; i--)sa[--wn[x[i]]]=i;
 22     for(j=1,p=1; p<n; j*=2,m=p)
 23     {
 24         //利用了上一次基数排序的结果,对待排序的子串的第二关键字进行了一次高效地基数排序
 25         for(p=0,i=n-j; i<n; i++)y[p++]=i;
 26         for(i=0; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
 27         ///基数排序
 28         for(i=0; i<n; i++)wv[i]=x[y[i]];
 29         for(i=0; i<m; i++)wn[i]=0;
 30         for(i=0; i<n; i++)wn[wv[i]]++;
 31         for(i=1; i<m; i++)wn[i]+=wn[i-1];
 32         for(i=n-1; i>=0; i--)sa[--wn[wv[i]]]=y[i];
 33         ///当p=n的时候,说明所有串都已经排好序了
 34         ///在第一次排序以后,rank数组中的最大值小于p,所以让m=p
 35         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
 36             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
 37     }
 38     return;
 39 }
 40 ///后缀数组  计算height数组
 41 /**
 42 height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
 43 原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
 44 一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
 45 增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
 46 (即height[1])应当为0.在调用calheight函数时,要注意height数组的范
 47 围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
 48 而不是calheight(r,sa,n+1)。*/
 49 int rank[maxn],height[maxn];
 50 void calheight(int* r,int* sa,int n)
 51 {
 52     int i,j,k=0;
 53     for(i=1; i<=n; i++)rank[sa[i]]=i;
 54     for(i=0; i<n; height[rank[i++]]=k)
 55         for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
 56     return;
 57 }
 58 int t;
 59 bool judge(int mid){
 60     int minn=sa[1],maxn=sa[1];
 61    for(int i=1;i<=t;i++){
 62        if(height[i]<mid){
 63         minn=sa[i],maxn=sa[i];
 64        }
 65        else{
 66           minn=min(minn,sa[i]);
 67           maxn=max(maxn,sa[i]);
 68           if(maxn-minn>mid)return true;
 69        }
 70    }
 71    return false;
 72 }
 73
 74 int main(){
 75     while(~scanf("%d",&t)&&t){
 76         f[0]=0;
 77         for(int i=1;i<=t;i++)
 78             scanf("%d",&f[i]);
 79        for(int i=1;i<=t;i++){
 80            a[i-1]=f[i]-f[i-1]+100;
 81        }
 82         a[t]=0;
 83         DA(a,sa,t+1,190);
 84         calheight(a,sa,t);
 85         int l=0,r=t,ans=0;
 86         while(l<=r){
 87              //   printf("%d %d\n",l,r);
 88             int mid=(l+r)/2;
 89             if(judge(mid)){
 90                 ans=mid;
 91                 l=mid+1;
 92             }
 93             else
 94                 r=mid-1;
 95         }
 96         if(ans>=4)
 97         printf("%d\n",ans+1);
 98         else
 99             printf("0\n");
100     }
101     return 0;
102 }

Musical Theme,布布扣,bubuko.com

时间: 2024-11-01 06:57:17

Musical Theme的相关文章

poj1743 Musical Theme

地址:http://poj.org/problem?id=1743 题目: Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 28675   Accepted: 9674 Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are integers in the range

POJ1743 Musical Theme [没做出来]

Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 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 (后缀数组)

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

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 Musical Theme (求最长不重叠子串)

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

POJ1743 (Musical Theme,后缀数组)

Links Musical Theme Time Limit: 1000MS   Memory Limit: 30000K       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 t

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

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

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