• HDU 2222 AC自动机模板题


    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222

    AC自动机模板题

    我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接:

    http://blog.csdn.net/niushuai666/article/details/7002823

    http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<queue>
     4 using namespace std;
     5 char key[55];
     6 char des[1111111];
     7 struct node{
     8     node *fail;
     9     node *next[26];
    10     int cnt;
    11     node(){
    12         fail = NULL;
    13         cnt = 0;
    14         for(int i = 0;i<26;i++)
    15             next[i] = NULL;
    16     }
    17 };
    18 node *root;
    19 void insert(char *str){
    20     node *head = root;
    21     int len = strlen(str);
    22     for(int i = 0;i<len;i++){
    23         int temp = str[i]-'a';
    24         if(head->next[temp] == NULL)
    25             head->next[temp] = new node();
    26         head = head->next[temp];
    27     }
    28     head->cnt++;
    29 } 
    30 void build(){
    31     queue<node *>q;
    32     q.push(root);
    33     while(!q.empty()){
    34         node *head = q.front();
    35         q.pop();
    36         for(int i = 0;i<26;i++){
    37             if(head->next[i] != NULL){
    38                 if(head == root){
    39                     head->next[i]->fail = root;
    40                 }else{
    41                     node *temp = head->fail;
    42                     while(temp != NULL){
    43                         if(temp->next[i] != NULL){
    44                             head->next[i]->fail = temp->next[i];
    45                             break;
    46                         }
    47                         temp = temp->fail;
    48                     }
    49                     if(temp == NULL)
    50                         head->next[i]->fail = root;
    51                 }
    52                 q.push(head->next[i]);
    53             }
    54         }
    55     }
    56 }
    57 int query(){
    58     int len = strlen(des),ans = 0;;
    59     node *head = root;
    60     for(int i = 0;i<len;i++){
    61         int index = des[i]-'a';
    62         while(head->next[index] == NULL && head != root)
    63             head = head->fail;
    64         head = head->next[index];
    65         if(head == NULL)
    66             head = root;
    67         node *temp = head;
    68         while(temp!=root && temp->cnt!=-1){
    69             ans += temp->cnt;
    70             temp->cnt = -1;
    71             temp = temp->fail;
    72         }
    73     }
    74     return ans;
    75 }
    76 int main(){
    77     int t;
    78     scanf("%d",&t);
    79     while(t--){
    80         root = new node();
    81         int n;
    82         scanf("%d",&n);
    83         for(int i = 0;i<n;i++){
    84             scanf(" %s",key);
    85             insert(key);
    86         }
    87         build();
    88         scanf(" %s",des);
    89         printf("%d
    ",query());
    90     }    
    91     return 0;
    92 }
  • 相关阅读:
    javamail 利用qq邮箱做邮箱服务器,简单小demo
    apache tiles 页面模板的使用
    PHP中Cookie与Session的异同以及使用
    PHP二维数组--去除指定列含有重复项的数组
    PHP实现简单的双色球机选号码
    PHP常用的数学函数和字符串函数
    PHP日期函数
    PHP的操作符与控制结构
    PHP的变量作用域-常亮-全局变量-表单提交变量
    PHP四种输出语句
  • 原文地址:https://www.cnblogs.com/zqy123/p/5420884.html
Copyright © 2020-2023  润新知