zoukankan      html  css  js  c++  java
  • hdu5007 Post Robot AC自动机

    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自动机匹配掉了整个文本。

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

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 const int maxm=5000;
     6 
     7 char s[1000005];
     8 int nxt[maxm][128],tail[maxm],f[maxm],size;
     9 int last[maxm];
    10 
    11 int newnode(){
    12     memset(nxt[size],0,sizeof(nxt[size]));
    13     f[size]=tail[size]=0;
    14     return size++;
    15 }
    16 
    17 void insert(char s[],int k){
    18     int i,p=0;
    19     for(i=0;s[i];i++){
    20         int &x=nxt[p][s[i]];
    21         p=x?x:x=newnode();
    22     }
    23     tail[p]=k;
    24 }
    25 
    26 void makenxt(){
    27     int i;
    28     queue<int>q;
    29     f[0]=0;
    30     for(i=0;i<128;i++){
    31         int v=nxt[0][i];
    32         if(v){
    33             f[v]=last[v]=0;
    34             q.push(v);
    35         }
    36     }
    37     while(!q.empty()){
    38         int u=q.front();
    39         q.pop();
    40         for(i=0;i<128;i++){
    41             int v=nxt[u][i];
    42             if(!v)nxt[u][i]=nxt[f[u]][i];
    43             else q.push(v);
    44             f[v]=nxt[f[u]][i];
    45             last[v]=tail[f[v]]?f[v]:last[f[v]];
    46         }
    47     }
    48 }
    49 
    50 void query(char s[]){
    51     int v=0;
    52     for(int i=0;s[i];i++){
    53         while(v&&!nxt[v][s[i]])v=f[v];
    54         v=nxt[v][s[i]];
    55         int tmp=v;
    56         while(tmp){
    57             if(tail[tmp]==1)printf("MAI MAI MAI!
    ");
    58             else if(tail[tmp]==2)printf("SONY DAFA IS GOOD!
    ");
    59             tmp=last[tmp];
    60         }
    61     }
    62 }
    63 
    64 int main(){
    65     size=0,newnode();
    66     char word[5][10]={"Apple","iPhone","iPod","iPad","Sony"};
    67     insert(word[0],1);
    68     insert(word[1],1);
    69     insert(word[2],1);
    70     insert(word[3],1);
    71     insert(word[4],2);
    72     makenxt();
    73     while(scanf("%s",s)!=EOF){
    74         query(s);
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    博客园样式
    nginx
    Django-environ 加载 .env 配置文件
    uwsgi 常见命令
    ActionScript3.0(AS3)中的泛型数组Vector
    ADDED、ADDED_TO_STAGE、REMOVED、REMOVED_FROM_STAGE这几个事件的区别
    Sprite/MovieClip的Enter_Frame事件,不受addChild/removeChild影响
    “AS3.0高级动画编程”学习:第二章转向行为(下)
    “AS3.0高级动画编程”学习:第二章转向行为(上)
    [转载]等角(斜45度)游戏与数学
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6843660.html
Copyright © 2011-2022 走看看