Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 51758    Accepted Submission(s):
16671

Problem Description
In the modern time, Search engine came into the life of
everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to
his image retrieval system.
Every image have a long description, when users
type some keywords to find the image, the system will match the keywords with
description of image and show the image which the most keywords be
matched.
To simplify the problem, giving you a description of image, and some
keywords, you should tell me how many keywords will be match.
 
Input
First line will contain one integer means how many
cases will follow by.
Each case will contain two integers N means the number
of keywords and N keywords follow. (N <= 10000)
Each keyword will only
contains characters 'a'-'z', and the length will be not longer than 50.
The
last line is the description, and the length will be not longer than
1000000.
 
Output
Print how many keywords are contained in the
description.
 
Sample Input

she
he
say
shr
her
yasherhs
 
Sample Output

 
Author
Wiskey
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
(转自 http://acm.hdu.edu.cn/showproblem.php?pid=2222)

  先讲讲大意,就是给一大堆模板串,再给一串很长的文本串,求有几个模板串在这个文本串中出现过
  首先,模板串的长度较短,数量多,极其符合ac自动机的特点,就这么愉快地决定使用ac自动机(其实也并不是特别地愉快
,虽说是裸题)
  接着注意几个事项
  1.模板串重复出现,比如下面这组数据

aa
bb
aa
aabbcc

  2.多组数据,第一个输入的数是数据组数(这个问题应该不大)

  3.如果用数组的话不要一次性用memset把整个数组都赋值,这样的话是十分浪费时间的

接着附上用指针写的AC自动机(虽说我用数组写了一个,调试了2天都没有调出来,果断重写)

Code

 /*
* acm.hdu.edu.cn
* Problem#2222
* Accepted
* Time:296ms
* Memory:58152k
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
#define SEG_SIZE 26
typedef class TrieNode{
public:
TrieNode* next[SEG_SIZE];
TrieNode* fail; //失配指针
TrieNode* last; //后缀链接
int value;
TrieNode():fail(NULL),last(NULL),value(){
memset(next, , sizeof(next));
}
}TrieNode;
typedef class Trie {
public:
TrieNode* root;
Trie(){
root = new TrieNode();
}
static int cti(char ch){ //将字符转换成Trie树中next数组的下标
return ch - 'a';
}
void insert(string s){
TrieNode *p = root;
for(int i = ;i < s.length();i++){
int c = cti(s[i]);
if(p->next[c] == NULL){
TrieNode *newNode = new TrieNode();
p->next[c] = newNode; //链接结点
}
p = p->next[c];
}
p->value++; //用结点的特殊值来存储有多少个单词是在这结尾
}
}Trie;
typedef class AhoMachine{
public:
Trie trie;
int count;
AhoMachine(){
trie = Trie();
count = ;
}
void getFail(){
queue<TrieNode*> que; //以bfs序构造状态转移图
trie.root->fail = trie.root;
for(int i = ;i < SEG_SIZE;i++){
TrieNode* pNode = trie.root->next[i];
if(pNode != NULL){
que.push(pNode);
pNode->fail = trie.root; //根节点的直接子节点的失配指针都是指向根节点
}
}
while(!que.empty()){
TrieNode* p = que.front();
que.pop();
for(int i = ;i < SEG_SIZE;i++){
TrieNode* pNode = p->next[i];
if(pNode == NULL) continue;
que.push(pNode);
TrieNode* pFail = p->fail;
//直到匹配,或者已经指向了根节点
while(pFail != trie.root && pFail->next[i] == NULL) pFail = pFail->fail;
pNode->fail = pFail->next[i];
if(pNode->fail == NULL) pNode->fail = trie.root;
//后缀链接的链接,指向失配后的结点或失配后的结点的后缀链接
pNode->last = (pNode->fail->value != )?(pNode->fail):(pNode->fail->last);
}
}
}
//当匹配完,或者是在一条链的中途仍然可能存在有匹配的结点
void rfind(TrieNode *p){
if(p != NULL){
count += p->value;
rfind(p->last);
p->value = ;
}
}
void find(string s){
TrieNode *pNode = trie.root;
for(int i = ;i < s.length();i++){
int c = Trie::cti(s[i]);
while(pNode != trie.root && pNode->next[c] == NULL) pNode = pNode->fail;
pNode = pNode->next[c];
if(pNode == NULL) pNode = trie.root;
if(pNode->value != ) rfind(pNode); //判断有没有可能匹配完其它的模板
else if(pNode->last != NULL) rfind(pNode->last);
}
}
}AC;
AC *ac;
int cases;
string buf;
int n;
int main(){
ios::sync_with_stdio(false); //取消同步,加快cin读取字符串的速度
cin>>cases;
while(cases--){
ac = new AC();
cin>>n;
for(int i = ;i < n;i++){
cin>>buf;
ac->trie.insert(buf);
}
cin>>buf;
ac->getFail();
ac->find(buf);
cout<<ac->count<<endl;
delete ac;
}
return ;
}

[后记]
  附上自己调程序时所用的数据生成器,如果用随机数生成可能基本上刷个1000,2000组很多问题都不能找出来,
因为直接用随机数要刷出点神数据的概率还是比较低的
 #include<iostream>
#include<fstream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<sstream>
using namespace std;
ofstream fout("ks.in");
//测试数据最少组数
#define CASE_LOW 3
//测试数据最大生成组数(CASE_LOW + CASE_LIMIT - 1)
#define CASE_LIMIT 10
//关键字最小生成组数
#define KEYWORD_LOW 3
#define KEYWORD_LIMIT 10
#define KEYWORD_MAX_LEN 5
//生成文章的次数
#define STR_TIMES 10
#define SUBSTR_LEN 5
string buf[KEYWORD_LIMIT + KEYWORD_LOW];
string str;
int _count;
string operator +(string str, char c){
stringstream ss;
ss<<str<<c;
return ss.str();
}
int main(){ srand((unsigned)time(NULL)); int cases = rand()%CASE_LIMIT + CASE_LOW;
fout<<cases<<endl; for(int i = ;i < cases;i++){ _count = rand()%KEYWORD_LIMIT + KEYWORD_LOW;
fout<<_count<<endl;
for(int j = ; j <= _count;j++){
int len = rand()%KEYWORD_MAX_LEN + ;
buf[j] = "";
for(int k = ; k <= len;k++){
buf[j] = buf[j] + (char)(rand()% + 'a');
}
fout<<buf[j]<<endl;
} int times = rand()%STR_TIMES + ;
str = "";
for(int j = ;j <= times;j++){ int v = rand()%;
if(v == ){
str += buf[rand()%_count + ];
}else{
int len = rand()%SUBSTR_LEN + ;
for(int i = ;i <= len;i++){
str = str + (char)(rand()% + 'a');
}
} } fout<<str<<endl; } fout.close();
return ;
}

md_ks.cpp

hdu 2222 Keywords Search - Aho-Corasick自动机的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模版题)

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  3. HDU 2222 Keywords Search 【AC自动机】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=2222] 题意:给出很多小字符串,然后给出一个文本串,问文本串中包含多少个小字符串.也就是说如果文本串 ...

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

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

  5. HDU 2222 Keywords Search(AC自动机入门)

    题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...

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

    题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...

  7. HDU 2222 Keywords Search 【AC自动机模板】

    询问有多少个模式串出现在了文本串里面.将模式串插入Trie树中,然后跑一边AC自动机统计一下就哦了. 献上一份模板. #include <cstdio> #include <cstr ...

  8. hdu 2222 Keywords Search(AC自动机)

    /* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...

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

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

  10. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

随机推荐

  1. Oracle管理监控之linux下wendba登录设置

    [root@localhost ~]# su - oracle [oracle@localhost ~]$ which jar/usr/bin/jar [oracle@localhost ~]$ ex ...

  2. ElasticSearch报 EsThreadPoolExecutor[search, queue capacity = 1000, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@c0efba

    ElasticSearch报以下错误的解决办法: "type": "es_rejected_execution_exception", "reason ...

  3. Jmeter(五)_函数

    JMeter提供了很多函数,如果能够熟练使用,可以为脚本带来很多方便. JMeter函数是一种特殊值,可用于除测试计划外的任何组件. 函数调用的格式如下所示:${__functionName(var1 ...

  4. AMD 和 CMD 的区别有哪些

    在说AMD 和 CMD 的区别之前,先说明commonjs,它的回调和amd.cmd的不同于:commomjs加载完了所有模块,才执行回调amd和cmd是加载对应的模块,就可以执行回调中对应的代码 1 ...

  5. linux,centOS,用LNMP搭建wordpress,更新固定连接--全流程

    下午到晚上的时间,买了个linux服务器,用的centOS系统,遇到各种问题! 1.用putty,ssh到vps后,根据网上命令,一步步下载并安装,具体步骤可以看一下网上教程,LNMP.org站上的教 ...

  6. mysql 内置功能 存储过程 删除存储过程

    删除存储过程 drop procedure proc_name;

  7. backreference Oracle正則表達式中的反向引用

      这是Oracle对正則表達式的backreference的描写叙述 从定义中能够看到,当匹配表达式中已()的形式将一个子串包括起来.后面就能够以\? 的形式来引用.\1相应第一个(),\2相应第二 ...

  8. HTML5-Canvas 绘制线条的深入认识

    1. lineWidth 线条宽度 ( 示例: lineWidth = 10 ) 2. lineCap 线条帽(线条两端的形状) ( 示例: lineCap = butt(default) | rou ...

  9. Spark会把数据都载入到内存么?

    前言 很多初学者其实对Spark的编程模式还是RDD这个概念理解不到位,就会产生一些误解. 比如,很多时候我们常常以为一个文件是会被完整读入到内存,然后做各种变换,这很可能是受两个概念的误导: RDD ...

  10. INT_MAX和INT_MIN注意事项

    版权声明:转载请注明出处 http://blog.csdn.net/TwT520Ly https://blog.csdn.net/TwT520Ly/article/details/53038345 I ...