单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了。

AC自己主动机的基础:

1 Trie。 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数

2 KMP,不是直接运用KMP。而是须要KMP的思想。KMP思想都没有的话,理解这个算法会更加吃力的。

注意本题的单词会有反复出现的,一个单词仅仅能统计一次。

搜索了一下网上的题解。发现好多代码都是一大抄的啊。⊙﹏⊙b汗。

本博客的乃是原创代码。代码风格也是几乎相同固定的,转载请注明出处:http://blog.csdn.net/kenden23。不少所谓的IT站点转载我的文章,不但链接没给出,连作者也没有,还好意思说自己是IT站点吗?

请尊重作者。假设觉得这些算法代码那么好敲的,能够自己去敲去。

#include <cstdio>

const int ARR_SIZE = 26;
const int MAX_N = 10001;
const int MAX_M = 1000001;
const int MAX_KEY_LEN = 51; struct Node
{
Node *arr[ARR_SIZE];
Node *fail;
int n;
}; void clearNode(Node *rt)
{
for (int i = 0; i < ARR_SIZE; i++)
{
rt->arr[i] = NULL;
}
rt->n = 0;
rt->fail = NULL;
} Node *q[MAX_KEY_LEN*MAX_N], pool[MAX_KEY_LEN*MAX_N], *Trie;
int head, tail, poolID; void insert(char *str)
{
Node *pCrawl = Trie;
for ( ; *str; str++)
{
int id = *str - 'a';
if (!pCrawl->arr[id])
{
pCrawl->arr[id] = &pool[poolID++];
clearNode(pCrawl->arr[id]);
}
pCrawl = pCrawl->arr[id];
}
pCrawl->n++;
} void buildFail()
{
Node *pCrawl;
head = tail = 0;
q[tail++] = Trie;
while (head < tail)
{
pCrawl = q[head++];
for (int i = 0; i < ARR_SIZE; i++)
{
if (pCrawl->arr[i] == NULL) continue;
pCrawl->arr[i]->fail = Trie;//initialize all to Trie
Node *fail = pCrawl->fail;
while (fail)
{
if (fail->arr[i])//find the first next up level match
{//which make it the longest match and the best.
pCrawl->arr[i]->fail = fail->arr[i];
break;
}
fail = fail->fail;
}//whi (p != NULL)
q[tail++] = pCrawl->arr[i];
}//for (int i = 0; i < kind; i++)
}//while (head < tail)
} int searchWordsInText(char *text)
{
Node *pCrawl = Trie;
int i = 0, ans = 0;
while (text[i])
{
int id = text[i++] - 'a';
//find the longest prefix match
while (!pCrawl->arr[id] && pCrawl != Trie) pCrawl = pCrawl->fail;
if (pCrawl->arr[id]) pCrawl = pCrawl->arr[id];
else continue; Node *tmp = pCrawl;
while (tmp && tmp->n != -1)
{//If one word apprear multiply times, only count as one time.
ans += tmp->n;
tmp->n = -1;
tmp = tmp->fail;
}//traval through all words that end with text[i], add them to result
}
return ans;
} int main()
{
int T, n;
char keyWord[MAX_KEY_LEN], text[MAX_M];
scanf("%d", &T);
while (T--)
{
Trie = &pool[0];
clearNode(Trie);
poolID = 1; scanf("%d", &n);
getchar();
while (n--)
{
gets(keyWord);
insert(keyWord);
}
gets(text);
buildFail();
printf("%d\n", searchWordsInText(text));
}
return 0;
}

HDU 2222 Keywords Search AC自己主动机入门题的更多相关文章

  1. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

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

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

  3. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

  4. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

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

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

  6. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  7. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  8. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  9. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

随机推荐

  1. LAMP自动安装脚本

    #!/bin/bash # 功能描述:LAMP自动安装脚本 # 初始化 if [ "$(cat /etc/system-release | awk '{print $(NF-1)}' | a ...

  2. python_传递任意数量的实参

    '''def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中 """打印所有姓名""" for i ...

  3. 常用的ES6方法

    常用的ES6方法 ES6之后,新增了定义变量的两个关键字,分别是let和const. let和const都能够声明块级作用域,用法和var是类似的,let的特点是不会变量提升,而是被锁在当前块中. 实 ...

  4. SQL SERVER-安装和卸载

    卸载后无法正常安装SQL SERVER 删除了本机的SQL SERVER以后,我发现我本机的SQL SERVER 再也安装不上了,这个一个比较严重的问题,要每天定时备份数据库到指定的地方才能防止数据丢 ...

  5. POJ 1430

    上面的估计是题解吧....呃,如果真要用到公式的话,确实没听过.... #include <iostream> #include <cstdio> #include <a ...

  6. javase复习

    一.总结封装 封装就是将数据和操作数据的方法绑定起来,通过private修饰数据,这样对数据的访问只能通过定义的操作数据的方法get/set来操作数据. 封装优点:1.由于将数据进行了封装,隐藏了不必 ...

  7. DICOM医学图像处理:fo-dicom网络传输之 C-Echo and C-Store

    背景: 上一篇博文对DICOM中的网络传输进行了介绍.主要參照DCMTK Wiki中的英文原文.通过对照DCMTK与fo-dicom两个开源库对DICOM标准的详细实现,对理解DICOM标准有一个更直 ...

  8. Service Mesh(服务网格)

    Service Mesh(服务网格) 什么是Service Mesh(服务网格)Service mesh 又译作 "服务网格",作为服务间通信的基础设施层.Buoyant 公司的 ...

  9. [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...

  10. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...