http://acm.hdu.edu.cn/showproblem.php?pid=2222

第一道 AC自动机。。。。。trie树的建立 和 AC自动机的查询,,可作模版。。。

解题思路:AC的应用。。。直接模版。。

 #include <iostream>
#include<cstring>
using namespace std;
struct point {
struct point *final;
struct point *next[];
int count;
point(){
final =NULL;
count =;
memset(next,NULL,sizeof(next));
}
}*q[]; char str[];
char tt[];
int head, tail; void build_trie(char *str, point *root){
point *p = root;
int i=,index;
while(str[i]){
index = str[i]-'a';
if(p->next[index]==NULL) p->next[index] = new point();
p = p->next[index];
i++;
}
p->count++;
} void get_final(point *root){
int i;
root->final = NULL;
q[head++] = root;
while(head!=tail){
point *temp = q[tail++];
point *p = NULL;
for(i=;i<;i++){
if(temp->next[i]!=NULL){
if(temp==root) temp->next[i]->final=root;
else{
p = temp->final;
while(p!=NULL){
if(p->next[i]!=NULL){
temp->next[i]->final = p->next[i];
break;
}
p = p->final;
}
if(p==NULL) temp->next[i]->final=root;
}
q[head++] = temp->next[i];
}
}
}
} int requry(point *root){
int i =,cnt=,index;
//int len = strlen(t);
point *p =root;
while(tt[i]){
index = tt[i]-'a';
while(p->next[index]==NULL&&p!=root) p= p->final;
p = p->next[index];
p=(p==NULL)? root:p;
point *temp =p;
while(temp!=root&&temp->count!=-){
//cout<<"------------>"<<endl;
cnt += temp->count;
temp->count=-;
temp = temp->final;
}
i++;
} return cnt;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
head =tail =;
point *root = new point();
cin>>n;
while(n--){
cin>>str;
build_trie(str,root);
}
get_final(root);
cin>>tt; cout<<requry(root)<<endl;
}
return ;
}

hdoj 2222的更多相关文章

  1. AC自动机 HDOJ 2222 Keywords Search

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

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

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

  3. HDOJ 2222: Keywords Search

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

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

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

  5. AC自动机入门

    Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...

  6. 【HDOJ】2222 Keywords Search

    AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...

  7. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. 《火球——UML大战需求分析》(0.1)——开篇废话

    说明: <火球——UML大战需求分析>是我撰写的一本关于需求分析及UML方面的书,我将会在CSDN上为大家分享前面几章的内容,总字数在几万以上,图片有数十张.欢迎你按文章的序号顺序阅读,谢 ...

  2. UVA122-Trees on the level(链二叉树)

    Trees on the level Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Sub ...

  3. javascript绑定事件

    本质:不同的库或者工具中总是封装了不同的事件绑定形式,但是究其根源,还是IE事件模型和W3C事件模型不同的处理方式 1)W3C事件模型:支持事件捕捉和冒泡 addEventListener('type ...

  4. jsp中的两种跳转方式分别是?有什么区别?

    在JSP中跳转有两种方式 forward跳转:<jsp:forward page ="跳转页面地址"> response跳转:response.sendRedirect ...

  5. Hibernate学习之面试问题汇总

    1. Hibernate 的检索方式有哪些 ? ① 导航对象图检索 ② OID检索 ③ HQL检索 ④ QBC检索 ⑤ 本地SQL检索 2. 在 Hibernate 中 Java 对象的状态有哪些 ? ...

  6. HDU 3729 二分匹配 反向匹配

    题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定  点 ...

  7. Creating a Broker (创建代理)

    1,CMD中运行 2,apollo的目录结构. bin  执行相关的脚步. etc  保存实例的配置文件 data  存储消息的文件 log 日志 tmp 临时的文件 3,Broker Configu ...

  8. php生成数据字典,代码

    <?php /** * 生成mysql数据字典 */ header("Content-type:text/html;charset=utf-8"); // 配置数据库 $da ...

  9. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  10. python中3个帮助函数help、dir、type的使用

    1.help函数:查看模块.函数.变量的详细说明: 查看模块 help("modules") 查看包  help("json") 查看类 help(json.J ...