链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384

题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和。

分析:之前并不知道AC自动机是用来求什么的,但翻模板的时候看见邝斌的字符串模板里有AC自动机一项,就看了一下,然后发现和题目要解决的问题一模一样。就开始改模板。结果没想到就是个裸的AC自动机,以为会TLE,10^10呢,迟迟不敢交,又被坑了。

目前对原理还一无所知。

 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
inline int Max(int a,int b)
{
return a>b?a:b;
}
inline int Min(int a,int b)
{
return a>b?b:a;
}
#define maxnode 600010
#define sigma_size 26
struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int haha[maxnode];
int f[maxnode];
int sz;
void init()
{
sz=;
memset(ch,,sizeof(ch));
memset(val, , sizeof(val));
memset(f,,sizeof(f));
memset(haha,,sizeof(haha));
}
int idx(char c)
{
return c-'a';
}
int insert(char *s)
{
int u = , len = strlen(s);
for(int i = ; i < len; i++)
{
int c = idx(s[i]);
if(!ch[u][c]) ch[u][c] = sz++;
u = ch[u][c];
}
val[u] ++;
return u;
}
void getFail()
{
queue<int> q;
for(int i = ; i<sigma_size; i++)
if(ch[][i]) q.push(ch[][i]); while(!q.empty())
{
int r = q.front();
q.pop();
for(int c = ; c<sigma_size; c++)
{
int u = ch[r][c];
if(!u)continue;
q.push(u);
int v = f[r];
while(v && ch[v][c] == ) v = f[v]; ///沿失配边走上去 如果失配后有节点 且 其子节点c存在则结束循环
f[u] = ch[v][c];
}
}
}
void find(char *T)
{
int len = strlen(T), j = ;
for(int i = ; i < len; i++)
{
int c = idx(T[i]);
while(j && ch[j][c]==) j = f[j];
j = ch[j][c];
int temp = j;
while(temp)
{
haha[temp]++;
temp = f[temp];
}
}
}
};
Trie ac;
char P[][];
int ans[];
char S1[];
int main()
{
int t,m,n;
scanf("%d",&t);
while(t--)
{
ac.init();
scanf("%d%d",&m,&n);
for(int i = ; i <= m; i++)
{
scanf("%s",P[i]);
}
for(int i = ; i <= n; i++)
{
scanf("%s",S1);
ans[i] = ac.insert(S1);
}
ac.getFail();
for(int i = ; i <= m; i++)
{
memset(ac.haha,,sizeof(ac.haha));
ac.find(P[i]);
int sum = ;
for(int i=; i <= n; i++)
sum += ac.haha[ans[i]];
printf("%d\n",sum);
}
}
return ;
}

AC自动机--改自久野模板

模板:http://blog.csdn.net/qq574857122/article/details/12355091

HDU 5384 AC自动机的更多相关文章

  1. hdu 2896 AC自动机

    // hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...

  2. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  3. hdu 5880 AC自动机

    Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu 3065 AC自动机(各子串出现的次数)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  8. HDU 2846 (AC自动机+多文本匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 题目大意:有多个文本,多个模式串.问每个模式串中,有多少个文本?(匹配可重复) 解题思路: 传统 ...

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. Best Time to Buy and Sell Stock III [LeetCode]

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. D​e​p​l​o​y​m​e​n​t​ ​f​a​i​l​u​r​e​ ​o​n​ ​T​o​m​c​a​t​ ​6​.​x​.​ ​C​o​u​l​d​ ​n​o​t​ ​c​o​p​y​ ​a​l​l​ ​r​e​s​o​u​r​c​e​s​ ​t​o

    在myeclipse总部署项目,一直有问题,提示如下的错误,经过研究在网上需求帮助,解决方案如下: Deployment failure on Tomcat  6.x. Could not copy  ...

  3. gulp教程之gulp-concat

    简介: 使用gulp-concat合并javascript文件,减少网络请求. 1.安装nodejs/全局安装gulp/本地安装gulp/创建package.json和gulpfile.js文件 1. ...

  4. Linux文件的建立、复制、删除和移动命令

    mkdir命令一.mkdir 命令使用权限 所有用户都可以在终端使用 mkdir 命令在拥有权限的文件夹创建文件夹或目录. 二.mkdir 命令使用格式 格式: mkdir [选项] DirName ...

  5. ElasticSearch配置说明

    配置文件位于%ES_HOME%/config/elasticsearch.yml文件中. cluster.name: elasticsearch 配置集群名称,默认elasticsearch node ...

  6. RMAN 备份及策略

    更详细信息请转http://blog.chinaunix.net/uid-21187846-id-3017363.html 天,这里修改为15天:这样比较保险,到时候恢复时也可以找到当时的备份元数据: ...

  7. 如何获取域名的ip地址

  8. javascript 中的借鸡生蛋

    如题所说,其实指的是 js 中的内置函数和 apply 的结合使用.这种用法很讨巧,读过 jQuery 源码的小伙伴都知道,它在里面被应用的非常广泛和精妙! 梨子:找出一个数组中的最大值和最小值 普遍 ...

  9. [转]ZendFramework数据库操作总结

    Zend_Db数据库知识 例子: Model文件: $this->fetchAll("is_jian=1","id DESC",0,2)->toAr ...

  10. Altium Designer自动更新——解决方法

    今天,打开AD,一直显示更新,关机重启也不管事. 然后,我把AD安装目录下,system文件夹下的Installation文件夹删了.就不再更新了.