题目链接

题意:每个文本串的出现次数

分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零。   新模板,加上last跑快一倍

#include <bits/stdc++.h>

struct AC {
static const int NODE = 10000 * 50 + 5;
static const int SIZE = 26;
int ch[NODE][SIZE], fail[NODE], last[NODE];
int end[NODE];
int sz; void clear() {
memset (ch[0], 0, sizeof (ch[0]));
end[0] = 0;
sz = 1;
}
int idx(char ch) {
return ch - 'a';
}
void insert(char *str) {
int u = 0;
for (int c, i=0; str[i]; ++i) {
c = idx (str[i]);
if (!ch[u][c]) {
memset (ch[sz], 0, sizeof (ch[sz]));
end[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
end[u]++;
}
void build() {
fail[0] = 0;
std::queue<int> que;
for (int c=0; c<SIZE; ++c) {
int u = ch[0][c];
if (u) {
fail[u] = 0;
last[u] = 0;
que.push (u);
}
}
while (!que.empty ()) {
int r = que.front (); que.pop ();
for (int c=0; c<SIZE; ++c) {
int u = ch[r][c];
if (!u) {
ch[r][c] = ch[fail[r]][c];
} else {
fail[u] = ch[fail[r]][c];
last[u] = end[fail[u]] ? fail[u] : last[fail[u]];
que.push (u);
}
}
}
}
int query(char *text) {
int ret = 0, u = 0;
for (int c, i=0; text[i]; ++i) {
c = idx (text[i]);
u = ch[u][c];
int t = u;
while (t) {
ret += end[t];
end[t] = 0;
t = last[t];
}
}
return ret;
}
};
AC ac;
char p[55], t[1000010]; int main(void) { //HDOJ 2222 Keywords Search
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
ac.clear ();
for (int i=1; i<=n; ++i) {
scanf ("%s", p); ac.insert (p);
}
ac.build (); scanf ("%s", t);
printf ("%d\n", ac.query (t));
} return 0;
}

AC自动机 HDOJ 2222 Keywords Search的更多相关文章

  1. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

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

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

  3. HDOJ 2222: Keywords Search

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

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

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

  5. 【AC自动机】hdu2222 Keywords Search

    AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...

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

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

  7. HDU 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 (AC自动机)

    题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...

  9. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

随机推荐

  1. django 简易博客开发 1 安装、创建、配置、admin使用

    首先贴一下项目地址吧  https://github.com/goodspeedcheng/sblog 到现在位置项目实现的功能有: 1.后台管理使用Admin ,前端显示使用bootstrap 2. ...

  2. curl 发送json请求

    curl 发送json请求 这个是在cmd环境下的输入:注意{\"userName\":\"helo\",\"id\":1}中间不能有空格 ...

  3. 转: 将Eclipse代码导入到AndroidStudio的两种方式

    评注: 讲解的非常之详细 转自:    http://www.cnblogs.com/ct2011/p/4183553.html 说到使用AndroidStudio,除了新建的项目,我们都会面临的问题 ...

  4. http://vdceye.com/ 全新页面上线

    vdceye manager home page

  5. VB6 如何添加自定义函数 模块 把代码放到一个模块中

    1 工程-添加模块,在右侧工程视图中可以发现多了一个Module1   2 比如我在这个模块中自定义两个函数,分别为写入和读取INI的函数   3 则在主程序中已经可以直接调用  

  6. Pacemaker 安装与使用

    Pacemaker 仅仅做资源管理器(CRM).底下的消息系统採用 corosync. 安装 以 ubuntu 为例, sudo aptitude install -y pacemaker coros ...

  7. Python中字符运算的优先级

    表1-2 运算符优先级 运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in,not in 成员测试 is,is not 同一性测试 &l ...

  8. web爬虫之登录google paly 商店

    我们先打开Google play 首页 ,点击右上角"登陆"button,即跳到登陆页面 每次我要用爬虫的方式来登陆某个站点的时候,我都会先随便输入一个账号password点击登陆 ...

  9. [IT新应用]brave浏览器

    https://www.brave.com/about.html The web has become a different place. With the ad-tech ecosystem ou ...

  10. 再谈HBase八大应用场景

    HBase概述 HBase是一个分布式存储.数据库引擎,可以支持千万的QPS.PB级别的存储,这些都已经在生产环境验证,并且在广大的公司已经验证.特别是阿里.小米.京东.滴滴内部都有数千.上万台的HB ...