POJ3261 Milk Patterns

Milk Patterns

题目大意

求一个数串(数集为0~10000000)中最长至少重复k次的子串。

题解

SA二分分组。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
void swap(int* &a, int* &b){int* tmp = a;a = b;b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
{
	x = 0;char ch = getchar(), c = ch;
	while(ch < '0' || ch > '9') c = ch, ch = getchar();
	while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if(c == '-') x = -x;
}

const int INF = 0x3f3f3f3f;
const int MAXN = 20000 + 10; 
const int MAXNUM = 1000000 + 10;


struct SuffixArray
{
    int s[MAXN], sa[MAXN], rank[MAXN], height[MAXN];
    int t[MAXNUM], t2[MAXNUM], c[MAXNUM];
    int n;
    void clear()
	{
		n = 0;
		memset(sa, 0, sizeof(sa));
	}
    
    void build_sa(int m)
    {
    	++ n;
        int i,*x = t,*y = t2;
    	for(i = 0;i < m;++ i) c[i] = 0;
    	for(i = 0;i < n;++ i) x[i] = s[i];
    	for(i = 0;i < n;++ i) ++ c[x[i]];
    	for(i = 1;i < m;++ i) c[i] += c[i-1];
    	for(i = n - 1;i >= 0;-- i) sa[-- c[x[i]]] = i;
    	for(int k = 1;k <= n;k <<= 1)
    	{
  			int p = 0;
   	    	for(i = n - k;i < n;++ i) y[p ++] = i;
        	for(i = 0;i < n;++ i) if(sa[i] >= k) y[p ++] = sa[i] - k;
        	for(i = 0;i < m;++ i) c[i] = 0;
        	for(i = 0;i < n;++ i) c[x[i]] ++;
        	for(i = 1;i < m;++ i) c[i] += c[i - 1];
        	for(i = n - 1;i>=0;--i) sa[-- c[x[y[i]]]] = y[i];
        	swap(x,y);
        	p = 1;x[sa[0]] = 0;
        	for(i = 1;i < n;++ i)
        	    x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? p-1 : p++;
        	if(p >= n) break;
        	m = p;
    	}
    	-- n;
    }
    
    void build_height()
    {
        int i, j, k = 0;
   		for(i = 1;i <= n;++ i) rank[sa[i]] = i;
    	for(i = 0;i < n;++ i)
    	{
    	    if(k) k --;
    	    j = sa[rank[i]-1];
    	    while(s[i + k] == s[j + k]) k ++;
    	    height[rank[i]] = k;
    	}
    }
}A;

int n, k;
int l[MAXN], r[MAXN], tot;

bool check(int x)
{
	l[tot = 1] = 1;
	for(int i = 2;i <= n;++ i)
	{
		while(A.height[i] >= x) ++ i;
		r[tot] = i - 1;
		l[++ tot] = i;
	}
	r[tot] = n;

	for(int i = 1;i <= tot;++ i)
		if(r[i] - l[i] + 1 >= k)
			return 1;
	return 0;
}

int main()
{
	read(n), read(k);
	for(int i = 0;i < n;++ i) read(A.s[i]);
	A.s[n] = 0;
	A.n = n;
	A.build_sa(MAXNUM);
	A.build_height();
	int l = 0, r = n, mid, ans = 0;
	while(l <= r)
	{
		mid = (l + r) >> 1;
		if(check(mid)) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	printf("%d", ans);
	return 0;
} 
posted @ 2019-07-31 13:35  嘒彼小星  阅读(146)  评论(0编辑  收藏