. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 const int MAXN = 35000 ; . 4 . 5 struct Aho_Corasock_Automaton { . 6 struct Node { . 7 int Fail; // mismatch pointer . 8 int Son [ 2 ]; // Sub location of the nodes . 9 BOOL danger; // risk marker 10 } Trie [MAXN]; . 11 int CNT = 0 ; // Trie pointer 12 is voidINSERT ( char * S) { 13 is int len = strlen (S); 14 int now = 0 ; // Trie current pointer 15 for ( int I = 0 ; I <len; ++ I) { 16 IF (Trie [now ] .son [S [I] - ' 0 ' ] == 0 ) { // if no child node . 17 Trie [now] .son [S [I] - ' 0 ' ] = CNT ++; // configuration the node 18 } 19 = Trie now [now] .son [S [I] - ' 0 ' ]; 20 is } 21 is Trie [now] = .danger to true ; 22 is } 23 is void get_fail () { // configured fail pointer 24 Queue < int > que ; 25 for ( int I = 0 ; I < 2 ; I ++) { // first layer before the second configuration 26 is IF (Trie [ 0 !] .son [I] = 0 ) { // node if there is 27 Trie [Trie [ 0 .] .Son [I]] Fail = 0 ; // Fail root node points 28 que.push (Trie [ 0 ] .son [I]); // queued 29 } 30 } 31 is the while ( ! que.empty ()) { // BFS seeking fail pointer 32 int U = que.front (); que.pop (); 33 is for ( int I = 0 ; I < 2 ; ++ I) { 34 is IF ( Trie [Trie [U] .fail] == .danger to true ) 35 Trie [U] = .danger to true ; 36 IF (! Trie [U] .son [I] = 0 ) { // If the child node exists 37 [ // fail child node pointer pointing to the current node pointer to the same content fail child node 38 is Trie [Trie [U] .son [I]] = Fail. Trie [Trie [U] .fail] .son [I]; 39 que.push (Trie [U] .son [I]); // enqueued 40 } 41 is the else { // if there is no child node 42 // the sub-node of the current node to the node fail Gezi stop pointer pointing to the current node 43 is Trie [U] .son [I] = Trie [Trie [U] .fail] .son [I]; 44 is } 45 } 46 } 47 } 48 }AC; 49 bool vis[maxn]; 50 bool dfs(int u) { 51 if (vis[u] == true) return true; 52 vis[u] = true; 53 if (!AC.Trie[AC.Trie[u].son[0]].danger) { 54 if (dfs(AC.Trie[u].son[0])) { 55 return true; 56 } 57 } 58 if (!AC.Trie[AC.Trie[u].son[1]].danger) { 59 if (dfs(AC.Trie[u].son[1])) { 60 return true; 61 } 62 } 63 vis[u] = false; 64 return false; 65 } 66 char t[maxn]; 67 int main() { 68 int n; scanf("%d",&n); 69 for (int i = . 1 ; I <= n-; ++ I) { 70 Scanf ( " % S " , T); 71 is AC.insert (T); 72 } 73 is AC.get_fail (); 74 IF (DFS ( 0 ) == to true ) the puts ( " TAK " ); // start looking from the root node of the trie ring 75 the else the puts ( " NIE " ); 76 return 0 ; 77 }