Advertisement
K_Y_M_bl_C

Untitled

Sep 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. #define FAST_ALLOCATOR_MEMORY 2e8
  3. #ifdef _DEBUG
  4. #include "opt.h"
  5. #endif
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11. typedef pair<int, int> pii;
  12. typedef vector<int> vi;
  13. typedef unsigned long long ull;
  14.  
  15. #define mk make_pair
  16. #define inb push_back
  17. #define X first
  18. #define Y second
  19. #define all(v) v.begin(), v.end()
  20. #define sqr(x) (x) * (x)
  21.  
  22. int solve();
  23.  
  24. int main()
  25. {
  26. //ios_base::sync_with_stdio(0);
  27. //cin.tie(0);
  28. #define TASK "carno"
  29. #ifndef _DEBUG
  30. //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  31. #endif
  32. solve();
  33. }
  34.  
  35. const int SZ = (1 << 16) - 1;
  36. const int P = 239;
  37.  
  38. struct HashTable
  39. {
  40. vector<ull> a;
  41. vector<short int> was;
  42. short int cursession;
  43. HashTable() { a.resize(SZ), was.resize(SZ), cursession = 0; };
  44. void put(ull x, int &ans)
  45. {
  46. int i = x & SZ;
  47. if (i == SZ)
  48. i = 0;
  49. while (was[i] == cursession && a[i] != x)
  50. {
  51. ++i;
  52. if (i == SZ)
  53. i = 0;
  54. }
  55. if (was[i] != cursession || a[i] != x)
  56. ++ans, a[i] = x, was[i] = cursession;
  57. }
  58. };
  59.  
  60. int solve()
  61. {
  62. int ans = 0;
  63. char *s = new char[20000];
  64. readWord(s);
  65. HashTable T;
  66. short int n = strlen(s);
  67. vector <ull> p(n + 1), h(n);
  68. p[0] = 1;
  69. for (short int i = 1; i <= n; ++i)
  70. p[i] = p[i - 1] * P;
  71. h[0] = s[0] - 'a' + 1;
  72. for (int i = 1; i < n; ++i)
  73. h[i] = h[i - 1] * P + s[i] - 'a' + 1;
  74. ull ch;
  75. for (short int len = 1; len <= n; ++len)
  76. {
  77. ch = h[len - 1];
  78. T.cursession++;
  79. T.put(ch, ans);
  80. for (int i = len; i < n; ++i)
  81. {
  82. ch = (ch - (s[i - len] - 'a' + 1) * p[len - 1]) * P + s[i] - 'a' + 1;
  83. T.put(ch, ans);
  84. }
  85. }
  86. writeInt(ans, '\n');
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement