Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <fstream>
- #include <set>
- #include <vector>
- #include <map>
- #include <queue>
- #include <stack>
- #include <string>
- #include <cstring>
- #include <unordered_set>
- #include <unordered_map>
- #include <random>
- #include <chrono>
- #include <ctime>
- #include <complex>
- #include <bitset>
- using namespace std;
- #define int long long
- #define ld long double
- #define nl "\n"
- #define pb push_back
- #define xx first
- #define yy second
- #define cinn(a) for (int i = 0; i < (int) a.size(); i++) cin >> a[i];
- #define coutt(a) for (int i = 0; i < (int) a.size(); i++) cout << a[i] << (i == a.size() - 1 ? nl : " ");
- #define sortt(a) sort(a.begin(), a.end());
- #define rev(a) reverse(a.begin(), a.end());
- #define hi(a) (--a.end())
- #define lo(a) a.begin()
- #define ll(a) int a; cin >> a;
- #define vi(a, n) vector<int> a(n); cinn(a);
- #define forn(i, n) for (int i = 0; i < n; i++)
- #define nfor(i, n) for (int i = n; i >= 0; i--)
- #define foreach(i, st) for (auto & i : st)
- const int inf = 1e15 + 7;
- const int N = 2e5 + 69;
- const int M = 998244353;
- struct bor {
- const static int al = 26;
- const static char a = 'a';
- struct node {
- int indexEnd = 0;
- int visited = 0;
- node* to[al];
- node() {
- memset(to, 0, sizeof(to));
- }
- };
- node* root = new node();
- void add(string& s, int ind) {
- node* now = root;
- now->visited++;
- for (int i = 0; i < s.size(); i++) {
- if (!now->to[s[i] - a]) {
- now->to[s[i] - a] = new node();
- }
- now = now->to[s[i] - a];
- now->visited++;
- }
- now->indexEnd = ind;
- }
- int find(string& s) {
- node* now = root;
- bool same = 1;
- int i = 0;
- for (; i < s.size(); i++) {
- node* next = now->to[s[i] - a];
- if (!next) break;
- if (s[s.size() - i - 1] == s[i] && next->visited < 2) break;
- now = next;
- }
- while (!now->indexEnd) {
- for (int nxt = 0; nxt < al; nxt++) {
- node* next = now->to[nxt];
- if (!next) continue;
- if (same && i < s.size() && s[s.size() - i - 1] - a == nxt
- && next->visited < 2) continue;
- if (i >= s.size() || s[i] - a != nxt) same = 0;
- now = next;
- break;
- }
- i++;
- }
- return now->indexEnd;
- }
- };
- void solve() {
- ll(n);
- vector<string> s(n);
- vector<string> rs(n);
- cinn(s);
- rs = s;
- bor b;
- for (int i = 0; i < n; i++) {
- b.add(s[i], i + 1);
- rev(rs[i]);
- }
- for (int i = 0; i < n; i++) {
- int ind = b.find(rs[i]);
- cout << s[ind - 1] << nl;
- }
- }
- signed main()
- {
- //freopen("magic.in", "r", stdin);
- //freopen("magic.out", "w", stdout);
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int start = chrono::high_resolution_clock::now().time_since_epoch().count();
- #endif
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout << fixed;
- cout.precision(10);
- int tests = 1;
- //cin >> tests;
- while (tests--) solve();
- #ifdef _DEBUG
- cout << nl << "TIME ms: ";
- cout << (chrono::high_resolution_clock::now().time_since_epoch().count() - start) / 1e6 << nl;
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement