Advertisement
Malinovsky239

Untitled

Jan 23rd, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. typedef long long LL;
  12.  
  13. #define P LL(1e9 + 7)
  14. #define N 55
  15. #define L int(2e5 + 5)
  16.  
  17. struct node {
  18.     int to[26], end;
  19. };
  20.  
  21. int n, m, cnt_node;
  22. LL p[N], hash[N];
  23. bool sufpref[N];
  24. char t[N];
  25. node trie[L];
  26.  
  27. void precalc() {
  28.     p[0] = 1;
  29.     for (int i = 1; i < N; i++)
  30.         p[i] = p[i - 1] * P;   
  31. }
  32.  
  33. LL count_hash(int l, int r) {
  34.     if (!l)
  35.         return hash[r];
  36.     return hash[r] - hash[l - 1] * p[r - l + 1];
  37. }
  38.  
  39. int main() {
  40.     srand(time(NULL));
  41.     freopen("sufpref.in", "r", stdin);
  42.     freopen("sufpref.out", "w", stdout);
  43.  
  44.     precalc();
  45.  
  46.     scanf("%d\n", &n);
  47.  
  48.     for (int i = 0; i < n; i++) {
  49.         gets(t);
  50.         int l = strlen(t);
  51.  
  52.         sufpref[0] = false;
  53.         hash[0] = t[0];
  54.         for (int j = 1; j < l; j++)
  55.             hash[j] = hash[j - 1] * P + t[j], sufpref[j] = false;          
  56.  
  57.         for (int j = 1; j <= l; j++) {
  58.             if (count_hash(0, j - 1) == count_hash(l - j, l - 1)) {
  59.                 bool eq = true;
  60.                 for (int k = 0; k < 3; k++) {
  61.                     int pos = rand() % j;
  62.                     if (t[pos] != t[l - j + pos]) {
  63.                         eq = false;
  64.                         break;
  65.                     }
  66.                 }
  67.                 if (eq)  
  68.                     sufpref[j - 1] = true;
  69.             }
  70.         }
  71.  
  72.         int now = 0;
  73.         for (int j = 0; j < l; j++) {
  74.             int sym = int(t[j] - 'a');
  75.             if (!trie[now].to[sym]) {
  76.                 cnt_node++;
  77.                 trie[now].to[sym] = cnt_node;          
  78.             }          
  79.             now = trie[now].to[sym];
  80.             if (sufpref[j])
  81.                 trie[now].end++;
  82.         }
  83.     }
  84.  
  85.     scanf("%d\n", &m);
  86.  
  87.     for (int i = 0; i < m; i++) {
  88.         gets(t);
  89.         int l = strlen(t);
  90.         bool fail = false; 
  91.         int now = 0;
  92.  
  93.         for (int j = 0; j < l; j++) {
  94.             int sym = int(t[j] - 'a');
  95.             now = trie[now].to[sym];
  96.             if (!now) {
  97.                 fail = true;
  98.                 break;
  99.             }
  100.         }
  101.  
  102.         if (!fail)
  103.             printf("%d\n", trie[now].end);
  104.     }
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement