bzoj 3172 后缀数组|AC自动机

时间:2023-03-08 16:01:38

  后缀数组或者AC自动机都可以,模板题。

/**************************************************************
Problem: 3172
User: BLADEVIL
Language: C++
Result: Accepted
Time:424 ms
Memory:34260 kb
****************************************************************/ //By BLADEVIL
#include <cstdio>
#include <cstring>
#define maxn 2000010
#define maxm 300 using namespace std; struct node{
int cnt;
node *fail,*child[];
node(){
cnt=; fail=NULL;
memset(child,,sizeof child);
}
} *que[maxn],*root=new(node),*w[maxm]; int n; int main(){
scanf("%d",&n);
for (int i=;i<=n;i++){
char c[maxn];
scanf("%s",&c);
int len=strlen(c);
node *t=root;
for (int j=;j<len;j++){
if (!t->child[c[j]-'a']) t->child[c[j]-'a']=new(node);
t=t->child[c[j]-'a']; t->cnt++;
}
w[i]=t;
}
int h=,t=;
root->fail=root; que[]=root;
while (h<t){
node *u=que[++h];
for (int i=;i<;i++) if (u->child[i]){
que[++t]=u->child[i];
node *v=u->fail;
que[t]->fail=v->child[i]&&v->child[i]!=que[t]?v->child[i]:root;
} else u->child[i]=u->fail->child[i];
}
for (int i=t;i;i--) que[i]->fail->cnt+=que[i]->cnt;
for (int i=;i<=n;i++) printf("%d\n",w[i]->cnt);
return ;
}
/**************************************************************
Problem: 3172
User: BLADEVIL
Language: C++
Result: Accepted
Time:6660 ms
Memory:107360 kb
****************************************************************/
//By BLADEVIL
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1001000
#define maxm 300 using namespace std; int n,len;
char s[maxn];
int sa[maxn],tsa[maxn],rk[maxn],trk[maxn],sum[maxn],h[maxn];
int w[maxn][];
int adr[maxm],len_s[maxm]; inline void getsa(int j) {
for (int i=;i<=len;i++) sum[i]=;
for (int i=;i<=len;i++) sum[rk[i+j]]++;
for (int i=;i<=len;i++) sum[i]+=sum[i-];
for (int i=len;i;i--) tsa[sum[rk[i+j]]--]=i; for (int i=;i<=len;i++) sum[i]=;
for (int i=;i<=len;i++) sum[rk[i]]++;
for (int i=;i<=len;i++) sum[i]+=sum[i-];
for (int i=len;i;i--) sa[sum[rk[tsa[i]]]--]=tsa[i];
} inline int query(int l,int r) {
int i,j;
for (i=,j=;(j<<)<=r-l+;j<<=,i++) ;
return min(w[l][i],w[r-j+][i]);
} int main() {
scanf("%d",&n); len=;
for (int i=;i<=n;i++) {
scanf("%s",s+len+);
adr[i]=len+;
len=strlen(s+);
s[++len]='^';
len_s[i]=len-adr[i];
}
//for (int i=1;i<=len;i++) printf("%c",s[i]); printf("\n");
for (int i=;i<=len;i++) sum[s[i]]++;
for (int i=;i<=;i++) sum[i]+=sum[i-];
for (int i=len;i;i--) sa[sum[s[i]]--]=i;
rk[sa[]]=;
for (int i=,p=;i<=len;i++) {
if (s[sa[i]]!=s[sa[i-]]) p++;
rk[sa[i]]=p;
}
for (int j=;j<=len;j<<=) {
getsa(j);
trk[sa[]]=;
for (int i=,p=;i<=len;i++) {
if (rk[sa[i]]!=rk[sa[i-]]||rk[sa[i]+j]!=rk[sa[i-]+j]) p++;
trk[sa[i]]=p;
}
for (int i=;i<=len;i++) rk[i]=trk[i];
}
for (int i=,j=;i<=len;i++) {
if (rk[i]==) continue;
while (i+j<=len&&sa[rk[i]-]+j<=len&&s[i+j]==s[sa[rk[i]-]+j]) j++;
h[rk[i]]=j;
if (j) j--;
}
//for (int i=1;i<=len;i++) printf("%d ",sa[i]); printf("\n");
//for (int i=1;i<=len;i++) printf("%d ",h[i]); printf("\n");
memset(w,,sizeof w);
for (int i=;i<=len;i++) w[i][]=h[i];
for (int j=,k=;j<;j++,k<<=)
for (int i=;i<=len;i++)
if (i+k<=len) w[i][j]=min(w[i][j-],w[i+k][j-]);
//for (int i=1;i<=len;i++) printf("%d ",w[i][2]); printf("\n");
//printf("%d\n",query(5,8));
//printf("%d\n",rk[6]);
for (int i=;i<=n;i++) {
int l=,r=rk[adr[i]],mid,left=rk[adr[i]],right=rk[adr[i]];
while (l<=r) {
mid=l+r>>;
if (query(mid+,rk[adr[i]])>=len_s[i])
left=mid,r=mid-; else l=mid+;
}
l=rk[adr[i]],r=len;
while (l<=r) {
mid=l+r>>;
if (query(rk[adr[i]]+,mid)>=len_s[i])
right=mid,l=mid+; else r=mid-;
}
printf("%d\n",right-left+);
}
return ;
}