Advertisement
biznesman

Untitled

Aug 3rd, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MAXN = 1e5 + 10;
  6. const int INF = 1e9 + 7;
  7. const double EPS = 1e-10;
  8. const double PI = acosl(-1);
  9. const int P = 41;
  10. const long long MOD = 1e9 + 23;
  11.  
  12. long long p[10000], h[10000], ans;
  13. string s;
  14.  
  15. int main() {
  16.     //ifstream cin("input.txt");
  17.     //ofstream cout("output.txt");
  18.     ios_base::sync_with_stdio(false);
  19.     cin.tie(0);
  20.     cout.tie(0);
  21.     cin >> s;
  22.     int n = s.size();
  23.     p[0] = 1;
  24.     for (int i = 1; i <= n; i++) {
  25.         p[i] = (1ll * p[i - 1] * P) % MOD;
  26.     }
  27.     h[0] = (s[0] - 'a' + 1);
  28.     for (int i = 1; i < n; i++) {
  29.         h[i] = (h[i - 1] + (s[i] - 'a' + 1) * p[i]) % MOD;
  30.     }
  31.     vector<long long> check;
  32.     for (int i = 0; i < n; i++) {
  33.         check.clear();
  34.         for (int j = 0; j < n - i; j++) {
  35.             long long ch;
  36.             if (j) {
  37.                 ch = (h[j + i] - h[j - 1] + MOD) % MOD;
  38.             } else {
  39.                 ch = (h[j + i] + MOD) % MOD;
  40.             }
  41.             ch = (ch * p[n - j - 1]) % MOD;
  42.             check.push_back(ch);
  43.             //if (j)
  44.             //  cout << s.substr(j, i + 1) << ' ' << ch << ' ' << h[i + j] << ' ' << h[j - 1] << ' ' << p[n - j - 1] << ' ' << i << ' ' << j << '\n';
  45.             //else
  46.             //    cout << s.substr(j, i + 1) << ' ' << ch << ' ' << h[i + j] << ' ' << 0 << ' ' << p[n - j - 1] << ' ' << i << ' ' << j << '\n';
  47.         }
  48.         sort(check.begin(), check.end());
  49.         check.erase(unique(check.begin(), check.end()), check.end());
  50.         ans += check.size();
  51.     }
  52.     cout << ans;
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement