• 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
  • 相关阅读:
    SPSS—回归—曲线估计方程案例解析
    SPSS—非线性回归(模型表达式)案例解析
    SPSS—回归—二元Logistic回归案例分析
    SPSS-两变量相关性分析
    SPSS-单因素方差分析(ANOVA) 案例解析
    SPSS-比较均值-独立样本T检验 案例解析
    做MFC的时候引用了头文件但VC还是报missing storage-class or type specifiers
    GetDocument missing storage-class or type specifiers的解决方法
    Python如何安装模块
    AE IRasterCursor 改变栅格图层像素值
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6843660.html
Copyright © 2020-2023  润新知