1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<template<typename...> class T, typename t, typename... Args> void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E5 * 4 + 100;
namespace sam { const int M = maxn; int t[M][26], len[M] = {-1}, fa[M], sz, last; int ml[M], ml_fr[M]; int c[M] = {1}, a[M]; bool cnt[M][2]; int pos[M][2], one[M][2]; vector<int> G[M];
void init() { FOR (i, 1, sz) { memset(t[i], 0, sizeof t[0]); memset(cnt[i], 0, sizeof cnt[0]); memset(pos[i], -1, sizeof pos[0]); memset(one[i], -1, sizeof one[0]); ml[i] = c[i] = a[i] = 0; G[i].clear(); } sz = 2; last = 1; } void re() { last = 1; } void ins(int ch, int id, int pp) { int p = last, np = 0, nq = 0, q = -1; if (!t[p][ch]) { np = sz++; len[np] = len[p] + 1; for (; p && !t[p][ch]; p = fa[p]) t[p][ch] = np; } if (!p) fa[np] = 1; else { q = t[p][ch]; if (len[p] + 1 == len[q]) fa[np] = q; else { nq = sz++; len[nq] = len[p] + 1; memcpy(t[nq], t[q], sizeof t[0]); memcpy(one[nq], one[q], sizeof t[0]); fa[nq] = fa[q]; fa[np] = fa[q] = nq; for (; t[p][ch] == q; p = fa[p]) t[p][ch] = nq; } } last = np ? np : nq ? nq : q; cnt[last][id] = 1; pos[last][id] = pp; } void rsort() { FOR (i, 1, sz) c[len[i]]++; FOR (i, 1, sz) c[i] += c[i - 1]; FOR (i, 1, sz) a[--c[len[i]]] = i; } void build() { FOR (i, 2, sz) G[fa[i]].push_back(i); } }
namespace pam { const int M = maxn; int t[M][26], fa[M], len[M], rs[M]; int sz, n, last; int N(int l) { memset(t[sz], 0, sizeof t[0]); len[sz] = l; return sz++; } void init() { rs[n = sz = 0] = -1; last = N(0); fa[last] = N(-1); } int get_fa(int p) { while (rs[n - 1 - len[p]] != rs[n]) p = fa[p]; return p; } void ins(int ch) { rs[++n] = ch; int p = get_fa(last); if (!t[p][ch]) { int np = N(len[p] + 2); fa[np] = t[get_fa(fa[p])][ch]; t[p][ch] = np; } last = t[p][ch]; } }
void add(string& s, char* a, char* b, int la, int ra, int lb, int rb) { s.clear(); FOR (i, lb, rb) s += b[i]; FOR (i, la, ra) s += a[i]; }
int pa_ml[maxn], pa_ml_fr[maxn]; string go(char* a, char* b) { sam::init(); int la = strlen(a), lb = strlen(b); FORD (i, la - 1, -1) sam::ins(a[i] - 'a', 0, i); sam::re(); FOR (i, 0, lb) sam::ins(b[i] - 'a', 1, i); sam::cnt[1][1] = 1; sam::rsort(); sam::build(); { using namespace sam; FORD (i, sz - 1, 1) { int u = sam::a[i]; if (cnt[u][1]) { cnt[fa[u]][1] |= cnt[u][1]; pos[fa[u]][1] = pos[u][1]; } } FOR (i, 1, sz) { int u = sam::a[i]; if (cnt[u][1]) ml[u] = len[u], ml_fr[u] = pos[u][1]; for (int& v: G[u]) ml[v] = ml[u], ml_fr[v] = ml_fr[u]; } FOR (i, 1, sz) if (pos[i][0] != -1) { pa_ml[pos[i][0]] = ml[i]; pa_ml_fr[pos[i][0]] = ml_fr[i]; } } pam::init(); int ans = 0, al = -1, ar = -1, bl = -1, br = -1; string last, nxt; pa_ml[la] = 0; FOR (i, -1, la) { pam::ins(a[i] - 'a'); int pl = i >= 0 ? pam::len[pam::last] : 0; // notice! int pl2 = pa_ml[i + 1]; int cur = pl + pl2 * 2; if (pl2 && cur >= ans) { if (cur == ans) add(last, a, b, al, ar, bl, br); int arr = i + 1 + pl2, all = arr - pl - pl2; int brr = pa_ml_fr[i + 1] + 1, bll = brr - pl2; if (cur == ans) { add(nxt, a, b, all, arr, bll, brr); if (last < nxt) continue; } ans = cur; al = all; ar = arr; bl = bll; br = brr; } } add(last, a, b, al, ar, bl, br); return last; }
char a[maxn], b[maxn]; int main() { int T; cin >> T; memset(sam::pos, -1, sizeof sam::pos); memset(sam::one, -1, sizeof sam::one); while (T--) { scanf("%s%s", a, b); auto ans1 = go(b, a); reverse(a, a + strlen(a)); reverse(b, b + strlen(b)); auto ans2 = go(a, b); string ans = ans1; if (ans.length() < ans2.length() || (ans.length() == ans2.length() && ans > ans2)) ans = ans2; cout << (ans.empty() ? "-1" : ans) << endl; } }
|