【POJ】3261 Milk Patterns

http://poj.org/problem?id=3261

题意:一个长度为n的串,要求最长的子串的长度且这个子串的出现次数不少于k次。(1<=n<=20000, 2<=k<=n)

#include <cstdio>

#include <algorithm>

using namespace std;



const int N=20015;

void sort(int *x, int *y, int *sa, int n, int m) {

	static int c[N], i;

	for(i=0; i<m; ++i) c[i]=0;

	for(i=0; i<n; ++i) c[x[y[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];

}

void hz(int *r, int *sa, int n, int m) {

	static int t1[N], t2[N];

	static int *x, *y, *t, j, i, p=0;

	x=t1; y=t2;

	for(i=0; i<n; ++i) x[i]=r[i], y[i]=i;

	sort(x, y, sa, n, m);

	for(j=1, p=1; p<n; j<<=1, m=p) {

		p=0;

		for(i=n-j; i<n; ++i) y[p++]=i;

		for(i=0; i<n; ++i) if(sa[i]-j>=0) y[p++]=sa[i]-j;

		sort(x, y, sa, n, m);

		for(t=x, x=y, y=t, x[sa[0]]=0, p=1, i=1; i<n; ++i)

			x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;

	}

}

void geth(int *a, int *sa, int *rank, int *h, int n) {

	static int k, i, j; k=0;

	for(i=1; i<=n; ++i) rank[sa[i]]=i;

	for(i=1; i<=n; h[rank[i++]]=k)

		for(k?--k:0, j=sa[rank[i]-1]; a[i+k]==a[j+k]; ++k);

}

const int oo=~0u>>2;

int sa[N], rank[N], h[N], n, a[N], b[N], K;

bool check(int k) {

	int cnt=1;

	for(int i=2; i<=n; ++i) {

		if(h[i]>=k) {

			++cnt;

			if(cnt>=K) return 1;

		}

		else cnt=1;

	}

	if(cnt>=K) return 1;

	return 0;

}

int mp[1000005];

int main() {

	scanf("%d%d", &n, &K);

	for(int i=1; i<=n; ++i) scanf("%d", &a[i]), b[i]=a[i];

	sort(b+1, b+1+n);

	int tot=unique(b+1, b+1+n)-b-1;

	for(int i=1; i<=tot; ++i) mp[b[i]]=i;

	for(int i=1; i<=n; ++i) a[i]=mp[a[i]];

	hz(a, sa, n+1, 200);

	geth(a, sa, rank, h, n);

	int mid, l=0, r=n;

	while(l<=r) {

		mid=(l+r)>>1;

		if(check(mid)) l=mid+1;

		else r=mid-1;

	}

	printf("%d\n", l-1);

	return 0;

}

  


 

经典题...同样是分组height...将高度>=二分值的分在一组,然后判断是否有大于等于K个元素即可

你可能感兴趣的:(Pattern)