Advertisement
Guest User

Untitled

a guest
Mar 21st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <string>
  9. #include <climits>
  10. #include <vector>
  11. #include <deque>
  12. #include <set>
  13. #include <map>
  14. #include <list>
  15. #include <stack>
  16. #include <cctype>
  17. #include <bitset>
  18. #include <ctime>
  19. #include <cassert>
  20. #include <fstream>
  21. #include <complex>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. #define MIN(x,y) (((x)<(y))?(x):(y))
  26. #define MAX(x,y) (((x)>(y))?(x):(y))
  27. #define ABS(x) (((x)<0)?(-(x)):(x))
  28. #define ff first
  29. #define ss second
  30. #define ei else if
  31. #define mp make_pair
  32. #define pb push_back
  33. typedef long long ll;
  34. typedef unsigned long long ull;
  35. typedef long double ld;
  36. typedef pair<int,int> pii;
  37. typedef pair<int,pair<int,int> > piii;
  38. const ld EPS = 1e-7;
  39. const ld PI  = 3.141592653589793;
  40.  
  41. const int MAX_N = 1234, MAX_LEN = 2005, ALP = 260;
  42. int N;
  43. string STR;
  44. bitset<MAX_N> res;
  45.  
  46. struct Node {
  47.   bitset<MAX_N> out;
  48.   map<char, int> go;
  49.   int fail;
  50.   int pred;
  51.   char symb;
  52. } t[5000005];
  53. int sz = 0;
  54.  
  55. inline void trie_add(const string& s, int idx) {
  56.   int v = 0; char c;
  57.   for(int i = 0; i < s.length(); ++i) {
  58.     c = s[i];
  59.     if(t[v].go.find(c) == t[v].go.end()) {
  60.       t[v].go.insert(mp(c, ++sz));
  61.       t[sz].symb = c;
  62.       t[sz].pred = v;
  63.     }
  64.     v = t[v].go[c];
  65.   }
  66.   t[v].out[idx] = 1;
  67. }
  68.  
  69. /*
  70.  
  71. ushers
  72. 4
  73. he
  74. hers
  75. his
  76. she
  77.  
  78. */
  79.  
  80. inline void failure_bfs() {
  81.   queue<int> Q;
  82.   Q.push(0);
  83.   int v, state;
  84.   char c;
  85.   while(!Q.empty()) {
  86.     v = Q.front(); Q.pop();
  87.     for(map<char, int>::iterator it = t[v].go.begin(); it != t[v].go.end(); ++it)
  88.       Q.push(it->second);
  89.  
  90.     if(v == 0 or t[v].pred == 0) {
  91.       t[v].fail = 0;
  92.       continue;
  93.     }
  94.  
  95.     c = t[v].symb;
  96.     state = t[t[v].pred].fail;
  97.     while(state != 0 and t[state].go.find(c) == t[state].go.end())
  98.       state = t[state].fail;
  99.     if(t[state].go.find(c) != t[state].go.end())
  100.       state = t[state].go[c];
  101.     t[v].fail = state;
  102.     t[v].out |= t[state].out;
  103.   }
  104. }
  105.  
  106. inline void next_func() {
  107.   int go_state; char c;
  108.   for(int state = 0; state <= sz; ++state) {
  109.     go_state = t[state].fail;
  110.     for(map<char, int>::iterator it = t[go_state].go.begin(); it != t[go_state].go.end(); ++it) {
  111.       c = it->first;
  112.       if(t[state].go.find(c) == t[state].go.end())
  113.     t[state].go[c] = t[go_state].go[c];
  114.     }
  115.   }
  116. }
  117.  
  118. inline void build_aho_cor() {
  119.   failure_bfs();
  120.   next_func();
  121. }
  122.  
  123. /*
  124. void print_out() {
  125.   for(int i = 0; i <= sz; ++i) {
  126.     cout << i << '\t';
  127.     for(set<int>::iterator it = t[i].out.begin(); it != t[i].out.end(); ++it)
  128.       cout << *it << ' ';
  129.     cout << '\n';
  130.   }
  131. }
  132. */
  133.  
  134. inline void recognize() {
  135.   int v = 0; char c;
  136.   for(int i = 0; i < STR.length(); ++i) {
  137.     c = STR[i];
  138.     if(t[v].go.find(c) != t[v].go.end())
  139.       v = t[v].go[c];
  140.     else
  141.       v = 0;
  142.  
  143.     res |= t[v].out;
  144.   }
  145. }
  146.  
  147. inline void print_res() {
  148.   for(int i = 0; i < N; ++i) {
  149.     if(res[i] == 1)
  150.       putchar('Y');
  151.     else
  152.       putchar('N');
  153.     putchar('\n');
  154.   }
  155. }
  156.  
  157. bool solve() {
  158.   while(cin >> STR) {
  159.     cin >> N;
  160.     string s;
  161.     for(int i = 0; i < N; ++i) {
  162.       cin >> s;
  163.       trie_add(s, i);
  164.     }
  165.     build_aho_cor();
  166. //    print_out();
  167.     recognize();
  168.     print_res();
  169.  
  170.     return true;
  171.   }
  172.  
  173.   return false;
  174. }
  175.  
  176. int main() {
  177.   ios_base::sync_with_stdio(0);
  178.   while(solve());
  179.   return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement