ZOJ 3784 String of Infinity AC自动机

String of Infinity

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Given a set of banned words S, please find out whether it is possible to construct a string str1..∞ with infinite length that fulfills the following constrains:

  1. It consists of only the first M types of lowercase letters in the alphabet. For example M = 3, only 'a', 'b' and 'c' are allowed to appear in the string.
  2. There does not exist such (ij) that stri..j is a banned word in S (1 <= i <= j < ∞).
  3. There does not exist such (ij) that for any k >= istrk = str(j + k) (1 <= ij < ∞).

 

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 100) and M (1 <= M <= 26). The following N lines, each line contains contains a non-empty string indicating a banned word in S. The length of each word will not exceed 1000 and the word only consists of lowercase letters.

Output

For each test case, output "Yes" if it is possible to construct such a string, otherwise "No".

Sample Input

2
2 2
aa
bb
1 2
aa

Sample Output

No
Yes

  1 #include <queue>
  2 #include <stack>
  3 #include <cstdio>
  4 #include <cstring>
  5 using namespace std;
  6 const int CHAR = 26;
  7 const int TOT = 100005;
  8 int next[TOT][CHAR], fail[TOT];
  9 bool virus[TOT];
 10 int L, root;
 11 int m;
 12 int newNode() {
 13     for (int i=0; i<CHAR; i++)
 14         next[L][i]=-1;
 15     fail[L]=0;
 16     virus[L]=0;
 17     return L++;
 18 }
 19 void insert(char *s) {
 20     int now=root;
 21     for (int i=0; s[i]; i++) {
 22         int ch=s[i]-'a';
 23         if (next[now][ch]==-1)
 24             next[now][ch]=newNode();
 25         now=next[now][ch];
 26     }
 27     virus[now]=1;
 28 }
 29 void build() {
 30     queue<int> Q;
 31     for (int i=0; i<m; i++) {
 32         if (next[root][i]==-1)
 33             next[root][i]=root;
 34         else {
 35             fail[next[root][i]]=root;
 36             Q.push(next[root][i]);
 37         }
 38     }
 39     while (!Q.empty()) {
 40         int now=Q.front(); Q.pop();
 41         virus[now]|=virus[fail[now]];
 42         for (int i=0; i<m; i++) {
 43             if (next[now][i]==-1)
 44                 next[now][i]=next[fail[now]][i];
 45             else {
 46                 fail[next[now][i]]=next[fail[now]][i];
 47                 Q.push(next[now][i]);
 48             }
 49         }
 50     }
 51 }
 52 ///////////////////////////////////////////////////////////////////////////
 53 const int maxn = TOT;
 54 const int maxm = TOT*26;
 55 int head[maxn], e, Time, id[maxn], dfn[maxn], low[maxn], sz;
 56 bool vis[maxn];
 57 stack<int> s;
 58 struct node
 59 {
 60     int v, next;
 61 }edge[maxm];
 62 void add_edge(int u, int v)
 63 {
 64     edge[e].v=v;
 65     edge[e].next=head[u];
 66     head[u]=e++;
 67 }
 68 void init()
 69 {
 70     memset(head, -1, sizeof(head));
 71     memset(id, 0, sizeof(id));
 72     memset(vis, 0, sizeof(vis));
 73     memset(dfn, 0, sizeof(dfn));
 74     while (!s.empty()) s.pop();
 75     e=Time=sz=0;
 76     L=0;
 77     root=newNode();
 78 }
 79 int Min(int a, int b)
 80 {
 81     if (a<=b) return a;
 82     return b;
 83 }
 84 void tarjan(int u)
 85 {
 86     int v;
 87     dfn[u]=low[u]=++Time;
 88     s.push(u);
 89     vis[u]=1;
 90     for (int i=head[u]; i!=-1; i=edge[i].next) {
 91         v=edge[i].v;
 92         if (!dfn[v]) {
 93             tarjan(v);
 94             low[u]=Min(low[u], low[v]);
 95         }
 96         else if (vis[v]) low[u]=Min(low[u], dfn[v]);
 97     }
 98     if (dfn[u]==low[u]) {
 99         sz++;
100         do {
101             v=s.top();
102             s.pop();
103             id[v]=sz;
104             vis[v]=0;
105         }while(v!=u);
106     }
107 }
108 bool judge()
109 {
110     int cnt;
111     for (int i=0; i<L; i++) {
112         cnt=0;
113         for (int j=head[i]; j!=-1; j=edge[j].next) {
114             if (id[i]==id[edge[j].v]) cnt++;
115             if (cnt>=2) return 1;
116         }
117     }
118     return 0;
119 }
120 char str[1005];
121 int main()
122 {
123     int cas, n;
124     scanf("%d", &cas);
125     while(cas--) {
126         scanf("%d %d", &n, &m);
127         init();
128         while (n--) {
129             scanf(" %s", str);
130             insert(str);
131         }
132         build();
133         for (int i=0; i<L; i++) {
134             if (virus[i]) continue;
135             for (int j=0; j<m; j++) {
136                 if (virus[next[i][j]]) continue;
137                 add_edge(i, next[i][j]);
138             }
139         }
140         for (int i=0; i<L; i++) {
141             if (!dfn[i])
142                 tarjan(i);
143         }
144         if (judge()) printf("Yes\n");
145         else printf("No\n");
146     }
147     return 0;
148 }
View Code

 通过AC自动机,建立不含禁止串的图,看这个图中每个强联通分量是否只有简单圈。假如是,最后建立的无穷串中肯定有循环节,否则输出Yes。

posted @ 2014-04-18 11:20  BlueMandora  阅读(489)  评论(0编辑  收藏  举报