hdoj 2222
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的更多相关文章
- AC自动机 HDOJ 2222 Keywords Search
题目链接 题意:每个文本串的出现次数 分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零. 新模板,加上last跑快一倍 #include <bits/stdc++.h> ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- HDOJ 2222: Keywords Search
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机入门
Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...
- 【HDOJ】2222 Keywords Search
AC自动机基础题. #include <iostream> #include <cstdio> #include <cstring> #include <cs ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
随机推荐
- jquery 使用ajax调用c#后台方法
$.ajax({ type: "get", cache: false, ...
- poj 1459 (最大流)
最大流简单题,,这题重要的是知道了scanf("%s",str);sscanf(str,"(%d,%d)%d",&x,&y,&w);读入 ...
- Head First SQL笔记
看的时候总结了一下,如下: Chapter 1: 创建数据库 CREATE DATABASE database_name; 使用数据库 USE database_name; 创建表 CRATE TAB ...
- BZOJAC400题留念
BZOJAC400题了...
- 51NOD 算法马拉松8
题目戳这里:51NOD算法马拉松8 某天晚上kpm在玩OSU!之余让我看一下B题...然后我就被坑进了51Nod... A.还是01串 水题..怎么乱写应该都可以.记个前缀和然后枚举就行了.时间复杂度 ...
- PHP-变量(www.w3school.com.cn/php)
写在前面: 变量可以形象的解释为信息的容器(存信息的东西).比如说$x=8,相当于把8给放到x里面,以后操作$x的时候就相当于操作8 >5+$x结果为13 ------------------- ...
- VS2008一个小bug
vc工程的项目属性里MFC的使用有三项:默认“使用标准windows库”,修改为“在静态库中使用MFC”,然后再改回默认,项目变化如下: 今天的工程在改过后编译不能通过,手工把0改回2(1改回3)后O ...
- Grunt.js 上手
Official Site gruntjs.org/docs/getting-started.html 或者看http://tgideas.qq.com/webplat/info/news_versi ...
- sql server 常见问题
自动生成的值 @@identity 存储过程参数应指定参数长度,且不大于数据库长度,自定义方法里如果有参数,也应指定长度,否则会报错,提取数据长度被断 connectionString 可以指定app ...
- MVC-03 控制器(2)
五.ActionResult解说 ActionResult的继承图: 类型 Controller辅助方法 用途帮助 ContentResult Content 回传一个用户自定义的文字属性 Empty ...