kmp+字典树
#include <iostream>
#include <cstring>
#include <queue>
using namespace std ;
const int N=1e4+2,M=1e6+2;
char s[M];
int val[N];
int ch[N][30],tot=1;
void insert(char *s){
int i,len=strlen(s);
int u=1;
for(i=0;i<len;i++){
int c=s[i]-'a';
if(ch[u][c]==0)
ch[u][c]=++tot,memset(ch[tot],0,sizeof ch[tot]);
u=ch[u][c];
}
val[u]++;
}
int ans=0;
int pre[N];
void init(){
queue<int> q;
int i,u;
q.push(1); pre[1]=0;
for(i=0;i<26;i++) ch[0][i]=1;
while(!q.empty()){
u=q.front(); q.pop();
for(i=0;i<26;i++){
if(ch[u][i]==0) ch[u][i]=ch[pre[u]][i];
else{
q.push(ch[u][i]);
int y=pre[u];
pre[ch[u][i]]=ch[y][i];
}
}
}
}
int find(char *s){
int i,len=strlen(s);
int u=1;
for(i=0;i<=len;i++){
int c=s[i]-'a',k=ch[u][c];
while(k>1){
ans+=val[k];
val[k]=0;
k=pre[k];
}
u=ch[u][c];
}
return 0;
}
int n;
char tmp[55];
int main(){
int cas;
cin>>cas;
while(cas--){
tot=1;
memset(ch,0,sizeof ch);
memset(val,0,sizeof val);
for(int i=0;i<26;i++) ch[0][i]=1,ch[1][i]=0;
cin>>n;
for(int i=1;i<=n;i++) cin>>tmp,insert(tmp);
cin>>s;
init();
ans=0;
find(s);
cout<<ans<<endl;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!