Advertisement
Guest User

Untitled

a guest
May 26th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. struct state {
  10. int len, lkk;
  11. map<char, int> next;
  12. };
  13.  
  14. const int mxln = 10000*21;
  15. state st[mxln * 2];
  16. int size, last;
  17. int cnt[2 * mxln];
  18. vector<int> pos[mxln];
  19.  
  20. void sa_init() {
  21. size = last = 0;
  22. st[0].len = 0;
  23. st[0].lkk = -1;
  24. ++size;
  25.  
  26. }
  27.  
  28. void sa_extend(char c) {
  29. int cc = size++;
  30. st[cc].len = st[last].len + 1;
  31. pos[st[cc].len].push_back(cc);
  32. cnt[cc] = 1;
  33. int k;
  34. for (k = last; k != -1 && !st[k].next.count(c); k = st[k].lkk)
  35. st[k].next[c] = cc;
  36. if (k == -1)
  37. st[cc].lkk = 0;
  38. else {
  39. int q = st[k].next[c];
  40. if (st[k].len + 1 == st[q].len)
  41. st[cc].lkk = q;
  42. else {
  43. int clone = size++;
  44. st[clone].len = st[k].len + 1;
  45. pos[st[clone].len].push_back(clone);
  46. st[clone].next = st[q].next;
  47. st[clone].lkk = st[q].lkk;
  48. for (; k != -1 && st[k].next[c] == q; k = st[k].lkk)
  49. st[k].next[c] = clone;
  50. st[q].lkk = st[cc].lkk = clone;
  51. }
  52. }
  53. last = cc;
  54. }
  55.  
  56.  
  57.  
  58. int count_substr(string& s)
  59. {
  60. int st_index = 0;
  61. int j = 0;
  62.  
  63. while (j < s.length())
  64. {
  65. char c = s[j];
  66. st_index = st[st_index].next[c];
  67. j++;
  68. }
  69.  
  70. return cnt[st_index];
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76. int n, n1;
  77. cin >> n1;
  78.  
  79. vector<string> ss;
  80. string s,text;
  81. for (int i = 0; i < n1; i++)
  82. {
  83. cin >> s;
  84. ss.push_back(s);
  85. if (text.length() != 0)
  86. text += "#";
  87. text += s;
  88. }
  89.  
  90. memset(cnt, 0, sizeof(cnt));
  91. s = text;
  92. sa_init();
  93. for (size_t i = 0; i < s.length(); i++)
  94. sa_extend(s[i]);
  95. n = text.length();
  96. for (int i = n; i > 0; --i)
  97. for (int j = 0; j < (int)pos[i].size(); ++j)
  98. {
  99. int x = pos[i][j];
  100. if (st[x].lkk >= 0) cnt[st[x].lkk] += cnt[x];
  101. }
  102.  
  103.  
  104. for (int i = 0; i < n1; i++)
  105. cout << (count_substr(ss[i]) - 1) << endl;
  106.  
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement