DT is a big fan of digital products. He writes posts about technological products almost everyday in his blog.

But there is such few comments of his posts that he feels depressed all the day. As his best friend and an excellent programmer, DT asked you to help make his blog look more popular. He is so warm that you have no idea how to refuse. But you are unwilling to read all of his boring posts word by word. So you decided to write a script to comment below his posts automatically.

After observation, you found words “Apple” appear everywhere in his posts. After your counting, you concluded that “Apple”, “iPhone”, “iPod”, “iPad” are the most high-frequency words in his blog. Once one of these words were read by your smart script, it will make a comment “MAI MAI MAI!”, and go on reading the post.

In order to make it more funny, you, as a fan of Sony, also want to make some comments about Sony. So you want to add a new rule to the script: make a comment “SONY DAFA IS GOOD!” when “Sony” appears.

题意:给出一个文本,遇到“Apple”, “iPhone”, “iPod”, “iPad” 输出“MAI MAI MAI!”,遇到“Sony”输出“SONY DAFA IS GOOD!”

直接上了AC自动机匹配掉了整个文本。

不知道直接暴力匹配什么的可不可以过。

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxm=; char s[];
int nxt[maxm][],tail[maxm],f[maxm],size;
int last[maxm]; int newnode(){
memset(nxt[size],,sizeof(nxt[size]));
f[size]=tail[size]=;
return size++;
} void insert(char s[],int k){
int i,p=;
for(i=;s[i];i++){
int &x=nxt[p][s[i]];
p=x?x:x=newnode();
}
tail[p]=k;
} void makenxt(){
int i;
queue<int>q;
f[]=;
for(i=;i<;i++){
int v=nxt[][i];
if(v){
f[v]=last[v]=;
q.push(v);
}
}
while(!q.empty()){
int u=q.front();
q.pop();
for(i=;i<;i++){
int v=nxt[u][i];
if(!v)nxt[u][i]=nxt[f[u]][i];
else q.push(v);
f[v]=nxt[f[u]][i];
last[v]=tail[f[v]]?f[v]:last[f[v]];
}
}
} void query(char s[]){
int v=;
for(int i=;s[i];i++){
while(v&&!nxt[v][s[i]])v=f[v];
v=nxt[v][s[i]];
int tmp=v;
while(tmp){
if(tail[tmp]==)printf("MAI MAI MAI!\n");
else if(tail[tmp]==)printf("SONY DAFA IS GOOD!\n");
tmp=last[tmp];
}
}
} int main(){
size=,newnode();
char word[][]={"Apple","iPhone","iPod","iPad","Sony"};
insert(word[],);
insert(word[],);
insert(word[],);
insert(word[],);
insert(word[],);
makenxt();
while(scanf("%s",s)!=EOF){
query(s);
}
return ;
}

hdu5007 Post Robot AC自动机的更多相关文章

  1. AC自动机-算法详解

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

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

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

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

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

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

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

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

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

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

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

  7. [AC自动机]【学习笔记】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...

  8. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  9. AC自动机 HDU 2896

    n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...

随机推荐

  1. Windows Server 2008 R2 下载地址

    以下资源均来自微软 MSDN,是原汁原味的原版系统资源,值得系统爱好者收藏.以下多数链接是 ed2k 链接,推荐使用国外开源的 eMule 下载,亦可使用迅雷,但使用 eMule 更有利于共享资源. ...

  2. 关于MVC RouteExistingFiles疑问

    如图,使用ajpg模拟静态资源.使用 routes.RouteExistingFiles = true; 使静态资源也Map Route,匹配不到则显示Not Foud. ①/a.jpg:访问不到 ② ...

  3. Entity FrameWork 增删改查

         Entity Framework (又称ADO.NET Entity Framework) 是微软以 ADO.NET 为基础所发展出来的对象关系对应(O/R Mapping) 解决方案   ...

  4. Confluence 6 示例 - https://confluence.atlassian.com/

    这里是有关存储空间和内存使用的情况,数据更新于 2013年04月: 数据库大小 2827 MB Home 目录占用空间大小 116 GB 平均内存消耗 1.9 GB 选择实例的数据库表格 数据(Dat ...

  5. Confluence 6 服务器硬件要求指南

    服务器管理员可以通过本页面的指南来对在运行 Confluence 评估版本的最小服务器硬件需求进行评估.应为实际的服务器负载是很难进行预测的,所以最好的办法是通过实际运行一个 Confluence 实 ...

  6. 数据分析库之Numpy

    Numpy get started NumPy中我们要学习的核心其实就是一个 ndarray n多 d dimension 维度 array数组 多维数组 创建一个ndarray的几种方法 impor ...

  7. C# 自定义配置文件

    配置文件有两种设置方式,第一种是直接在网站根目录下的web.config中设置:第二种方式是自定义配置文件,在web.config中指定其他配置文件的路径. 第一种:除了在常用的appSettings ...

  8. Flutter(一)安装配置和几个注意点

    Flutter(一)安装配置和几个注意点 记住要仰望星空,不要低头看脚下.无论生活如何艰难,请保持一颗好奇心.你总会找到自己的路和属于你的成功. 愿您的来世灵魂依旧,躯体康健. 一句话来形容Flutt ...

  9. 记一次前端css样式的三角形的应用

    1)面试题是这样的要求用css实现 <section> <div></div> <div></div> </section> & ...

  10. spoj mgame

    题解: f[i][j]表示先后手最大差 g[i][j]表示在最大差的时候是否有后手没得走 代码: #include<bits/stdc++.h> using namespace std; ...