POJ1743 Musical Theme

 
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 28303   Accepted: 9553

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

 

字符串 后缀自动机

マジやばくね——

为了过滤掉“转调”操作,需要把读入的数字序列转换成差分序列,然后把差分序列建成后缀自动机

然后在后缀自动机上维护每个状态的right集合的min和max值,作差更新答案。

 

总算没写错SAM模板了233,然而还是WAWAWA,看了discuss简直受到了惊吓,原来还有n=1的情况。

我原本的读入方式是把读入的第一个数字当做基准,将后面的序列转成差值←显然会被卡掉

然后就学黄学长那样舍弃最后一个数字

↑但是仔细想想真的有区别吗?迷

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=40010;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
14     return x*f;
15 }
16 struct SAM{
17     int t[mxn][180],fa[mxn];
18     int l[mxn],mx[mxn],mi[mxn];
19     int w[mxn],rk[mxn];
20     int S,cnt,last;
21     void init(){
22         memset(t,0,sizeof t);
23         memset(fa,0,sizeof fa);
24         memset(w,0,sizeof w);
25         memset(l,0,sizeof l);
26         memset(mx,0,sizeof mx);
27         memset(mi,0x3f,sizeof mi);
28         S=cnt=last=1;return;
29     }
30     void add(int c){
31         int p=last,np=++cnt;last=np;
32         l[np]=l[p]+1;
33         mx[np]=mi[np]=l[np];
34         for(;p && !t[p][c];p=fa[p])t[p][c]=np;
35         if(!p)
36             fa[np]=S;
37         else{
38             int q=t[p][c];
39             if(l[q]==l[p]+1)fa[np]=q;
40             else{
41                 int nq=++cnt;l[nq]=l[p]+1;
42                 memcpy(t[nq],t[q],sizeof t[q]);
43                 fa[nq]=fa[q];
44                 fa[q]=fa[np]=nq;
45                 for(;p && t[p][c]==q;p=fa[p])t[p][c]=nq;
46             }
47         }
48         return;
49     }
50     void st(int len){
51         for(int i=1;i<=cnt;i++)w[l[i]]++;
52         for(int i=1;i<=len;i++)w[i]+=w[i-1];
53         for(int i=1;i<=cnt;i++)rk[w[l[i]]--]=i;
54         return;
55     }
56     void solve(){
57         for(int i=cnt;i;i--){
58             int t=rk[i];
59 //            printf("t:%d  mi:%d mx:%d\n",t,mi[t],mx[t]);
60             mi[fa[t]]=min(mi[fa[t]],mi[t]);
61             mx[fa[t]]=max(mx[fa[t]],mx[t]);
62         }
63         int ans=0;
64         for(int i=1;i<=cnt;i++)ans=max(ans,min(mx[i]-mi[i],l[i]));
65 //        printf("ans:%d\n",ans);
66         if(ans<4)ans=-1;
67         printf("%d\n",ans+1);
68         return;
69     }    
70 }sa;
71 int n,a[mxn>>1];
72 int main(){
73     int i,j;
74     while(scanf("%d",&n)!=EOF && n){
75         sa.init();
76         for(i=1;i<=n;i++)a[i]=read();n--;
77         for(i=1;i<=n;i++){
78             a[i]=a[i+1]-a[i]+88;
79             sa.add(a[i]);
80         }
81         sa.st(n);
82         sa.solve();
83     }
84     return 0;
85 }

 

posted @ 2017-03-13 20:51  SilverNebula  阅读(108)  评论(0编辑  收藏  举报
AmazingCounters.com