limimage

AHOKARASIC

Nov 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 1e5 + 1;
  10. constexpr int mod = 1e9 + 7;
  11.  
  12. struct Node{
  13. bool isTerm;
  14. int link;
  15. int nextTerm;
  16. int par;
  17. int symb;
  18. vector<int> go;
  19. vector<int> next;
  20. Node(){
  21. next.resize(126 - 31);
  22. go.resize(126 - 31);
  23. nextTerm = 0;
  24. link = 0;
  25. par = 0;
  26. symb = 0;
  27. }
  28. };
  29.  
  30. vector<Node> trie;
  31. int n;
  32. string query, text;
  33. vector<bool> ans;
  34. vector<bool> used;
  35. vector<int> temp;
  36.  
  37.  
  38. void build(){
  39. trie.emplace_back();
  40. }
  41.  
  42. void add(string& s){
  43. int i = 0;
  44. for (auto c: s){
  45. if (trie[i].next[c - 32] == 0){
  46. trie[i].next[c - 32] = trie.size();
  47. trie.emplace_back();
  48. trie.back().par = i;
  49. trie.back().symb = c - 32;
  50. }
  51. i = trie[i].next[c - 32];
  52. }
  53. trie[i].isTerm = 1;
  54. }
  55.  
  56. void init(){
  57. queue<int> q;
  58. trie[0].link = trie[0].par = 0;
  59. for (int i = 0; i < 126 - 31; i++){
  60. if (trie[0].next[i] == 0){
  61. trie[0].go[i] = 0;
  62. }
  63. else {
  64. trie[0].go[i] = trie[0].next[i];
  65. q.push(trie[0].next[i]);
  66. }
  67. }
  68. while(!q.empty()){
  69.  
  70. int v = q.front();
  71. q.pop();
  72.  
  73. trie[v].link = trie[v].par ? trie[trie[trie[v].par].link].go[trie[v].symb] : 0;
  74. trie[v].nextTerm = trie[trie[v].link].isTerm ? trie[v].link : trie[trie[v].link].nextTerm;
  75.  
  76. for (int i = 0; i < 126 - 31; i++){
  77. if (trie[v].next[i]){
  78. trie[v].go[i] = trie[v].next[i];
  79. q.push(trie[v].next[i]);
  80. }
  81. else
  82. trie[v].go[i] = trie[trie[v].link].go[i];
  83. }
  84. }
  85. }
  86.  
  87. bool work(string& t){
  88. int v = 0;
  89. bool ans = 0;
  90. for (auto c: t){
  91. v = trie[v].go[c - 32];
  92. if (!used[v]){
  93. for (int u = v; u && !used[u]; u = trie[u].nextTerm){
  94. if (trie[u].isTerm){
  95. ans = 1;
  96. break;
  97. }
  98. used[u] = 1;
  99. temp.push_back(u);
  100. }
  101. if (ans)
  102. break;
  103. }
  104. }
  105. while(temp.size()){
  106. used[temp.back()] = 0;
  107. temp.pop_back();
  108. }
  109. return ans;
  110. }
  111.  
  112.  
  113. void Solve() {
  114. build();
  115. cin >> n;
  116. getline(cin, query);
  117. for (int i = 0; i < n; i++){
  118. getline(cin, query);
  119. add(query);
  120. }
  121. init();
  122. used.resize(trie.size());
  123. while(getline(cin, text)){
  124. cout << (work(text) ? text : "") << endl;
  125. }
  126. }
  127.  
  128. int main(){
  129. ios::sync_with_stdio(false);
  130. cin.tie(nullptr);
  131. cout.tie(nullptr);
  132. Solve();
  133. return 0;
  134. }
Add Comment
Please, Sign In to add comment