码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1743:Musical Theme

时间:2018-01-23 20:22:35      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:--   eof   htm   ems   http   gis   long   bre   class   

题目

vjudge

Sol

先差分
然后求不可重叠最长重复子串

bits/stdc++.h会CE

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(20010);

IL ll Read(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, a[_], sa[_], rk[_], y[_], h[_], height[_], t[_], ans;

IL bool Cmp(RG int i, RG int j, RG int k){  return y[i] == y[j] && y[i + k] == y[j + k];  }

IL void Sort(){
    RG int m = 180;
    for(RG int i = 1; i <= m; ++i) t[i] = 0;
    for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
    for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
    for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
    for(RG int k = 1; k <= n; k <<= 1){
        RG int l = 0;
        for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
        for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
        for(RG int i = 1; i <= m; ++i) t[i] = 0;
        for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
        for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
        for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
        swap(rk, y); rk[sa[1]] = l = 1;
        for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i], sa[i - 1], k) ? l : ++l;
        if(l >= n) break; m = l;
    }
    for(RG int i = 1; i <= n; ++i){
        h[i] = max(0, h[i - 1] - 1);
        if(rk[i] == 1) continue;
        while(a[i + h[i]] == a[sa[rk[i] - 1] + h[i]]) ++h[i];
    }
    for(RG int i = 1; i <= n; ++i) height[i] = h[sa[i]];
}

IL bool Check(RG int k){
    RG int mn = sa[1], mx = sa[1];
    for(RG int i = 2; i <= n; ++i){
        if(height[i] >= k) mx = max(mx, sa[i]), mn = min(mn, sa[i]);
        else mx = mn = sa[i];
        if(mx - mn > k) return 1;
    }
    return 0;
}

int main(RG int argc, RG char* argv[]){
    for(n = Read(); n; n = Read()){
        Fill(a, 0); ans = 0;
        for(RG int i = 1; i <= n; ++i) a[i] = Read();
        for(RG int i = 1; i <= n; ++i) a[i] = a[i + 1] - a[i] + 88;
        Sort();
        RG int l = 4, r = n;
        while(l <= r){
            RG int mid = (l + r) >> 1;
            if(Check(mid)) ans = mid + 1, l = mid + 1;
            else r = mid - 1;
        }
        printf("%d\n", ans);
    }
    return 0;
}

POJ1743:Musical Theme

标签:--   eof   htm   ems   http   gis   long   bre   class   

原文地址:https://www.cnblogs.com/cjoieryl/p/8337239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!