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\n",ss);
105         }
106         else printf("No\n");
107     }
108     return 0;
109 }
2168MS

 

转载于:https://www.cnblogs.com/GraceSkyer/p/7536618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值