Advertisement
slash0t

Untitled

Mar 15th, 2025 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define _USE_MATH_DEFINES
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <fstream>
  8. #include <set>
  9. #include <vector>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <string>
  14. #include <cstring>
  15. #include <unordered_set>
  16. #include <unordered_map>
  17. #include <random>
  18. #include <chrono>
  19. #include <ctime>
  20. #include <complex>
  21. #include <bitset>
  22.  
  23. using namespace std;
  24.  
  25. #define int long long
  26. #define ld long double
  27. #define nl "\n"
  28. #define pb push_back
  29. #define xx first
  30. #define yy second
  31. #define cinn(a) for (int i = 0; i < (int) a.size(); i++) cin >> a[i];
  32. #define coutt(a) for (int i = 0; i < (int) a.size(); i++) cout << a[i] << (i == a.size() - 1 ? nl : " ");
  33. #define sortt(a) sort(a.begin(), a.end());
  34. #define rev(a) reverse(a.begin(), a.end());
  35. #define hi(a) (--a.end())
  36. #define lo(a) a.begin()
  37. #define ll(a) int a; cin >> a;
  38. #define vi(a, n) vector<int> a(n); cinn(a);
  39. #define forn(i, n) for (int i = 0; i < n; i++)
  40. #define nfor(i, n) for (int i = n; i >= 0; i--)
  41. #define foreach(i, st) for (auto & i : st)
  42.  
  43. const int inf = 1e15 + 7;
  44. const int N = 2e5 + 69;
  45. const int M = 998244353;
  46.  
  47. struct bor {
  48. const static int al = 26;
  49. const static char a = 'a';
  50. struct node {
  51. int indexEnd = 0;
  52. int visited = 0;
  53. node* to[al];
  54. node() {
  55. memset(to, 0, sizeof(to));
  56. }
  57. };
  58.  
  59. node* root = new node();
  60.  
  61. void add(string& s, int ind) {
  62. node* now = root;
  63. now->visited++;
  64. for (int i = 0; i < s.size(); i++) {
  65. if (!now->to[s[i] - a]) {
  66. now->to[s[i] - a] = new node();
  67. }
  68. now = now->to[s[i] - a];
  69. now->visited++;
  70. }
  71. now->indexEnd = ind;
  72. }
  73.  
  74. int find(string& s) {
  75. node* now = root;
  76.  
  77. bool same = 1;
  78. int i = 0;
  79. for (; i < s.size(); i++) {
  80. node* next = now->to[s[i] - a];
  81. if (!next) break;
  82. if (s[s.size() - i - 1] == s[i] && next->visited < 2) break;
  83. now = next;
  84. }
  85. while (!now->indexEnd) {
  86. for (int nxt = 0; nxt < al; nxt++) {
  87. node* next = now->to[nxt];
  88. if (!next) continue;
  89. if (same && i < s.size() && s[s.size() - i - 1] - a == nxt
  90. && next->visited < 2) continue;
  91.  
  92. if (i >= s.size() || s[i] - a != nxt) same = 0;
  93. now = next;
  94. break;
  95. }
  96. i++;
  97. }
  98. return now->indexEnd;
  99. }
  100. };
  101.  
  102. void solve() {
  103. ll(n);
  104. vector<string> s(n);
  105. vector<string> rs(n);
  106. cinn(s);
  107. rs = s;
  108. bor b;
  109. for (int i = 0; i < n; i++) {
  110. b.add(s[i], i + 1);
  111. rev(rs[i]);
  112. }
  113. for (int i = 0; i < n; i++) {
  114. int ind = b.find(rs[i]);
  115. cout << s[ind - 1] << nl;
  116. }
  117. }
  118.  
  119. signed main()
  120. {
  121. //freopen("magic.in", "r", stdin);
  122. //freopen("magic.out", "w", stdout);
  123. #ifdef _DEBUG
  124. freopen("input.txt", "r", stdin);
  125. freopen("output.txt", "w", stdout);
  126. int start = chrono::high_resolution_clock::now().time_since_epoch().count();
  127. #endif
  128. ios_base::sync_with_stdio(0);
  129. cin.tie(0);
  130. cout.tie(0);
  131. cout << fixed;
  132. cout.precision(10);
  133.  
  134. int tests = 1;
  135. //cin >> tests;
  136. while (tests--) solve();
  137.  
  138. #ifdef _DEBUG
  139. cout << nl << "TIME ms: ";
  140. cout << (chrono::high_resolution_clock::now().time_since_epoch().count() - start) / 1e6 << nl;
  141. #endif
  142. return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement