题目
题解
其实这题也非常简单,
想到一种非常非常暴力的做法,
用所有的t串,建出一棵tire,
然后每个节就将它子树里面所有的结尾作为1,然后求出全0串。
其实正解的思路差不多,
每个节点维护一个lst和v,
顺序将t插入,
假设当前
ti
t
i
遍历到点x,
那么就
vx=max(vx,i−lstx−1),lstx=i
v
x
=
m
a
x
(
v
x
,
i
−
l
s
t
x
−
1
)
,
l
s
t
x
=
i
这个很好理解吧。
code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#define ll long long
#define N 8000003
#define M 1003
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
ll w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}
int max(int a,int b){return a<b?b:a;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}
int v[N],z[M][M],son[3][N],tot,lst[N],n,m,x,y;
int main()
{
freopen("word.in","r",stdin);
freopen("word.out","w",stdout);
read(n);read(m);tot=1;
for(int i=1;i<=n;i++)
{
for(;ch<'a' || ch>'c';ch=G());
for(x=1;'a'<=ch && ch<='c';ch=G())
{
v[x]=max(v[x],i-lst[x]-1);
lst[x]=i;
if(son[ch-'a'][x]==0)son[ch-'a'][x]=++tot;
x=son[ch-'a'][x];
}
v[x]=max(v[x],i-lst[x]-1);
lst[x]=i;
}
for(int i=1;i<=tot;i++)
v[i]=max(v[i],n-lst[i]);
for(int i=1;i<=m;i++)
{
for(;ch<'a' || ch>'c';ch=G());y=1;
for(x=1;'a'<=ch && ch<='c';ch=G())
{
if(son[ch-'a'][x]==0)y=0;
x=son[ch-'a'][x];
}
if(y)write(v[x]);else write(n);
P('\n');
}
return 0;
}