Advertisement
Guest User

Opencup K

a guest
Mar 17th, 2013
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iostream>
  5. #include <cstring>
  6. #include <unordered_set>
  7.  
  8. using namespace std;
  9.  
  10. #define sz(x) ((int)(x.size()))
  11. #define pb push_back
  12.  
  13. typedef long long ll;
  14.  
  15. const int N = 100000;
  16. const int M = 1000000;
  17.  
  18. const ll P1 = 31;
  19. const ll P2 = 29;
  20.  
  21. const ll MOD1 =  999999997;
  22. const ll MOD2 = 1000000007;
  23.  
  24. ll p_pow1[M + 1];
  25. ll p_pow2[M + 1];
  26.  
  27. ll h1[M + 1];
  28. ll h2[M + 1];
  29.  
  30. char s[M + 1];
  31.  
  32. unordered_set<long long> used[N + 1];
  33.  
  34. inline int lcp(ll h1[], ll h2[], int n) {
  35.   int l = 0, r = n + 1;
  36.   while (r - l > 1) {
  37.     int m = (l + r) / 2;
  38.     ll cur_h1 = (h1[m] - h1[0] * p_pow1[m]) % MOD1;
  39.     ll cur_h2 = (h2[m] - h2[0] * p_pow2[m]) % MOD2;
  40.     if (cur_h1 < 0) cur_h1 += MOD1;
  41.     if (cur_h2 < 0) cur_h2 += MOD2;    
  42.     if (used[m].find(cur_h1 ^ (cur_h2 << 32)) != used[m].end()) {
  43.       l = m;
  44.     } else {
  45.       r = m;
  46.     }
  47.   }
  48.   return l;
  49. }
  50.  
  51. int main() {
  52.   int n;
  53.   scanf("%d\n", &n);
  54.   for (int i = 0; i < n; ++i) {
  55.     gets(s);
  56.     int len = strlen(s);
  57.     ll cur_h1 = 0;
  58.     ll cur_h2 = 0;
  59.     for (int j = 0; j < len; ++j) {
  60.       cur_h1 *= P1;
  61.       cur_h2 *= P2;
  62.       cur_h1 += s[j] - 'a' + 1;
  63.       cur_h2 += s[j] - 'a' + 1;
  64.       cur_h1 %= MOD1;
  65.       cur_h2 %= MOD2;
  66.       used[j + 1].insert(cur_h1 ^ (cur_h2 << 32));
  67.     }
  68.   }
  69.  
  70.   p_pow1[0] = 1;
  71.   p_pow2[0] = 1;
  72.   for (int i = 0; i < M; ++i) {
  73.     p_pow1[i + 1] = p_pow1[i] * P1 % MOD1;
  74.     p_pow2[i + 1] = p_pow2[i] * P2 % MOD2;
  75.  
  76.   }
  77.   int m;
  78.   scanf("%d\n", &m);
  79.   for (int i = 0; i < m; ++i) {
  80.     gets(s);
  81.     int len = strlen(s);
  82.     h1[0] = 0;    
  83.     h2[0] = 0;
  84.     for (int j = 0; j < len; ++j) {
  85.       h1[j + 1] = (h1[j] * P1 + s[j] - 'a' + 1) % MOD1;
  86.       h2[j + 1] = (h2[j] * P2 + s[j] - 'a' + 1) % MOD2;
  87.     }
  88.     for (int j = 0; j < len; ++j) {
  89.       printf("%d ", lcp(h1 + j, h2 + j, min(N, len - j)));      
  90.     }
  91.     printf("\n");
  92.   }
  93.  
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement