Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6. using namespace std;
  7. char s[6000];
  8. long long ps[6000];
  9. long long h[6000];
  10. long long hcur[6000];
  11. int size = 0;
  12. int n;
  13. int main()
  14. {
  15.     freopen("input.txt", "r", stdin);
  16.     freopen("output.txt", "w", stdout);
  17.     scanf("%s", s);
  18.     n = strlen(s);
  19.     const int p = 7;
  20.     ps[0] = 1;
  21.     for (int i = 1; i < n; ++i)
  22.         ps[i] = ps[i - 1] * p;
  23.     for (int i=0; i < n; ++i)
  24.     {
  25.         h[i] = (s[i]) * ps[i];
  26.         if (i)  
  27.             h[i] += h[i-1];
  28.     }
  29.     int result = 0;
  30.     for (int l = 1; l < n; ++l)
  31.     {
  32.         memset(hcur, 0, sizeof hcur);
  33.         size = 0;
  34.         for (int i = 0; i < n - l + 1; ++i)
  35.         {
  36.             long long cur_h = h[i + l - 1];
  37.             if (i)  
  38.                 cur_h -= h[i - 1];
  39.             cur_h *= ps[n - i - 1];
  40.             hcur[size++ ] = cur_h;
  41.         }
  42.         int current = 1;
  43.         sort(hcur, hcur + size);
  44.         for (int i = 1; i < size; ++i)
  45.         {
  46.             if (hcur[i] == hcur[i - 1])
  47.                 current++;
  48.             else
  49.             {
  50.                 if (current > 1)
  51.                 {
  52.                     result++;
  53.                 }
  54.                 current = 1;
  55.             }
  56.         }
  57.         if (current > 1)
  58.             result++;
  59.        
  60.     }
  61.     printf("%d", result);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement