AC自动机基础题。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXL 1000005
#define TRIEN 26 char des[MAXL], src[]; typedef struct Trie {
int n;
Trie *fail;
Trie *next[TRIEN];
Trie () {
n = ;
fail = NULL;
memset(next, , sizeof(next));
}
} Trie; Trie *root;
int n; void create(char str[]) {
int i=, id;
Trie *p = root, *q; while (str[i]) {
id = str[i] - 'a';
++i;
if (p->next[id] == NULL) {
q = new Trie();
p->next[id] = q;
}
p = p->next[id];
}
p->n++;
} void build_fail() {
int i;
Trie *p, *q;
queue<Trie *> que; for (i=; i<TRIEN; ++i) {
if (root->next[i]) {
root->next[i]->fail = root;
que.push(root->next[i]);
}
} while (!que.empty()) {
p = que.front();
que.pop();
for (i=; i<TRIEN; ++i) {
if (p->next[i]) {
q = p->fail;
while (q != NULL) {
if (q->next[i]) {
p->next[i]->fail = q->next[i];
break;
}
q = q->fail;
}
if (q == NULL)
p->next[i]->fail = root;
que.push(p->next[i]);
}
}
}
} void search() {
int i = , id;
Trie *p = root, *tmp; n = ; while (des[i]) {
id = des[i] - 'a';
++i;
while (p->next[id]==NULL && p!=root)
p = p->fail;
p = p->next[id];
if (p == NULL)
p = root;
tmp = p;
while (tmp != root) {
n += tmp->n;
tmp->n = ;
tmp = tmp->fail;
}
}
} void del(Trie *t) {
if (t == NULL)
return ;
for (int i=; i<TRIEN; ++i)
del(t->next[i]);
free(t);
} int main() {
int case_n; scanf("%d", &case_n);
while (case_n--) {
scanf("%d", &n);
root = new Trie();
while (n--) {
scanf("%s", src);
create(src);
}
scanf("%s", des);
build_fail();
search();
printf("%d\n", n);
del(root);
} return ;
}

【HDOJ】2222 Keywords Search的更多相关文章

  1. 【HDU】2222 Keywords Search

    [算法]AC自动机 [题解]本题注意题意是多少关键字能匹配而不是能匹配多少次,以及可能有重复单词. 询问时AC自动机与KMP最大的区别是因为建立了trie,所以对于目标串T与自动机串是否匹配只需要直接 ...

  2. 【HDU】2222 Keywords Search(AC自动机)

    题目 传送门:QWQ 分析 $ AC $自动机模板,黈力的码风真的棒极了,这是我抄他的. 还有 题号不错 代码 #include <cstdio> #include <cstring ...

  3. 【HDOJ】1648 Keywords

    PE的注意,如果没有满足条件的不输出空格.简单模拟,暴力解. /* */ #include <iostream> #include <sstream> #include < ...

  4. 【HDOJ】2279 File Search Tool

    显然适用字典树建树,串长和模式串都很小,所以直接递归搜索.同时,适用bk标记当前的查询次数(排除不同模式的多次查询成功,如*t*).需要主要的是,居然存在同名文件!!!. /* 2279 */ #in ...

  5. AC自动机 HDOJ 2222 Keywords Search

    题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零.   新模板,加上last跑快一倍 #include <bits/stdc++.h> ...

  6. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  7. hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】

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

  8. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  9. HDOJ 2222: Keywords Search

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

随机推荐

  1. /proc/sysrq-trigger

    立即重启计算机      echo "b" > /proc/sysrq-trigger 立即关闭计算机      echo "o" > /proc/ ...

  2. jersey + tomcat 实现restful风格

    本文参考 http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html 环境: idea 15.0.2 jersey 1.3 tom ...

  3. 【转载】ASP.NET支持多语言

    ASP.NET 2.0中实现:1.使用工具自动生成本地化资源(LocalResources) 首先建立一个WEB工程,如图所示:双击Default.aspx,切换到[设计]视图,从工具箱里拖一个But ...

  4. PowerDesigner15的安装和破解

    一.PowerDesigner15的安装 运行安装包,出现如下安装界面

  5. 多目标遗传算法 ------ NSGA-II (部分源码解析) 二进制编码的个体解码操作 decode.c

    种群解码函数  decode_pop  为包装函数, 核心调用函数为  decode_ind  , 对每个个体进行解码. /* Routines to decode the population */ ...

  6. codevs 3044 矩形面积求并 (扫描线)

    /* 之前一直偷懒离散化+暴力做着题 今天搞一下扫描线 自己按照线段树的一般写法写的有些问题 因为不用于以前的区间sum so 题解搬运者23333 Orz~ 去掉了打标记的过程 同时更新区间的时候先 ...

  7. Unity3D 创建一个简单的2D游戏

    开始研究Unity3d 中的2D游戏. 首先创建出一个项目: 然后创建出一个场景: 然后添加一个背景: 然后创建一个主人公对象: 可以是自己做的素材,也可以是用unity裁剪的素材, 下面贴出裁剪素材 ...

  8. 解决kernel headers报错

    Make sure you have updated version $ sudo apt-get update Search for kernel version (optional) $ apt- ...

  9. CI 笔记4 (easyui 手风琴)

    添加父div标签,和子div标签 <div class="easyui-accordion" data-options="fit:true,border:false ...

  10. Zend Server安装后首次运行就出现Internal Server Error的解决

    无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...