POJ 3261 Milk Patterns(后缀数组)

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

题意:给你一个序列,求序列里重复出现至少K次的最长子串

分析:这题如果学过后缀数组的话,那就是模版题了,直接构造一个后缀数组,然后枚举i,询问[ i, i+k-1 ] 的最长公共前缀就行,i表示排在第i位的后缀

代码:

/** head files*/
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

/** some operate*/
#define PB push_back
#define MP make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define UPTO(i,l,h) for(i=(l);i<=(h);++i)
#define DOWN(i,h,l) for(i=(h);i>=(l);--i)
#define MSET(arr,val) memset(arr,val,sizeof(arr))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))

/** some const*/
#define N 222222
#define M 222222
#define PI acos(-1.0)
#define oo 1111111111

/** some alias*/
typedef long long ll;

/** Global variables*/


/** some template names, just push ctrl+j to get it in*/
//manacher 求最长回文子串
//pqueue 优先队列
//combk n元素序列的第m小的组合和
//pmatrix n个点的最大子矩阵
//suffixarray 后缀数组
template <typename T, int LEN>
struct suffixarray
{
	int str[LEN*3],sa[LEN*3];
	int rank[LEN],height[LEN];
	int id[LEN];
	int best[LEN][20];
	int len;
	bool equal(int *str, int a, int b)
	{
		return str[a]==str[b]&&str[a+1]==str[b+1]&&str[a+2]==str[b+2];
	}
	bool cmp3(int *str, int *nstr, int a, int b)
	{
		if(str[a]!=str[b])return str[a]<str[b];
		if(str[a+1]!=str[b+1])return str[a+1]<str[b+1];
		return nstr[a+b%3]<nstr[b+b%3];
	}
	void radixsort(int *str, int *sa, int *res, int n, int m)
	{
		int i;
		REP(i,m)id[i]=0;
		REP(i,n)++id[str[sa[i]]];
		REP(i,m)id[i+1]+=id[i];
		DOWN(i,n-1,0)res[--id[str[sa[i]]]]=sa[i];
	}
	void dc3(int *str, int *sa, int n, int m)
	{
		#define F(x) ((x)/3+((x)%3==1?0:one))
		#define G(x) ((x)<one?(x)*3+1:((x)-one)*3+2)
		int *nstr=str+n, *nsa=sa+n, *tmpa=rank, *tmpb=height;
		int i,j,k,len=0,num=0,zero=0,one=(n+1)/3;
		REP(i,n)if(i%3)tmpa[len++]=i;
		str[n]=str[n+1]=0;
		radixsort(str+2, tmpa, tmpb, len, m);
		radixsort(str+1, tmpb, tmpa, len, m);
		radixsort(str+0, tmpa, tmpb, len, m);
		nstr[F(tmpb[0])]=num++;
		UPTO(i,1,len-1)
			nstr[F(tmpb[i])]=equal(str,tmpb[i-1],tmpb[i])?num-1:num++;
		if(num<len)dc3(nstr,nsa,len,num);
		else REP(i,len)nsa[nstr[i]]=i;
		if(n%3==1)tmpa[zero++]=n-1;
		REP(i,len)if(nsa[i]<one)tmpa[zero++]=nsa[i]*3;
		radixsort(str, tmpa, tmpb, zero, m);
		REP(i,len)tmpa[nsa[i]=G(nsa[i])]=i;
		i=j=0;
		REP(k,n)
		if(j>=len||(i<zero&&cmp3(str,tmpa,tmpb[i],nsa[j])))sa[k]=tmpb[i++];
		else sa[k]=nsa[j++];
	}
	void initSA(T *s, int n,int m)
	{
		int i,j,k=0;
		str[len=n]=0;
		REP(i,n)str[i]=s[i];
		dc3(str,sa,n+1,m);
		REP(i,n)sa[i]=sa[i+1];
		REP(i,n)rank[sa[i]]=i;
		REP(i,n)
		{
			if(k)--k;
			if(rank[i])for(j=sa[rank[i]-1];str[i+k]==str[j+k];++k);
			else k=0;
			height[rank[i]]=k;
		}
	}
	void initRMQ()
	{
		int i,j;
		REP(i,len)best[i][0]=height[i];
		for(j=1;(1<<j)-1<len;++j)
			for(i=0;i+(1<<j)-1<len;++i)
				best[i][j]=min(best[i][j-1],best[i+(1<<(j-1))][j-1]);
	}
	int RMQ(int l, int r)
	{
		int k=0;
		while(l+(1<<k)-1+1<r-(1<<k)+1)++k;
		return min(best[l][k],best[r-(1<<k)+1][k]);
	}
	int LCPSA(int a, int b)
	{
		if(a==b)return len-sa[a];
		if(++a>b)swap(a,b);
		return RMQ(a,b);
	}
};
suffixarray<int,N> msa;
map<int ,int > mymap;
int s[N];
int main()
{
    int i,n,m,k,ans;
    while(~scanf("%d%d",&n,&k))
    {
        mymap.clear();
        m=1;
        REP(i,n)
        {
            scanf("%d",&s[i]);
            if(mymap.find(s[i])==mymap.end())
                mymap[s[i]]=m++;
            s[i]=mymap[s[i]];
        }
        msa.initSA(s,n,m);
        msa.initRMQ();
        ans=0;
        REP(i,n)
            if(i+k-1<n)
                ans=max(ans,msa.LCPSA(i,i+k-1));
        printf("%d",ans);
    }
	return 0;
}


你可能感兴趣的:(POJ 3261 Milk Patterns(后缀数组))