题目大意:统计模式串出现的次数。

题目分析:模板题。

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std; const int N=1000; int cnt;
int ch[N*50+5][95];
int f[N*50+5];
int val[N*50+5];
char p[N][55];
int num[N+5];
char str[N*N*10+5]; void init()
{
cnt=0;
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
memset(num,0,sizeof(num));
} int idx(char c)
{
return (int)(c-32);
} void insert(char *s,int id)
{
int r=0;
int m=strlen(s);
for(int i=0;i<m;++i){
int c=idx(s[i]);
if(!ch[r][c]) ch[r][c]=++cnt;
r=ch[r][c];
}
val[r]=id;
} void getFail()
{
queue<int>q;
f[0]=0;
for(int i=0;i<95;++i){
int u=ch[0][i];
if(u){
f[u]=0;
q.push(u);
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int i=0;i<95;++i){
if(ch[r][i]){
f[ch[r][i]]=ch[f[r]][i];
q.push(ch[r][i]);
}else ch[r][i]=ch[f[r]][i];
}
}
} void ac(char *str)
{
int j=0;
int len=strlen(str);
for(int i=0;i<len;++i){
j=ch[j][idx(str[i])];
int u=j;
while(u){
if(val[u]) ++num[val[u]];
u=f[u];
}
}
} int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=0;i<n;++i){
cin>>p[i];
insert(p[i],i+1);
}
getFail();
scanf("%s",str);
ac(str);
for(int i=1;i<=n;++i) if(num[i]>0)
printf("%s: %d\n",p[i-1],num[i]);
}
return 0;
}

  

  

HDU-2778 DNA Sequence(AC自动机)的更多相关文章

  1. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

  2. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  3. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  4. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

  5. POJ 2778 DNA Sequence (AC自动机,矩阵乘法)

    题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...

  6. POJ 2778 DNA Sequence (AC自动机+DP+矩阵)

    题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个. 析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散 ...

  7. POJ2278 DNA Sequence —— AC自动机 + 矩阵优化

    题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  8. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  9. [poj2778]DNA Sequence(AC自动机+矩阵快速幂)

    题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...

  10. poj2778 DNA Sequence(AC自动机+矩阵快速幂)

    Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...

随机推荐

  1. linux应用程序开发-文件编程-库函数

    ---恢复内容开始--- c库函数文件操作独立于具体的系统平台,可移植性较好. 库函数-创建和打开 FILE*fopen(const char*filename,const char*mode) mo ...

  2. (八)open函数的flag详解

    3.1.4.open函数的flag详解13.1.4.1.读写权限:O_RDONLY O_WRONLY O_RDWR(1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明(譬 ...

  3. 插件框架(Plugin Framework)

    HOW TO MAKE PLUGIN FRAMEWORK 插件系统构建 Dissect Eclipse Plugin Framework

  4. CSS 3 3D转换

    绘制3D环境 父元素设置了 preserve-3d  子元素就可以以父元素作为平面进行3d转换 transform-style: preserve-3d; 设置视点 :表示透视效果  值越小 透视效果 ...

  5. 转json using指令

    using Newtonsoft.Json;using Newtonsoft.Json.Converters; string result = JsonConvert.SerializeObject( ...

  6. jquery的colorbox关闭并传递数据到父窗

    function closebox(para1, para2) { var k = parent;// 父窗口对象 k.document.getElementById("para1" ...

  7. Copy List with Random Pointer [LeetCode]

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. html5的spellcheck属性(拼写、文法检查)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. 初学UML——用例图

    开始学习UML建模语言,从用例图入手.建模工具选择visio 用例图描述的是参与者所理解的系统功能,主要元素是用例和参与者,是帮助开发团队以一种可视化的方式理解系统的功能需求.这时处于项目初始,分析用 ...

  10. WCF服务在类库中的引用

    在类库中引用了WCF服务,悲剧降临了,追踪日志看到下边一串: --------------------------------------------------------------------- ...