题目大意
给出n个操作序列,每个操作序列包括两种操作,加一个字符和删除最后一个字符。中途所出现的串的子串成为这操作序列的生成子串,给定一个操作序列集,求其生成子串的交集,串的最长的长度。
解题思路
对每个操作序列建sam,每加入一个字符记录一下前驱,退格的时候是直接退,其他按sam的增量构建法来建。特别注意新加字符的时候,原本可能就有转移,我们看一下他们的mx是否相邻,否则就另建新点,同nq的情况。
对于不同的操作序列,只要从头开始即可。每个状态需标记是由哪些操作集合sta得到,一个状态的sta一定是fa的sta的子集,完成构建后更新即可。
对每个操作集合sta都更新一下mx。再用sta更新sta的子集的答案即可。
code
using namespace std;
int const mn=1e6+9,mp=2*1e6+9,inf=1e9+7;
int n,pon=1,ans[mp],pre[mp],f[mp],fa[mp],mx[mp],son[mp][26],
qu[mp],cnt[mp];
char s[mn];
int main(){
//freopen("wechat.in","r",stdin);
//freopen("wechat.out","w",stdout);
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%d\n",&n);
fo(cas,1,n){
scanf("%s\n",s+1);int len=strlen(s+1),last=1,val=1<<(n-cas);
fo(i,1,len)if(s[i]=='<')last=pre[last];
else{
int p=last,ch=s[i]-'a';
if(son[p][ch]){
int q=son[p][ch];
if(mx[p]+1==mx[q])last=q,pre[q]=p,f[q]|=val;
else{
int nq=last=++pon;mx[nq]=mx[p]+1;
pre[nq]=p;f[nq]|=val;
fa[nq]=fa[q];fa[q]=nq;
fo(j,0,25)son[nq][j]=son[q][j];
for(;son[p][ch]==q;p=fa[p])son[p][ch]=nq;
}
continue;
}
int np=last=++pon;pre[np]=p;f[np]|=val;mx[np]=mx[p]+1;
for(;p&&(!son[p][ch]);p=fa[p])son[p][ch]=np;
if(!p){fa[np]=1;continue;}
int q=son[p][ch];
if(mx[p]+1==mx[q]){fa[np]=q;continue;}
int nq=++pon;mx[nq]=mx[p]+1;
fa[nq]=fa[q];
fa[np]=fa[q]=nq;
fo(j,0,25)son[nq][j]=son[q][j];
for(;son[p][ch]==q;p=fa[p])son[p][ch]=nq;
}
}
fo(i,1,pon)cnt[mx[i]]++;
fo(i,1,pon)cnt[i]+=cnt[i-1];
fd(i,pon,1)qu[cnt[mx[i]]--]=i;
fd(i,pon,1){
int now=qu[i],tmp=f[now];
f[fa[now]]|=tmp;
ans[tmp]=max(ans[tmp],mx[now]);
}
int mx=(1<<n)-1;
fd(i,mx,0)fo(j,0,n-1)ans[i]=max(ans[i],ans[i|(1<<j)]);
int q;scanf("%d\n",&q);
fo(i,1,q){
int x=0;char ch;
fo(j,1,n)ch=getchar(),x=(x<<1)+ch-'0';
scanf("\n");
printf("%d\n",ans[x]);
}
return 0;
}