zoukankan      html  css  js  c++  java
  • POJ 1743 Musical Theme

    Musical Theme

    Time Limit: 1000ms
    Memory Limit: 30000KB
    This problem will be judged on PKU. Original ID: 1743
    64-bit integer IO format: %lld      Java class name: Main
    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

     
    解题:最长非重叠重复子串,后缀数组
     
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn = 500010;
     6 int sa[maxn],rk[maxn],height[maxn],c[maxn];
     7 int n,t[maxn],t2[maxn],s[maxn];
     8 void build_sa(int m) {
     9     int i,j,*x = t,*y = t2;
    10     for(i = 0; i < m; ++i) c[i] = 0;
    11     for(i = 0; i < n; ++i) c[x[i] = s[i]]++;
    12     for(i = 1; i < m; ++i) c[i] += c[i-1];
    13     for(i = n-1; i >= 0; --i) sa[--c[x[i]]] = i;
    14 
    15     for(int k = 1; k <= n; k <<= 1) {
    16         int p = 0;
    17         for(i = 0; i < m; ++i) c[i] = 0;
    18         for(i = n-k; i < n; ++i) y[p++] = i;
    19         for(i = 0; i < n; ++i)
    20             if(sa[i] >= k) y[p++] = sa[i] - k;
    21         for(i = 0; i < n; ++i) c[x[y[i]]]++;
    22         for(i = 1; i < m; ++i) c[i] += c[i-1];
    23         for(i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
    24 
    25         swap(x,y);
    26         p = 1;
    27         x[sa[0]] = 0;
    28         for(i = 1; i < n; ++i)
    29             if(y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k])
    30                 x[sa[i]] = p-1;
    31             else x[sa[i]] = p++;
    32         if(p >= n) break;
    33         m = p;
    34     }
    35 }
    36 void getHeight() {
    37     int i,j,k = 0;
    38     for(i = 0; i < n; ++i) rk[sa[i]] = i;
    39     for(i = 0; i < n; ++i) {
    40         if(k) --k;
    41         j = sa[rk[i]-1];
    42         while(i + k < n && j + k < n && s[i+k] == s[j+k]) k++;
    43         height[rk[i]] = k;
    44     }
    45 }
    46 bool check(int k) {
    47     int low = sa[1],high = sa[1];
    48     for(int i = 2; i < n; ++i) {
    49         if(height[i] < k) low = high = sa[i];
    50         else {
    51             low = min(sa[i],low);
    52             high = max(sa[i],high);
    53             if(high - low > k) return true;
    54         }
    55     }
    56     return false;
    57 }
    58 int tmp[maxn];
    59 int main() {
    60     while(scanf("%d",&n),n) {
    61         for(int i = 0; i < n; ++i)
    62             scanf("%d",tmp+i);
    63         for(int i = 0; i+1 < n; ++i)
    64             s[i] = tmp[i+1] - tmp[i] + 90;
    65         s[n-1] = 0;
    66         build_sa(200);
    67         getHeight();
    68         int low = 4,high = n,ret = -1;
    69         while(low <= high) {
    70             int mid = (low + high)>>1;
    71             if(check(mid)) {
    72                 low = mid + 1;
    73                 ret = mid;
    74             } else high = mid - 1;
    75         }
    76         printf("%d
    ",ret+1);
    77     }
    78     return 0;
    79 }
    View Code

    后缀自动机

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn = 50010;
     6 int c[maxn],sa[maxn],n;
     7 struct SAM {
     8     struct node {
     9         int son[180],f,len,l,r;
    10         void init() {
    11             memset(son,-1,sizeof son);
    12             f = -1;
    13             l = r = len = 0;
    14         }
    15     } e[maxn];
    16     int tot,last;
    17     int newnode(int len = 0) {
    18         e[tot].init();
    19         e[tot].len = len;
    20         return tot++;
    21     }
    22     void init() {
    23         tot = last = 0;
    24         newnode();
    25     }
    26     void extend(int c) {
    27         int p = last,np = newnode(e[p].len + 1);
    28         e[np].l = e[np].r = e[np].len;
    29         while(p != -1 && e[p].son[c] == -1) {
    30             e[p].son[c] = np;
    31             p = e[p].f;
    32         }
    33         if(p == -1) e[np].f = 0;
    34         else {
    35             int q = e[p].son[c];
    36             if(e[p].len + 1 == e[q].len) e[np].f = q;
    37             else {
    38                 int nq = newnode();
    39                 e[nq] = e[q];
    40                 e[nq].len = e[p].len + 1;
    41                 e[q].f = e[np].f = nq;
    42                 while(p != -1 && e[p].son[c] == q) {
    43                     e[p].son[c] = nq;
    44                     p = e[p].f;
    45                 }
    46             }
    47         }
    48         last = np;
    49     }
    50     void solve() {
    51         memset(c,0,sizeof c);
    52         int ret = 0;
    53         for(int i = 1; i < tot; ++i) c[e[i].len]++;
    54         for(int i = 1; i <= n; ++i) c[i] += c[i-1];
    55         for(int i = tot-1; i > 0; --i) sa[c[e[i].len]--] = i;
    56         for(int i = tot-1; i > 0; --i) {
    57             int sn = sa[i];
    58             e[e[sn].f].l = min(e[sn].l,e[e[sn].f].l);
    59             e[e[sn].f].r = max(e[sn].r,e[e[sn].f].r);
    60         }
    61         for(int i = 0; i < tot; ++i)
    62             ret = max(ret,min(e[i].len,e[i].r - e[i].l));
    63         printf("%d
    ",ret < 4?0:ret+1);
    64     }
    65 } sam;
    66 int main() {
    67     int pre;
    68     while(scanf("%d",&n),n) {
    69         sam.init();
    70         for(int i = pre = 0,tmp; i < n; ++i) {
    71             scanf("%d",&tmp);
    72             if(i) sam.extend(tmp - pre + 88);
    73             pre = tmp;
    74         }
    75         n--;
    76         sam.solve();
    77     }
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    【原创】项目管理软件之争,禅道和JIRA大对比
    互联网大数据下渐渐被人遗忘的数据安全隐患
    然之和今目标办公系统软件功能大对比
    企业如何在办公系统中实践阿米巴
    企业在建站前需要了解的七点
    一个好的网站设计如何影响内容营销
    前端设计师必须知道的10个重要的CSS技巧
    为什么用CDN给你网站加速?
    超实用!网站导航栏设计方法总结
    作为新手,SEO要避免的五大误区
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4634145.html
Copyright © 2011-2022 走看看