[SDOI2008]Sandy的卡片

Description
Sandy和Sue的热衷于收集干脆面中的卡片。然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡片兑换超炫的人物模型。每一张卡片都由一些数字进行标记,第i张卡片的序列长度为Mi,要想兑换人物模型,首先必须要集够N张卡片,对于这N张卡片,如果他们都有一个相同的子串长度为k,则可以兑换一个等级为k的人物模型。相同的定义为:两个子串长度相同且一个串的全部元素加上一个数就会变成另一个串。Sandy的卡片数远远小于要求的N,于是Sue决定在Sandy的生日将自己的卡片送给Sandy,在Sue的帮助下,Sandy终于集够了N张卡片,但是,Sandy并不清楚他可以兑换到哪个等级的人物模型,现在,请你帮助Sandy和Sue,看看他们最高能够得到哪个等级的人物模型。

Input
第一行为一个数N,表示可以兑换人物模型最少需要的卡片数,即Sandy现在有的卡片数
第i+1行到第i+N行每行第一个数为第i张卡片序列的长度Mi,之后j+1到j+1+Mi个数,用空格分隔,分别表示序列中的第j个数
n<=1000,M<=1000,2<=Mi<=101

Output
一个数k,表示可以获得的最高等级。

Sample Input
2
2 1 2
3 4 5 9

Sample Output
2


这种集体加元素可以相等的……显然是用差分序列进行KMP

然后我们强行拎出一个,然后把其他串对其做KMP,取最大值

然后匹配可以从任意位置开始,所以我们初始时枚举位置,总复杂度为(O(n^2))

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1;char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1;char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)    putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e3,M=1e2;
int S[N+10],T[N+10][M+10],len[N+10];
int main(){
	int Q=read(),n=read(),Last=read(),Ans=0;
	for (int i=1,x;i<n;i++){
		S[i]=Last-(x=read());
		Last=x;
	}
	for (int i=1;i<Q;i++){
		len[i]=read(); int Last=read();
		for (int j=1,x;j<len[i];j++){
			T[i][j]=Last-(x=read());
			Last=x;
		}
		len[i]--;
	}n--;
	for (int k=0;k<n;k++){
		static int Nxt[N+10]; int res=inf;
		memset(Nxt,0,sizeof(Nxt));
		Nxt[0]=-1;
		for (int i=2,j=0;i<=n-k;i++){
			while (~j&&S[i+k]!=S[j+k+1])	j=Nxt[j];
			Nxt[i]=++j;
		}
		for (int l=1;l<Q;l++){
			int tmp=0;
			for (int i=1,j=0;i<=len[l];i++){
				while (~j&&T[l][i]!=S[j+k+1])	j=Nxt[j];
				tmp=max(tmp,++j);
				if (j==n-k)	break;
			}
			res=min(res,tmp+1);
		}
		Ans=max(Ans,res);
	}
	printf("%d
",Ans);
	return 0;
}
原文地址:https://www.cnblogs.com/Wolfycz/p/10484713.html