社区讨论
第一个点TLE了 求助~~
P3808AC 自动机(简单版)参与者 3已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @mi6y9f4h
- 此快照首次捕获于
- 2025/11/20 12:47 4 个月前
- 此快照最后确认于
- 2025/11/20 12:47 4 个月前
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,ans,tot,cnt=1;
int nxt[N],ch[N][30],bo[N],que[N];
void make(char *s){
int len=strlen(s),u=1;
for(int i=0;i<len;i++){
int c=s[i]-'a';
if(!ch[u][c]){
ch[u][c]=++cnt;
memset(ch[cnt],0,sizeof(ch[cnt]));
}
u=ch[u][c];
}
bo[u]++;
return;
}
void bfs(){
for(int i=0;i<=25;i++)
ch[0][i]=1;
que[1]=1;nxt[1]=0;
for(int q1=1,q2=1;q1<=q2;q1++){
int u=que[q1];
for(int i=0;i<=25;i++){
if(!ch[u][i]) ch[u][i]=ch[nxt[u]][i];
else{
que[++q2]=ch[u][i];
int v=nxt[u];
//while(v>0&&!ch[v][i]) v=next[v];
nxt[ch[u][i]]=ch[v][i];
}
}
}
}
void find(char *s){
int u=1,len=strlen(s),c,k;
for(int i=0;i<=len;i++){
c=s[i]-'a';
k=ch[u][c];
while(k>1){
ans+=bo[k];
bo[k]=0;
k=nxt[k];
}
u=ch[u][c];
}
return ;
}
int main(){
scanf("%d",&n);
char s[N<<1];
for(int i=1;i<=n;i++){
scanf("%s",s);
make(s);
}
bfs();
scanf("%s",s);
find(s);
printf("%d\n",ans);
return 0;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...