zoukankan      html  css  js  c++  java
  • POJ1743(后缀数组)

    Musical Theme

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 26899   Accepted: 9081

    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.
     
    倍增法求sa,rank;
      1 //2016.10.12
      2 #include <iostream>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <algorithm>
      6 
      7 using namespace std;
      8 
      9 const int N = 20005;
     10 const int inf = 0x3f3f3f3f;
     11 int sa[N], wa[N], wb[N], wv[N], wss[N], Rank[N], height[N];
     12 
     13 int cmp(int *r, int a, int b, int len)
     14 {
     15     return r[a]==r[b] && r[a+len]==r[b+len];
     16 }
     17 
     18 void da(int *r, int *sa, int n, int m)
     19 {
     20     int i, j, p, *x = wa, *y = wb, *t;
     21     for(i = 0; i < m; i++)wss[i] = 0;
     22     for(i = 0; i < n; i++)wss[x[i]=r[i]]++;
     23     for(i = 0; i < m; i++)wss[i]+=wss[i-1];
     24     for(i = n-1; i >= 0; i--)sa[--wss[x[i]]]=i;
     25     for(j = 1, p = 1; p < n; j *= 2, m = p){
     26         for(p = 0, i = n-j; i < n; i++)
     27               y[p++] = i;
     28         for(i = 0; i < n; i++)
     29               if(sa[i] >= j)
     30                   y[p++] = sa[i]-j;
     31         for(i = 0; i < n; i++)
     32               wv[i] = x[y[i]];
     33         for(i = 0; i < m; i++)
     34               wss[i] = 0;
     35         for(i = 0; i < n; i++)
     36               wss[wv[i]]++;
     37         for(i = 1; i < m; i++)
     38               wss[i] += wss[i-1];
     39         for(i = n-1; i >= 0; i--)
     40               sa[--wss[wv[i]]] = y[i];
     41         for(t = x, x = y, y = t, p = 1, x[sa[0]]=0, i = 1; i < n; i++)
     42               x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
     43     }
     44 }
     45 
     46 void calheight(int *r, int *sa, int n)
     47 {
     48     int i, j, k = 0;
     49     for(i = 1; i <= n; i++)Rank[sa[i]] = i;
     50     for(i = 0; i < n; height[Rank[i++]] = k)
     51           for(k?k--:0, j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++)
     52           ;
     53 }
     54 
     55 int n, r[N], arr[N];
     56 
     57 bool haveTheme(int len)
     58 {
     59     int minn = inf, maxn = -inf;
     60     for(int i = 1; i <= n; i++){
     61         if(height[i] < len){
     62             if(maxn-minn >= len)
     63                   return true;
     64             minn = inf; maxn = -inf;
     65         }
     66         minn = min(minn, sa[i]);
     67         maxn = max(maxn, sa[i]);
     68         if(i==n && maxn-minn >= len)
     69               return true;
     70     }
     71     return false;
     72 }
     73 
     74 int main()
     75 {
     76     while(scanf("%d", &n)!=EOF && n)
     77     {
     78         int maxn = 0;
     79         for(int i = 0; i < n; i++)
     80               scanf("%d", &arr[i]);
     81         n--;
     82         for(int i = 0; i < n; i++)
     83               r[i] = arr[i+1]-arr[i] + 90;
     84         r[n] = 0;
     85         da(r, sa, n+1, 200);
     86         calheight(r, sa, n);
     87         int l = 3, r = n, mid, ans;
     88         while(l <= r){
     89             mid = (l+r)>>1;
     90             if(haveTheme(mid)){
     91                 l = mid + 1;
     92                 ans = mid;
     93             }else r = mid-1;
     94         }
     95         if(n < 9 || ans < 4)printf("0
    ");
     96         else printf("%d
    ", ans+1);
     97     }
     98 
     99     return 0;
    100 }
  • 相关阅读:
    Spring总结四:IOC和DI 注解方式
    Spring总结二:IOC(控制反转)xml方式
    Spring总结一:Srping快速入门
    TCP UDP HTTP 的关系和区别
    sql 一些偶尔会用到的写法和函数 不定时更新
    AngularJS分层开发
    AngularJS入门
    url和uri的区别
    MyBatis总结八:缓存介绍(一级缓存,二级缓存)
    Javascript 中 == 与=== 对比
  • 原文地址:https://www.cnblogs.com/Penn000/p/5953579.html
Copyright © 2011-2022 走看看