Advertisement
Mirandek

Untitled

Aug 7th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int mod = 10001;
  6. int maxlen = 100000;
  7. int x = 37;
  8. int hc[100000];
  9.  
  10. int exponent(int x, int n) {
  11.     if (n == 0)
  12.         return 1;
  13.     if (n == 1)
  14.         return x;
  15.     if (n == 2)
  16.         return (x*x) % mod;
  17.  
  18.     int res = exponent(x, n/2) % mod;
  19.     return (res * res * exponent(x, n&1)) % mod;
  20. }
  21.  
  22. void create_hash(char str[]) {
  23.     hc[0] = str[0];
  24.     for (int i = 1; str[i] != '\0'; i++)
  25.         hc[i] = (hc[i-1]*x + str[i]) % mod;
  26. }
  27.  
  28. int hash_get(int i) {
  29.     return hc[i];
  30. }
  31.  
  32. int main() {
  33.     char str[maxlen];
  34.     int loc1, loc2, len;
  35.     int hc1, hc2;
  36.  
  37.     scanf("%s", &str);
  38.     scanf("%d %d %d", &loc1, &loc2, &len);
  39.  
  40.     create_hash(str);
  41.  
  42.     int ex = exponent(x, len);
  43.     if (loc1 == 0)
  44.         hc1 = hash_get(len-1);
  45.     else
  46.         hc1 = (hash_get(loc1+len-1) - hash_get(loc1-1)*ex) % mod;
  47.  
  48.     if (loc2 == 0)
  49.         hc2 = hash_get(len-1);
  50.     else
  51.         hc2 = (hash_get(loc2+len-1) - hash_get(loc2-1)*ex) % mod;
  52.  
  53.     int same = 1;
  54.     if (hc1 == hc2) {
  55.         for (int i = 0; i < len; i++)
  56.             if (str[i+loc1] != str[i+loc2]) {
  57.                 same = 0;
  58.                 break;
  59.             }
  60.     } else
  61.         same = 0;
  62.     printf("%d\n", same);
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement