Advertisement
yeskendir_sultanov

Все различные подстроки

Mar 3rd, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #define ull unsigned long long
  3.  
  4. using namespace std;
  5.  
  6. const int N = 1e5 + 3;
  7. const ull base = 31;
  8.  
  9. ull h[N], b[N];
  10.  
  11. ull get(int i, int j) {
  12.     return (h[j] - ((i > 0) ? h[i - 1] * b[j - i + 1] : 0));
  13. }
  14.  
  15. int main() {
  16.     string s;
  17.     cin >> s;
  18.     int n = s.size();
  19.     b[0] = 1;
  20.     for (int i = 1; i <= n; ++i) {
  21.         b[i] = b[i - 1] * base;
  22.     }
  23.     h[0] = s[0];
  24.     for (int i = 1; i < n; ++i) {
  25.         h[i] = (h[i - 1] * base + s[i]);
  26.     }
  27.    
  28.     set<ull> d;
  29.    
  30.     for (int len = 1; len <= n; ++len) {
  31.         for (int i = 0, j = len - 1; j < n; ++i, ++j) {
  32.             d.insert(get(i, j));
  33.         }
  34.     }
  35.    
  36.     cout << d.size();
  37.     return 0;
  38. }
  39.  
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement