http://acm.hdu.edu.cn/showproblem.php?pid=2222

题目大意:

给定 n 个长度不超过 50 的由小写英文字母组成的单词,以及一篇长为 m 的文章,问有多少个单词在文章中出现了,多组数据。

——————————————————————

AC自动机裸题。

(C++AC,G++TLE我能说什么……)

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
const int N=1e4+;
const int M=1e6+;
const int L=+;
struct trie{
int ed;
int a[];
int fail;
void clear(){
ed=fail=;
memset(a,,sizeof(a));
return;
}
}tree[N*L]={};
char s[M];
int cnt=;
inline void insert(){
int now=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
if(!tree[now].a[t]){
cnt++;
tree[now].a[t]=cnt;
tree[cnt].clear();
}
now=tree[now].a[t];
}
tree[now].ed++;
return;
}
int q[N*L];
void getfail(){
int r=;
//以下是对第一层的特殊处理
for(int i=;i<;i++){
if(tree[].a[i]!=){
tree[tree[].a[i]].fail=;
r++;q[r]=tree[].a[i];
}
}
for(int l=;l<=r;l++){
int u=q[l];
for(int i=;i<;i++){
if(tree[u].a[i]!=){
tree[tree[u].a[i]].fail=tree[tree[u].fail].a[i];
r++;q[r]=tree[u].a[i];
}else{
tree[u].a[i]=tree[tree[u].fail].a[i];
}
}
}
return;
}
int check(){
int now=,ans=;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-;
now=tree[now].a[t];
for(int j=now;j&&tree[j].ed!=-;j=tree[j].fail){
ans+=tree[j].ed;
tree[j].ed=-;
}
}
return ans;
}
int main(){
int t;
cin>>t;
while(t--){
tree[].clear();
cnt=;
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>s;
insert();
}
getfail();
cin>>s;
cout<<check()<<endl;
}
return ;
}

HDU2222:Keywords Search——题解的更多相关文章

  1. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  3. JSOI2012 玄武密码 和 HDU2222 Keywords Search

    玄武密码 给若干模式串和一个文本串.求每个模式串在文本串上能匹配的最大前缀长度. N<=10^7,M<=10^5,每一段文字的长度<=100. jklover的题解 将模式串建成一个 ...

  4. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  5. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. hdu----(2222)Keywords Search(trie树)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU2222 Keywords Search(AC自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. hdu2222 Keywords Search【AC自动机】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  10. AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)

    首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...

随机推荐

  1. docker 应用场景

    内容来自知乎.先mark,后续再研究 0.无痛尝试新事物 这应该是最早让我感受到docker的便利性的使用场景了. 以前,如果想尝试新的编程语言/数据库/命令行工具,会先找找apt的源里有没有相应的包 ...

  2. Ruby 基础教程1-9

    异常 1.异常结构      [ begin]          ...     rescue         [retry]          ...     [ensure]          . ...

  3. OSG-漫游

    本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...

  4. 第四十篇 Python之设计模式总结-简单工厂、工厂方法、抽象工厂、单例模式

    一. 简单工厂 简单工厂模式(Simple Factory Pattern):是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 简单工厂的用处不大,主要就是一个if... ...

  5. Linux命令应用大词典-第7章 字符串、文件和命令查找

    7.1 grep:字符串.文件和命令的查找 7.2 egrep:在文件或标准输入中查找模式 7.3 fgrep:在每个文件或是标准输入中查找模式 7.4 find:列出文件系统内符合条件的文件 7.5 ...

  6. JavaWeb--------JSP语法基础学习(特别适合入门)

    准备工作: 需要Tomcat8.0,MyEclipse,JDK JSP是一种运行在服务器端的脚本语言,JSP页面又是基于HTML网页的程序,它是Java Web 开发技术的基础. 基本内容: JSP页 ...

  7. ionic typescript--验证码发送倒计时功能

    1.新建页面 ionic g page forget   2.mode.html文件 <ion-item> <ion-input clearInput [(ngModel)]='co ...

  8. [转]bashrc与profile区别

    作者:KornLee 2005-02-03 15:49:57 来自:Linux先生 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/pro ...

  9. LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告

    题目 题目链接 Given a non-empty string check if it can be constructed by taking a substring of it and appe ...

  10. Linux系统inotify工具安装配置

    inotify主要功能 Inotify 是一个 Linux特性,它监控文件系统操作,比如读取.写入和创建.Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多.学习如何将 ...