Advertisement
Guest User

Untitled

a guest
Mar 20th, 2014
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 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 depth;
  51.   int pred;
  52.   char symb;
  53.   int next[ALP];
  54.  
  55.   Node() {
  56.     memset(next, 0, sizeof(next));
  57.   }
  58. } t[1100005];
  59. int sz = 0;
  60.  
  61. inline void trie_add(const string& s, int idx) {
  62.   int v = 0; char c;
  63.   for(int i = 0; i < s.length(); ++i) {
  64.     c = s[i];
  65.     if(t[v].go.find(c) == t[v].go.end()) {
  66.       t[v].go.insert(mp(c, ++sz));
  67.       t[sz].symb = c;
  68.       t[sz].depth = t[v].depth + 1;
  69.       t[sz].pred = v;
  70.     }
  71.     v = t[v].go[c];
  72.   }
  73.   t[v].out[idx] = 1;
  74. }
  75.  
  76. /*
  77.  
  78. ushers
  79. 4
  80. he
  81. hers
  82. his
  83. she
  84.  
  85. */
  86.  
  87. inline void failure_bfs() {
  88.   queue<int> Q;
  89.   Q.push(0);
  90.   int v, state;
  91.   char c;
  92.   while(!Q.empty()) {
  93.     v = Q.front(); Q.pop();
  94.     for(map<char, int>::iterator it = t[v].go.begin(); it != t[v].go.end(); ++it)
  95.       Q.push(it->second);
  96.  
  97.     if(t[v].depth <= 1) {
  98.       t[v].fail = 0;
  99.       continue;
  100.     }
  101.  
  102.     c = t[v].symb;
  103.     state = t[t[v].pred].fail;
  104.     while(state != 0 and t[state].go.find(c) == t[state].go.end())
  105.       state = t[state].fail;
  106.     if(t[state].go.find(c) != t[state].go.end())
  107.       state = t[state].go[c];
  108.     t[v].fail = state;
  109.     t[v].out |= t[state].out;
  110.   }
  111. }
  112.  
  113. inline void next_func() {
  114.   int go_state; char c;
  115.   for(int state = 0; state <= sz; ++state) {
  116.     go_state = t[state].fail;
  117.     for(map<char, int>::iterator it = t[go_state].go.begin(); it != t[go_state].go.end(); ++it) {
  118.       c = it->first;
  119.       if(t[state].go.find(c) == t[state].go.end())
  120.     t[state].next[c] = t[go_state].go[c];
  121.     }
  122.   }
  123. }
  124.  
  125. inline void build_aho_cor() {
  126.   failure_bfs();
  127.   next_func();
  128. }
  129.  
  130. /*
  131. void print_out() {
  132.   for(int i = 0; i <= sz; ++i) {
  133.     cout << i << '\t';
  134.     for(set<int>::iterator it = t[i].out.begin(); it != t[i].out.end(); ++it)
  135.       cout << *it << ' ';
  136.     cout << '\n';
  137.   }
  138. }
  139. */
  140.  
  141. inline void recognize() {
  142.   int v = 0; char c;
  143.   for(int i = 0; i < STR.length(); ++i) {
  144.     c = STR[i];
  145.     if(t[v].go.find(c) != t[v].go.end())
  146.       v = t[v].go[c];
  147.     else
  148.       v = t[v].next[c];
  149.  
  150.     res |= t[v].out;
  151.   }
  152. }
  153.  
  154. inline void print_res() {
  155.   for(int i = 0; i < N; ++i) {
  156.     if(res[i] == 1)
  157.       putchar('Y');
  158.     else
  159.       putchar('N');
  160.     putchar('\n');
  161.   }
  162. }
  163.  
  164. bool solve() {
  165.   while(cin >> STR) {
  166.     cin >> N;
  167.     string s;
  168.     for(int i = 0; i < N; ++i) {
  169.       cin >> s;
  170.       trie_add(s, i);
  171.     }
  172.     build_aho_cor();
  173. //    print_out();
  174.     recognize();
  175.     print_res();
  176.  
  177.     return true;
  178.   }
  179.  
  180.   return false;
  181. }
  182.  
  183. int main() {
  184.   ios_base::sync_with_stdio(0);
  185.   while(solve());
  186.   return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement