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

 

posted @ 2017-09-17 18:51  GraceSkyer  阅读(263)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……