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 ;
}

最新文章

  1. 使用Bandwagon的VPS第一件事《FQ》
  2. 捉襟见肘之NSMutableSet和NSPointerArray
  3. ssm(spring,springmvc,mybatis)
  4. gitignore for vs
  5. 可折叠的ToolBar+抽屉菜单NavigationView+浮动按钮FloatButton
  6. PHP 设计模式 笔记与总结(5)PHP 魔术方法的使用
  7. cf--------(div1)1A. Theatre Square
  8. NSNotificationCenter 使用姿势详解
  9. Eclipse安装SVN插件的方法( 手动安装)
  10. WPF ListView的使用及Linq to XML练习
  11. tmux tutorial
  12. HA高可用集群
  13. Scala 操作符与提取器
  14. Centos7安装官方JDK
  15. docker 将正在运行的容器打包为镜像
  16. springmvc 孔浩 hibernate code
  17. 使用maven构建scala项目
  18. Asp.net Vnext TagHelpers
  19. 前端~HTML~CSS~JavaScript~JQuery~Vue
  20. PHP中preg_match正则匹配的/u /i /s是什么意思

热门文章

  1. 智能合约 helloworld
  2. MySQL5.5安装教程
  3. js时间戳转化成日期格式
  4. MVC模式和MVP模式的区别
  5. MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中
  6. Java入门自学笔记
  7. 简单函数template max
  8. coursera-斯坦福-机器学习-吴恩达-笔记week1
  9. learning makefile grammar
  10. 负载均衡----实现配置篇(Nginx)