zoukankan      html  css  js  c++  java
  • [HIHO1036] Trie图(AC自动机)

    题目链接:http://hihocoder.com/problemset/problem/1036

    不知道为什么匹配到某点存在next[idx]的时候,只需要检查这一个sign就行,如果检查此点的fail的sign就会TLE。

    大概数据比较弱能让我水过吧。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef struct Node {
     5     Node* fail;
     6     Node* next[26];
     7     bool sign;
     8     Node() { fail = NULL; for(int i = 0; i < 26; i++) next[i] = NULL; }
     9 }Node;
    10 
    11 const int maxn = 1000100;
    12 Node* rt;
    13 int n;
    14 char tmp[maxn];
    15 
    16 void insert(Node* rt, char* str) {
    17     Node* tmp = rt;
    18     for(int i = 0; str[i]; i++) {
    19         int idx = str[i] - 'a';
    20         if(tmp->next[idx] == NULL) tmp->next[idx] = new Node();
    21         tmp = tmp->next[idx];
    22     }
    23     tmp->sign = 1;
    24 }
    25 
    26 void build(Node* rt) {
    27     Node* cur;
    28     Node* tmp;
    29     queue<Node*> q;
    30     q.push(rt);
    31     while(!q.empty()) {
    32         cur = q.front(); q.pop();
    33         for(int i = 0; i < 26; i++) {
    34             if(!cur->next[i]) continue;
    35             if(cur == rt) cur->next[i]->fail = rt;
    36             else {
    37                 tmp = cur->fail;
    38                 do {
    39                     if(tmp->next[i]) {
    40                         cur->next[i]->fail = tmp->next[i];
    41                         break;
    42                     }
    43                 } while(tmp = tmp->fail);
    44                 if(tmp == NULL) cur->next[i]->fail = rt;
    45             }
    46             q.push(cur->next[i]);
    47         }
    48     }
    49 }
    50 
    51 bool query(Node* rt, char* str) {
    52     Node* cur = rt;
    53     for(int i = 0; str[i]; i++) {
    54         int idx = str[i] - 'a';
    55         while(cur->next[idx] == NULL && cur != rt) cur = cur->fail;
    56         cur = cur->next[idx];
    57         if(cur == NULL) {
    58             cur = rt;
    59             continue;
    60         }
    61         if(cur->sign) return 1;
    62     }
    63     return 0;
    64 }
    65 
    66 int main() {
    67     // freopen("in", "r", stdin);
    68     while(~scanf("%d", &n)) {
    69         rt = new Node();
    70         for(int i = 0; i < n; i++) {
    71             scanf("%s", tmp);
    72             insert(rt, tmp);
    73         }
    74         build(rt);
    75         scanf("%s", tmp);
    76         if(query(rt, tmp)) puts("YES");
    77         else puts("NO");
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    IconRes提供免费高质量的Material风格android官方图标库
    android中的所谓观察者模式
    Android总结篇——Intent机制详解及示例总结
    SpringMVC注解@initbinder解决类型转换问题
    ubuntu16.04上安装tomcat7
    ImportError: No module named corsheaders
    linux 网卡
    工控机安装Ubuntu14.04
    python2安装django
    Ubuntu14.04 terminal添加右键
  • 原文地址:https://www.cnblogs.com/kirai/p/6756852.html
Copyright © 2011-2022 走看看