2222

思路:

  ac自动机模板题;

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005 struct TreeNodeType {
int count; TreeNodeType *fail; TreeNodeType *next[]; TreeNodeType()
{
fail=NULL,count=;
for(int i=;i<;i++) next[i]=NULL;
}
};
struct TreeNodeType *root,*que[maxn]; int n; char str[maxn*],word[]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void insert(char *ch)
{
int temp,len=strlen(ch);
TreeNodeType *p=root;
for(int i=;i<len;i++)
{
temp=ch[i]-'a';
if(p->next[temp]==NULL) p->next[temp]=new TreeNodeType;
p=p->next[temp];
}
p->count++;
} void build()
{
int tail=,head=;que[head]=root;
while(head<tail)
{
TreeNodeType *p=que[head++],*temp=NULL;
for(int i=;i<;i++)
{
if(p->next[i]==NULL) continue;
if(p==root) p->next[i]->fail=root;
else
{
temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) p->next[i]->fail=root;
}
que[tail++]=p->next[i];
}
}
} int query()
{
int pos,len=strlen(str),res=;TreeNodeType *p=root;
for(int i=;i<len;i++)
{
pos=str[i]-'a';
while(p->next[pos]==NULL&&p!=root) p=p->fail;
if(p->next[pos]!=NULL) p=p->next[pos];else p=root;
TreeNodeType *temp=p;
while(temp!=root&&temp->count!=-) res+=temp->count,temp->count=-,temp=temp->fail;
}
return res;
} int main()
{
int T;
in(T);
while(T--)
{
root=new TreeNodeType;
in(n);for(int i=;i<=n;i++) gets(word),insert(word);
build(),scanf("%s",str),printf("%d\n",query());
}
return ;
}

最新文章

  1. 使用Monit监控本地进程
  2. bootstrap datetimepicker 中只显示年或者只显示月份
  3. mysql高可用之DRBD + HEARTBEAT + MYSQL
  4. MySQL的分页
  5. 在linux中的virtualbox无法挂载usb设备的解决方法
  6. 2016 Multi-University Training Contest 5 ATM Mechine
  7. ubuntu为Python添加默认搜索路径
  8. Fedora安装
  9. android--HttpURLConnection(转载)
  10. 【BZOJ1861】【splay】Book 书架
  11. USACO Milk2 区间合并
  12. .Net程序猿玩转Android开发---(7)相对布局RelativeLayout
  13. .net设计模式之装饰模式
  14. Git的安装使用和基本命令(一)
  15. 人工神经网络,支持任意数量隐藏层,多层隐藏层,python代码分享
  16. 腾讯技术分享:GIF动图技术详解及手机QQ动态表情压缩技术实践
  17. 计算机基础:计算机网络-chapter2
  18. Visual studio中编译和使用libpng和zlib
  19. STL基础--算法(不修改数据的算法)
  20. JQuery字符串的操作

热门文章

  1. vue.js的特点-1
  2. [STL] STL各容器实现原理
  3. 【bzoj1455】罗马游戏 可并堆+并查集
  4. 【bzoj2007】[Noi2010]海拔 最小割+对偶图+最短路
  5. [Leetcode] Path Sum II路径和
  6. wya费用流
  7. [学习笔记]可持久化数据结构——数组、并查集、平衡树、Trie树
  8. HZOI String STL的正确用法
  9. Codeforces Round #525 (Div. 2)B. Ehab and subtraction
  10. barba.js 优化页面跳转用户体验