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. 聊一聊Android 6.0的运行时权限

    Android 6.0,代号棉花糖,自发布伊始,其主要的特征运行时权限就很受关注.因为这一特征不仅改善了用户对于应用的使用体验,还使得应用开发者在实践开发中需要做出改变. 没有深入了解运行时权限的开发 ...

  2. Json字符串与字典互转

    #pragma mark 转换json字符串 +(NSString *)toJSON:(id)aParam { NSData   *jsonData=[NSJSONSerialization data ...

  3. swift 关于delegate

    Cocoa 开发中接口-委托 (protocol-delegate) 模式是一种常用的设计模式,它贯穿于整个 Cocoa 框架中,为代码之间的关系清理和解耦合做出了不可磨灭的贡献. 在 ARC 中,对 ...

  4. QT Windows下生成动态链接库

    目标:需要将一个QT程序生成动态链接库 Windows环境下Qt生成的共享库文件其后缀为dll,可以在程序运行过程中动态加载 新建项目,选择库 选择共享库 建立好项目后生成三个文件,两个.h一个.cp ...

  5. 第四篇:python 高级之面向对象初级

    python 高级之面向对象初级   python 高级之面向对象初级 本节内容 类的创建 类的构造方法 面向对象之封装 面向对象之继承 面向对象之多态 面向对象之成员 property 1.类的创建 ...

  6. datazen 修改instanceid db_encryption_key

    切换到Datazen.Enterprise.Server.3.0.3327.exe 所在的目录 运行如下命令: Datazen.Enterprise.Server.3.0.3327.exe DATAZ ...

  7. JAVA HashMap详细介绍和示例

    http://www.jb51.net/article/42769.htm 我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.   第1部分 HashMa ...

  8. oracle中用comment on的用法

    oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON  { TABLE [ schema. ]    { table | view }  | COLUMN [ s ...

  9. javascript 之DOM篇

    要怎么样的开场白才能使我有力气再更新学习进度呢?啊啊啊啊啊,表示好累啊~~~默念“棒棒棒,我最棒~”召唤精气神开总结敲字咯.哈哈哈. --------------------------------- ...

  10. Spring配置多数据源错误总结

    由于系统需要调用多个数据源包含mysql,sqlServe和Oracle,所以要在Spring的xml文件中配置多数据源,一下是配置过程中常见的错误: 1.配置的是mysql的数据源,却报oracle ...