问题描述

LG5357


题解

不是fail树AC自动机复杂度是假的。

AC自动机搞出来,建立Trie树,树上爆搜一遍就好了。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std; template <typename Tp>
void read(Tp &x){
x=0;char ch=1;int fh;
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
if(ch=='-') ch=getchar(),fh=-1;
else fh=1;
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=fh;
} int root,ch[200007][26],tot,n;
char s[2000003]; int pos[200007],val[200007],fail[200007];
int m,ed[200007]; int chk(char s){
return s-'a';
} void insert(int x){
int p=root;
for(int i=1;i<=n;i++){
int k=chk(s[i]);
if(!ch[p][k]) ch[p][k]=++tot;
p=ch[p][k];
}
++ed[p],pos[x]=p;
} void query(){
int p=root;
for(int i=1;i<=n;i++){
p=ch[p][chk(s[i])];
++val[p];
}
} void build(){
queue<int>q;
for(int i=0;i<26;i++){
if(ch[root][i]) q.push(ch[root][i]);
}
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<26;i++){
if(ch[x][i]) fail[ch[x][i]]=ch[fail[x]][i],q.push(ch[x][i]);
else ch[x][i]=ch[fail[x]][i];
}
}
} int Head[200007],Next[200007],cnt,to[200007]; void add(int x,int y){
to[++cnt]=y,Next[cnt]=Head[x],Head[x]=cnt;
} int size[200007]; void dfs(int x){
size[x]=val[x];
for(int i=Head[x];i;i=Next[i]){
dfs(to[i]);size[x]+=size[to[i]];
}
} int main(){
ios::sync_with_stdio(0);
cin>>m;
for(int i=1;i<=m;i++){
cin>>(s+1);n=strlen(s+1);
insert(i);
}
cin>>(s+1);n=strlen(s+1);
build();query();
for(int i=1;i<=tot;i++) add(fail[i],i);
dfs(root);
for(int i=1;i<=m;i++) cout<<size[pos[i]]<<endl;
return 0;
}

最新文章

  1. UnrealScript语言基础
  2. EasyUI datagrildview导出excel报表
  3. Linux学习总结
  4. phpcms不能后台编辑模板
  5. linux怎么模糊查找一个文件
  6. VS制作软件安装项目,版本控制和软件升级
  7. 黄聪:深入理解PHP Opcode缓存原理
  8. Linux下安装MySQL数据库以及用C语言编程存取数据库
  9. Android开发之xUtils-HttpUtils的使用
  10. 通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法
  11. CentOS7+Tomcat 生产系统部署
  12. 使用Eclipse开发及测试Spark的环境搭建及简单测试
  13. NSNotificationCenter机制
  14. windows环境搭建jira 详解
  15. spring的注解使用
  16. 微信小程序在开发中遇到的问题与解决方法
  17. MacOS下Rails+Nginx+SSL环境的搭建(上)
  18. 为什么重写了equals() 就要重写hashcode()
  19. ViZDoom深度预测(Depth Prediction)
  20. Structs复习 ActionMethod

热门文章

  1. 突然看到原来除了jar包还有war包啊?????
  2. c# 多线程 双色球
  3. Metersploit系统参数说明
  4. golang基础之初识
  5. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III
  6. MySQL使用crontab定时备份不执行问题
  7. yum 找不到程序,yum更换国内阿里源
  8. 【git】git命令集合
  9. 【JS】---4用JS获取地址栏参数方法
  10. Java自学-集合框架 LinkedList