• hdu 6208 The Dominator of Strings【AC自动机】


    hdu 6208 The Dominator of Strings【AC自动机】

    求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了。。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 using namespace std;
      7 inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
      8 const int N = 100005;
      9 const int M = 26;
     10 struct Trie {
     11     int next[N][M], fail[N], tag[N];
     12     int root;
     13     int vis[N];
     14     int L;
     15     int newnode() {
     16         for(int i = 0;i < M;i++)
     17             next[L][i] = -1;
     18         vis[L] = 0;
     19         tag[L++] = 0;
     20         
     21         return L-1;
     22     }
     23     void init() {
     24         L = 0;
     25         root = newnode();
     26     }
     27     void Insert(char buf[]) {
     28         int len = strlen(buf);
     29         int u = root;
     30         for(int i = 0; i < len; i++) {
     31             if(next[u][buf[i]-'a'] == -1)
     32                 next[u][buf[i]-'a'] = newnode();
     33             u = next[u][buf[i]-'a'];
     34         }
     35         tag[u]++;
     36     }
     37     void build() {
     38         queue<int> Q;
     39         fail[root] = root;
     40         for(int i = 0; i < M; i++) {
     41             if(next[root][i] == -1)
     42                 next[root][i] = root;
     43             else {
     44                 fail[next[root][i]] = root;
     45                 Q.push(next[root][i]);
     46             }
     47         }
     48         while( !Q.empty() ) {
     49             int u = Q.front();
     50             Q.pop();
     51             for(int i = 0; i < M; i++) {
     52                 int &v = next[u][i];
     53                 if(v == -1)
     54                     v = next[fail[u]][i];
     55                 else {
     56                     Q.push(v);
     57                     fail[v] = next[fail[u]][i];
     58                 }
     59             }
     60         }
     61     }
     62     int query(char buf[]) {
     63         int len = strlen(buf);
     64         int u = root;
     65         int res = 0;
     66         for(int i = 0; i < len; i++) {
     67             u = next[u][buf[i]-'a'];
     68             int t = u;
     69             while( t != root && !vis[t] ) {
     70                 res += tag[t];
     71                 
     72                 vis[t] = 1;//要不然会t啊
     73                 
     74                 tag[t] = 0;
     75                 t = fail[t];
     76                 
     77             }
     78         }
     79         return res;
     80     }
     81 };
     82 char s[N];
     83 char ss[N];
     84 Trie ac;
     85 int main() {
     86     int t, n;
     87     read(t);
     88     while(t--){
     89         int ma = 0;
     90         ac.init();
     91         read(n);
     92 
     93         for(int i=0; i<n; i++){
     94             scanf("%s",s);
     95             int len = strlen(s);
     96             ac.Insert(s);
     97             if(ma <= len){
     98                 ma = len; strcpy(ss, s);
     99             }
    100         }
    101         ac.build();
    102         int ans = ac.query(ss);
    103         if(ans == n){
    104             printf("%s
    ",ss);
    105         }
    106         else printf("No
    ");
    107     }
    108     return 0;
    109 }
    2168MS
  • 相关阅读:
    docker使用
    window版docker安装及配置
    mysql命令
    xshell
    git 命令
    分页器原理
    PCL-Kinfu编译手册
    cmake-add_definitions
    cmake-include_directories
    cmake-source_group
  • 原文地址:https://www.cnblogs.com/GraceSkyer/p/7536618.html
Copyright © 2020-2023  润新知