POJ 3261Milk Patterns可重叠至少出现K次最长子串 二分+height分段

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
#define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb))
#define G(x) ((x) < tb ? (x) * 3 + 1 :((x) - tb) * 3 + 2)
const int MAXN = 70001;
int wa[MAXN],wb[MAXN],ws[MAXN],wv[MAXN],wsd[MAXN],r[MAXN],sa[MAXN];
int c0(int *r,int a,int b)
{return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];}
int c12(int k,int *r,int a,int b)
{if(k == 2) return r[a] < r[b] || r[a] == r[b] && c12(1,r,a + 1,b + 1);
else return r[a] < r[b] || r[a] == r[b] && wv[a + 1]< wv[b + 1];}
void sort(int *r,int *a,int *b,int n,int m)
{
    int i;
    for(i = 0 ; i < n ; i++) wv[i] = r[a[i]];
    for(i = 0 ; i < m ; i++) wsd[i] = 0;
    for(i = 0 ; i < n ; i++) wsd[wv[i]]++;
    for(i = 1 ; i < m ; i++) wsd[i] += wsd[i - 1];
    for(i = n - 1 ; i >= 0 ; i--) b[--wsd[wv[i]]] = a[i];
}

void dc3(int *r,int *sa,int n,int m)
{
    int i,j,*rn = r + n ,*san = sa + n,ta = 0,tb = (n + 1) / 3,tbc = 0,p;
    r[n] = r[n + 1] = 0;
    for(i = 0 ; i < n ; i++) if(i % 3 != 0) wa[tbc++] = i;
    sort(r + 2,wa,wb,tbc,m);
    sort(r + 1,wb,wa,tbc,m);
    sort(r,wa,wb,tbc,m);
    for(p = 1,rn[F(wb[0])] = 0,i = 1 ; i < tbc ; i++)
        rn[F(wb[i])] = c0(r,wb[i - 1],wb[i])?p - 1 : p++;
    if(p < tbc) dc3(rn,san,tbc,p);
    else for(i = 0 ; i < tbc ; i++) san[rn[i]] = i;
    for(i = 0 ;i < tbc ; i++) if(san[i] < tb) wb[ta++] = san[i] * 3;
    if(n % 3 == 1) wb[ta++] = n - 1;
    sort(r,wb,wa,ta,m);
    for(i = 0 ; i < tbc ; i++) wv[wb[i] = G(san[i])] = i;
    for(i = 0,j = 0,p = 0 ; i < ta && j < tbc ; p++)
        sa[p]=c12(wb[j] % 3,r,wa[i],wb[j]) ? wa[i++] : wb[j++];
    for(;i < ta ; p++) sa[p] = wa[i++];
    for(;j < tbc ; p++) sa[p] = wb[j++];
}
int Rank[MAXN],height[MAXN];
void calheight(int *r,int *sa,int n)
{
    int i,j,k = 0;
    for(i = 1 ; i <= n ; i++) Rank[sa[i]] = i;
    for(i = 0 ; i < n ; height[Rank[i++]] = k)
    for(k ? k--:0,j = sa[Rank[i] - 1] ;r[i + k]==r[j + k];k++);
}
int a[MAXN];
int b[MAXN];
bool judge(int mid,int n,int k){
    int cas = 2;
    while(true){
        while(cas<=n && height[cas] < mid) cas++;
        if(cas > n) return false;
        int cnt = 1;
        while(cas<=n && height[cas] >= mid){
            cnt++;
            cas++;
        }
        if(cnt >= k) return true;
    }
    return false;
}
int main(){
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        for(int i = 0; i < n; ++i){
            scanf("%d",&a[i]);
            b[i] = a[i];
        }
        sort(b,b+n);
        int siz = unique(b,b+n)-b;
        for(int i = 0; i < n; ++i){
            a[i] = lower_bound(b,b+n,a[i])-b+1;
        }
        a[n] = 0;
        dc3(a,sa,n+1,20001);
        calheight(a,sa,n);
        int l = 2,r = n;
        int ans = 0;
        while(l <= r){
            int mid = (l+r)>>1;
            if(judge(mid,n,k)){
                ans = mid;
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值