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. ABAP search help (搜索帮助) 几种种方法

    ABAP search help (搜索帮助) 几种种方法    域范围  ABAP 的搜索帮助有很多种方法,掌握下面的几种基本差不多了 *&------------------------- ...

  2. Qt信号和槽机制

    概述 信号和槽机制是QT的核心机制,要精通QT编程就必须对信号和槽有所了解.信号和槽是一种高级接口,应用于对象之间的通信,他是QT的核心特性,也是QT差别于其他工具包的重要地方.信号和槽是QT自行定义 ...

  3. public,private,protected,以及default时的区别

    作用域    当前类   同一package     子孙类   其他package public       √                 √                    √    ...

  4. js实现往数组中添加非存在的对象,如果存在就改变键值。

    let arr = [] // 数组中元素数据类型为{name: 'bb', age: 12} // 现在需求是,将每次获得的新对象{name: '', age: }push到数组arr中,但前提是数 ...

  5. echarts的axisLabel的文字内容过长的解决办法

    通过查找资料学习,我总结了四种解决的办法,不一定是最好的,但是希望能够帮助到需要的童鞋,同时如果大家有什么更好的方法欢迎指导. 方法一:这种方法就是将文本内容转换为字符串数组,然后再按需求换行,需要每 ...

  6. adt安装----只为测试使用adb命令,故无需安装过于复杂

    下载并安装Android SDK  借鉴自原文 https://blog.csdn.net/qq_15304853/article/details/79168248 官网(可FQ选择):http:// ...

  7. SAM文件格式

    帮朋友处理sam各式文件,又记不住sam各式每列代表的什么内容,干脆转个帖子留着以后查询. 在SAM输出的结果中每一行都包括十二项通过Tab分隔,从左到右分别是: 1 序列的名字 2 概括出一个合适的 ...

  8. 雷林鹏分享:jQuery EasyUI 数据网格 - 列运算

    jQuery EasyUI 数据网格 - 列运算 在本教程中,您将学习如何在可编辑的数据网格(datagrid)中包含一个运算的列.一个运算列通常包含一些从一个或多个其他列运算的值. 首先,创建一个可 ...

  9. 【Oracle】【8】大批量update某个字段

    正文: 需实现:将A表的某个字段的值复制到B表中 我们一般会这样写:UPDATE B SET B.NAME = (SELECT A.NAME FROM A WHERE A.NO = B.NO) 出现的 ...

  10. spark获取时间

    在spark 中获取时间用到java.util.{Calendar,Date} 以及java.text.SimpleDateFormat来对时间输出格式作规范 首先先导入包 import java.t ...