Description

对,这就是裸的AC自动机。 
要求:在规定时间内统计出模版字符串在文本中出现的次数。

Input

第一行:模版字符串的个数N。 
第2->N+1行:N个字符串。(每个模版字符串的长度<=50) 
第N+2行:一行很长的字符串。长的很。(使用AC自动机能在1s内计算出)

Output

共N行,每行输出一个模版及出现的次数。(之间有一个空格,按照输入顺序输出)

Sample Input


hers 
her 
his 
she 
shershisher

Sample Output

hers 1 
her 2 
his 1 
she 2

Hint

所有字母均为小写 
所给模版不会重复

模版题 贴代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; struct Lin
{
int next[];
int cnt,id;
}a[];
char s[];
int fail[];
int ans[];
char o[][];
int num=; void work(int x)
{
scanf("%s",o[x]);
int ls=strlen(o[x]);int u;
int p=;
for(int i=;i<ls;i++)
{
u=o[x][i]-'a';
if(a[p].next[u])p=a[p].next[u];
else
{
a[p].next[u]=++num;
p=num;
}
}
a[p].cnt++;
a[p].id=x;
} void getfail()
{
queue<int>q;
q.push();
int u,v,p;
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=;i<;i++)
{
v=a[u].next[i];
if(!v)continue;
p=fail[u];
while(p)
{
if(a[p].next[i])
{
break;
}
p=fail[p];
}
if(a[p].next[i] && a[p].next[i]!=v)fail[v]=a[p].next[i];
q.push(v);
}
}
}
void getanswer()
{
scanf("%s",s);
int p=;int ls=strlen(s);int u;int go;
for(int i=;i<ls;i++)
{
u=s[i]-'a';
while(!a[p].next[u] && p)p=fail[p];
p=a[p].next[u];
go=p;
while(go)
{
ans[a[go].id]+=a[go].cnt;
go=fail[go];
}
}
return ;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
work(i);
getfail();
getanswer();
for(int i=;i<=n;i++)
printf("%s %d\n",o[i],ans[i]);
return ;
}
 

【USACO】AC自动机的更多相关文章

  1. BZOJ3075[USACO 2013 Mar Gold 3.Necklace]——AC自动机+DP

    题目描述 给你一个长度为n的字符串A,再给你一个长度为m的字符串B,求至少在A中删去多少个字符才能使得B不是A的子串.注:该题只读入A和B,不读入长度,先读入A,再读入B.数据保证A和B中只含小写字母 ...

  2. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  3. AC自动机题单

    AC自动机题目 真的超级感谢xzy 真的帮到我很多 题单 [X] [luogu3808][模板]AC自动机(简单版) https://www.luogu.org/problemnew/show/P38 ...

  4. 基于trie树做一个ac自动机

    基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...

  5. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

  6. python爬虫学习(11) —— 也写个AC自动机

    0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...

  7. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  8. BZOJ 3172: [Tjoi2013]单词 [AC自动机 Fail树]

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3198  Solved: 1532[Submit][Status ...

  9. BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]

    1212: [HNOI2004]L语言 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1367  Solved: 598[Submit][Status ...

随机推荐

  1. PTA题目的處理(四)

    题目7-3 求交错序列前N项和 1.实验代码 #include <stdio.h> //#include <stdlib.h> int main() { ,N; double ...

  2. 顺企网 爬取16W数据保存到Mongodb

    import requests from bs4 import BeautifulSoup import pymongo from multiprocessing.dummy import Pool ...

  3. bzoj 2962 序列操作

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 有一个长度为n的序列, ...

  4. Mybatis学习日志

    在Mybatis深入学习的一周中,总感觉跟着师傅的视屏讲解什么都能懂,但实际自己操作的时候才发现自己一脸懵逼,不知道从何入手.但还好自己做了点笔记.在此记录一下自己浅度学习Mybatis遇到几个小问题 ...

  5. ASP.NET MVC中错误处理方式

    /// <summary> /// 标记了HandleError,并指明错误处理页为AboutError.aspx /// </summary> /// <returns ...

  6. 电子称DIY(贴应变片+写代码)

    第一步.应变片介绍   ---------------------------------------------------------------------------------------- ...

  7. JMeter入门(03)多台JMeter联合测试

    一.配置各个节点 1.配置jmeter.properties # Remote Hosts - comma delimited#remote_hosts=localhost:1099,localhos ...

  8. hadoop2.6.0实践:000 虚拟机配置

  9. 新概念英语(1-17)How do you do ?

    Is there a problem wtih the Customers officer? What are Michael Baker and Jeremy Short's jobs? A:Com ...

  10. python—-模块与包1

    模块与包 1 什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀 2 为何要使用模块? 如果你对出python解释器然后重新进入,那么你之前定义的函数 ...