Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define _USE_MATH_DEFINES
  3. #pragma GCC optimize("unroll-loops")
  4. #pragma GCC optimize("fast-math")
  5. #pragma optimize("O3")
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <fstream>
  10. #include <bitset>
  11. #include <iomanip>
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include <map>
  15. #include <unordered_map>
  16. #include <tuple>
  17. #include <cmath>
  18. #include <functional>
  19. #include <cassert>
  20. #include <algorithm>
  21. #include <set>
  22. #include <deque>
  23. #include <numeric>
  24. using namespace std;
  25. #define kekek ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  26.  
  27. const int maxL = 1e6 + 3;
  28. const int alpha = 'z' - 'a' + 1;
  29.  
  30. struct node {
  31. int suff, p, supersuff, move[alpha], go[alpha];
  32. char c;
  33. vector<int> term;
  34.  
  35. node(int par, int ch) {
  36. suff = supersuff = -1;
  37. c = ch;
  38. p = par;
  39. for (int i = 0; i < alpha; i++) {
  40. move[i] = -1;
  41. go[i] = -1;
  42. }
  43. }
  44.  
  45. node() {
  46. c = p = suff = supersuff = -1;
  47. for (int i = 0; i < alpha; i++) {
  48. move[i] = -1;
  49. go[i] = -1;
  50. }
  51. }
  52. };
  53.  
  54. int sz = 1;
  55. node trie[maxL];
  56. vector<vector<int>> ans;
  57.  
  58. void add(string& s, int num) {
  59. int v = 0, n = s.size();
  60. for (int i = 0; i < n; i++) {
  61. if (trie[v].move[s[i] - 'a'] == -1) {
  62. trie[sz].suff = trie[sz].supersuff = -1;
  63. for (int i = 0; i < alpha; i++) {
  64. trie[sz].move[i] = -1;
  65. trie[sz].go[i] = -1;
  66. }
  67. trie[v].move[s[i] - 'a'] = sz;
  68. trie[sz].p = v;
  69. trie[sz].c = s[i] - 'a';
  70. sz++;
  71. }
  72. v = trie[v].move[s[i] - 'a'];
  73. }
  74. trie[v].term.emplace_back(num);
  75. }
  76.  
  77. int go(int u, char c);
  78.  
  79. int getsuff(int u) {
  80. if (trie[u].suff == -1) {
  81. if (trie[u].p == 0) {
  82. trie[u].suff = 0;
  83. }
  84. else {
  85. return go(getsuff(trie[u].p), trie[u].c);
  86. }
  87. }
  88. return trie[u].suff;
  89. }
  90.  
  91. int go(int u, char c) {
  92. if (trie[u].go[c] == -1) {
  93. if (trie[u].move[c] != -1) {
  94. trie[u].go[c] = trie[u].move[c];
  95. }
  96. else {
  97. if (u != 0) {
  98. trie[u].go[c] = go(getsuff(u), c);
  99. }
  100. else {
  101. trie[u].go[c] = 0;
  102. }
  103. }
  104. }
  105. return trie[u].go[c];
  106. }
  107.  
  108. int supersuff(int u) {
  109. if (trie[u].supersuff == -1) {
  110. if (!trie[getsuff(u)].term.empty()) {
  111. trie[u].supersuff = getsuff(u);
  112. }
  113. else {
  114. trie[u].supersuff = supersuff(getsuff(u));
  115. }
  116. }
  117. return trie[u].supersuff;
  118. }
  119.  
  120. void dfs(int v, int pos) {
  121. if (v == 0) return;
  122.  
  123. if (trie[v].term.size()) {
  124. for (auto i : trie[v].term) {
  125. ans[i].emplace_back(pos);
  126. }
  127. }
  128. dfs(supersuff(v), pos);
  129. }
  130.  
  131. signed main() {
  132. kekek;
  133. node root(0, -1);
  134. root.suff = 0;
  135. root.term.push_back(-1);
  136. root.supersuff = 0;
  137. trie[0] = root;
  138.  
  139. string t;
  140. cin >> t;
  141.  
  142. int n;
  143. cin >> n;
  144. string *kek = new string[n];
  145. for (int i = 0; i < n; i++) {
  146. cin >> kek[i];
  147. add(kek[i], i);
  148. }
  149.  
  150. ans.resize(n + 1);
  151.  
  152. int v = 0;
  153. for (int i = 0; i < t.size(); i++) {
  154. v = go(v, t[i] - 'a');
  155. dfs(v, i + 1);
  156. }
  157.  
  158. for (int i = 0; i < n; i++) {
  159. cout << ans[i].size() << ' ';
  160. for (auto j : ans[i]) {
  161. cout << j - kek[i].size() + 1 << " ";
  162. }
  163. cout << '\n';
  164. }
  165.  
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement