先在后缀自动机上匹配
求出mat[i]表示作文的每个位置i为结尾最长能匹配多长
二分L
设f[i]表示前i个字符熟悉的部分最多有多长
那么f[i]=max(f[i-1],f[j]+i-j) 其中j要满足i-j>=L&&i-j+1<=mat[i]
得i-mat[i]<=j<=i-L
而mat[i]+1>=mat[i+1]
得i+1-mat[i+1]>=i-mat[i]
所以i-mat[i]单调不减 用单调队列优化DP
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline int read(char *s){
char c=nc(); int len=0;
for (;!(c>='0' && c<='1');c=nc());
for (;c>='0' && c<='1';s[++len]=c,c=nc()); s[++len]=0; return len-1;
}
const int N=2200005;
struct state{
int link,len,next[3];
}st[N];
int ncnt,last;
inline void Extend(int c){
int cur=++ncnt,p;
st[cur].len=st[last].len+1;
for (p=last;p!=-1 && !st[p].next[c];p=st[p].link)
st[p].next[c]=cur;
if (p==-1)
st[cur].link=0;
else{
int q=st[p].next[c];
if (st[p].len+1==st[q].len)
st[cur].link=q;
else{
int nq=++ncnt;
st[nq].link=st[q].link;
st[nq].len=st[p].len+1;
for (int i=0;i<3;i++) st[nq].next[i]=st[q].next[i];
for (;p!=-1 && st[p].next[c]==q;p=st[p].link)
st[p].next[c]=nq;
st[q].link=st[cur].link=nq;
}
}
last=cur;
}
int mat[N],f[N];
char str[N]; int len;
int l,r,Q[N];
inline bool check(int mid){
l=r=-1;
for (int i=1;i<=len;i++){
f[i]=f[i-1];
while (l<r && Q[l+1]<i-mat[i]) Q[++l]=0;
if (i-mid>=i-mat[i]){
while (l<r && f[Q[r]]-Q[r]<=f[i-mid]-(i-mid)) Q[r--]=0;
Q[++r]=i-mid;
}
if (l<r)
f[i]=max(f[i],f[Q[l+1]]+i-Q[l+1]);
}
return 10*f[len]>=9*len;
}
int n,m;
int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(m);
st[0].link=-1;
for (int i=1;i<=m;i++){
len=read(str);
for (int j=1;j<=len;j++) Extend(str[j]-'0');
Extend(2);
}
while (n--){
len=read(str);
int p=0,l=0;
for (int i=1;i<=len;i++){
while (p && !st[p].next[str[i]-'0'])
p=st[p].link,l=st[p].len;
if (st[p].next[str[i]-'0'])
p=st[p].next[str[i]-'0'],l++;
mat[i]=l;
}
int L=0,R=len+1,MID;
while (L+1<R)
if (check(MID=(L+R)>>1))
L=MID;
else
R=MID;
printf("%d\n",L);
}
return 0;
}