传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1212
对每一个串做匹配,把匹配上的区间拿出来,问题转化为给定多个线段,求最长的,不相交的,首尾相连的线段覆盖,nlogn的dp
好像别人都是Trie上dp……而且要想叉我也非常简单……不过利用数据漏洞也应该是OI选手的基本素质吧233
Code:
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int maxlen=1024*1024+5;
int n,m,len;
typedef unsigned int UL;
typedef pair<int,int> pii;
UL hash[maxlen],hash_l[maxlen];
UL H[21],length[21];
UL base=233;
char str[maxlen];
pii lines[maxlen*4];
short f[maxlen];
int size;
void hash_init(const char *s,int len){
UL val=0;size=0;
for(int i=0;i<len;++i)val=(val*base+s[i]-'0'),hash[i]=val;
for(int i=1;i<=n;i++){
for(int j=0;j<len-length[i]+1;j++){
UL key=(hash[j+length[i]-1]-(j?hash[j-1]*hash_l[length[i]]:0));
if(key==H[i]){
size++;
lines[size].x=j;
lines[size].y=j+length[i]-1;
}
}
}
}
bool cmp(pii a,pii b){return a.y<b.y||(a.y==b.y&&a.x<b.x);}
void dp(int len){
sort(lines+1,lines+1+size,cmp);
memset(f,0,sizeof f);
for(int i=1;i<=size;i++){
if(lines[i].x==0){f[lines[i].y]=1;continue;}
int ps=lower_bound(lines+1,lines+1+size,pii(-1,lines[i].x-1),cmp)-lines;
for(;lines[ps].y==lines[i].x-1;ps++){
f[lines[i].y]|=lines[ps].x?f[lines[ps].x-1]:1;
if(f[lines[i].y])break;
}
}for(int i=len;i>=1;i--)if(f[i-1]){printf("%d\n",i);return;}
puts("0");
}
int main(){
hash_l[0]=1;
for(int i=1;i<maxlen;i++)hash_l[i]=(hash_l[i-1]*base);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
char c=getchar();int len=0;
while(!isalpha(c))c=getchar();
while(isalpha(c))str[len++]=c,c=getchar();
length[i]=len;UL val=0;
for(int j=0;j<len;j++)val=(val*base+str[j]-'0');H[i]=val;
}
while(m--){
char c=getchar();int len=0;
while(!isalpha(c))c=getchar();
while(isalpha(c))str[len++]=c,c=getchar();
hash_init(str,len);
dp(strlen(str));
}
return 0;
}