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 ...
随机推荐
- 侯捷C++ Type traits(类型萃取
泛型編程編出來的代碼,適用於任何「吻合某種條件限制」的資料型別.這已成為撰寫可復用代碼時的一個重要選擇.然而,總有一些時候,泛型不夠好 — 有時候是因為不同的型別差距過大,難以產生一致的泛化實作版本. ...
- java 动态获取web应用的部署路径
public static String DEPLOY_PATH = null; static { String CurrentClassFilePath = Constant.class.getRe ...
- JAVA中正則表達式总结
昨天,我的朋友请教我正則表達式.我也好久没有写过正則表達式了,昨天刚好看了下如鹏网创始人杨中科老师关于正則表達式的解说.使我加深了正則表達式的印像.现我把他总结下: 很多语言,包含Perl.PHP.P ...
- 原生js下拉刷新
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- POJ 2115 模线性方程 ax=b(mod n)
/* (x*c+a)%(2^k)==b →(x*c)%(2^k)==b-a 满足定理: 推论1:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a,n) | b. 推论2:方程ax=b(m ...
- Mac系统下下删除加锁文件方法|使用终端命令强制清除废纸篓中的文件
链接地址1:http://jingyan.baidu.com/article/fdffd1f8e39403f3e98ca195.html 在Mac OS X下,无法删除的文件无外乎三种情况:1,文件( ...
- 图片剪切之Croppic插件
前几天做图片上传时需要进行图片的剪切和缩放,上网查找时找到了这个插件.样式很好看,功能也很OK.但是网上都是php进行后台处理图片的例子,然后只好慢慢琢磨C#的处理.插件地址是:http://www. ...
- FOREIGN KEY相关
在添加外键的时候可以在最后通过ON指定行为和三个参数,来表示操作主表数据之后外表的变化 比如若是删除主表之后的变化,就可以 ON DELETE + 三个参数 --删除department表中相关数据行 ...
- Mysql开启远程
改表法: x:\>mysql -u root -pvmware mysql> use mysql;mysql> update User set Host = ‘%’ where Us ...
- SumoLogic
SumoLogic>>>Loggly. https://diyunpeng.loggly.com/setup MonitorWare http://www.monitorware.c ...