HDU 3065 AC自动机 裸题
中文题题意不再赘述
注意 失配数组 f 初始化一步到位
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; #define N 2000100
#define maxnode 50010
#define sigma_size 26
struct node{
char name[55];
int num;
}S[1101];
struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int f[maxnode];
int sz;
void init(){
sz=1;
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
memset(f,0,sizeof(f));
val[0] = 0;
}
int idx(char c){
return c-'A';
} void Creat(char *s, int num){
int u = 0, len = strlen(s);
for(int i = 0; i < len; i++){
int c = idx(s[i]);
if(!ch[u][c]) ch[u][c] = sz++;
u = ch[u][c];
}
val[u] = num ; //u若是单词结尾则为 +1
} int find(char *T){
int len = strlen(T);
int j = 0;
for(int i = 0; i < len; i++){
if(T[i]<'A' || T[i]>'Z'){ j = 0; continue;}
int c = idx(T[i]);
j = ch[j][c];
int temp = j;
while(temp && val[temp]){
S[ val[temp] ].num ++;
temp = f[ temp ];
}
} return 0;
} void getFail(){
queue<int> q;
//初始化队列
for(int c = 0; c < sigma_size; c++)
if(ch[0][c])q.push(ch[0][c]); while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c < sigma_size; c++){
int u = ch[r][c];
if(!u){ ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v]; //沿失配边追溯到可以匹配的(非原点)位置
f[u] = ch[v][c];
}
}
}
};
Trie ac;
char S1[N]; int main(){
int n;
while(~scanf("%d",&n)){
ac.init();
for(int i = 1; i <= n; i++){
scanf("%s",S[i].name);
S[i].num = 0;
ac.Creat(S[i].name, i);
}
ac.getFail(); scanf("%s",S1);
ac.find(S1); for(int i = 1; i <= n; i++)
if(S[i].num)
printf("%s: %d\n",S[i].name, S[i].num); }
return 0;
}
HDU 3065 AC自动机 裸题的更多相关文章
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- hdu 3065 AC自动机模版题
题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...
- hdu 3065 AC自动机
// hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...
- hdu 3065 AC自动机(各子串出现的次数)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- php函数声明的简单实例
<?phpecho table(10,5,500);echo table(2,2,400); //函数调用 function table($row,$col,$width){ //通过函数标记t ...
- tabBar选中底部弹出窗口
//UITabBarControllerDelegate方法 - (BOOL)tabBarController:(UITabBarController *)tabBarController shoul ...
- Python Tutorial学习(十一)-- Brief Tour of the Standard Library – Part II
11.1. Output Formatting 格式化输出 The repr module provides a version of repr() customized for abbreviate ...
- linux c++ 遍历一个目录下的文件名 (包括子目录的文件名)
最近写代码有一个要遍历目录下的每一个文件并取得这个文件的绝对路径的需求, 我们知道linux c++中有system命令所以我在代码中 先生成了一个log,然后去读log文件的每一行文件名,然后给存储 ...
- 既然HTTP1.1协议里每个连接默认都是持久连接,那么为何当今所有报文都在使用Connetion:Keep-Alive
说白了,如果你发起时有,那么服务器支持,回应时也会有,不支持,也就没有了.所以一般客户端都会默认带着发,服务端返回不返回就是服务端的事了. 1. 支不支持长连接,关键在于服务端是否支持. 如果服务端不 ...
- 原生js实现吸顶导航和回到顶部特效
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java or spring +jython +python (Error:python.home,Determine if the following attributes are correct:)
最近更新 :16年3月10日更 首先你在用JAVA,你需要运行Python,于是你找了Jython,我不介绍什么是Jythyon了 如何在Java中调用Python的方法,一百度一大堆, 如下:是一种 ...
- 当用DJANGO的migrate不成功时。。。。
URL:http://my.oschina.net/u/862582/blog/355421 因为操作SQL数据库时不规范,或是多人开发时产生了同步问题,就可能导致正规的MIGRATE时不能完成. 已 ...
- spring 动态数据源
1.动态数据源: 在一个项目中,有时候需要用到多个数据库,比如读写分离,数据库的分布式存储等等,这时我们要在项目中配置多个数据库. 2.原理: (1).spring 单数据源获取数据连接过程: ...
- [LeetCode#256] Paint House
Problem: There are a row of n houses, each house can be painted with one of the three colors: red, b ...