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. 清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引。

    清理8组nodes中表的历史数据,平均每个node中的表有1.5亿条记录,需要根据date_created字段清理8000W数据记录,这个字段没有索引. 环境介绍  线上磁盘空间不足,truncate ...

  2. linux c in common use function reference manual

    End User License Agreement guarantees or warranties,大战前得磨刀!!!!! Tips:C Funcs Chk header Modules!

  3. How can I get an object's absolute position on the page in Javascript?

    How can I get an object's absolute position on the page in Javascript? How can I get an object's abs ...

  4. maven项目启动

    1服务install 2 build (tomcat:run)

  5. mvc 防止客服端多次提交

    但凡web开发中都会有户多次点击了提交按钮导致多次提交的情况,一般的集中做法 1.通过js在用户点击的时候将按钮disabled掉,但是这样并不是很可靠(我就可以跳过这个,用一个for循环 我直接自己 ...

  6. The Painter's Partition Problem Part II

    (http://leetcode.com/2011/04/the-painters-partition-problem-part-ii.html) This is Part II of the art ...

  7. #ifndef 与 #program once 的区别(转)

    转自http://hi.baidu.com/hrx20091001/item/ee70f7cc6d036d4ea9ba94e0 #ifndef 与 #program once 的区别 为了避免同一个文 ...

  8. 一次搞懂 Assets Pipeline 转载自http://gogojimmy.net/2012/07/03/understand-assets-pipline/

    Assets Pipeline 是 Rails 3.1 一個重要的功能,一直並沒有很去了解其特性,但因為最近都在寫前端的東西在 assets pipeline 的東西上跌跌撞撞了不少次(尤其在 dep ...

  9. Android 开发笔记 “Sqlite数据库删除”

    1.代码方式 Context.deleteDatabase(String databaseName);//删除某一数据库 2.设置里面 进入应用程序 ,然后清除数据就ok了

  10. 从PHP程序员到RAW开发~

    RAW是一款PHP网站开发系统,即使不懂PHP,也可以使用RAW进行PHP程序开发,当然如果已经掌握了PHP,那么使用RAW开发将会是如虎添翼! 怎么理解“如虎添翼”:我们平时要做一个项目的话,我们要 ...