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
| #include<bits/stdc++.h> using namespace std; typedef double db; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define fi first #define se second #define pb push_back #define mp make_pair #define sz(x) ((int)(x).size()) #define all(x) begin(x),end(x) #define rep(i,l,r) for(int i=(l);i<(r);++i) #define per(i,l,r) for(int i=(r)-1;i>=(l);--i) #define dd(x) cout << #x << "=" << x << ", " #define de(x) cout << #x << "=" << x << endl
const int N = 5e5 + 7; const int M = 100; char s[N], t[N], str[2 * N];
pair<int, int> PL[2 * N], GPL[2 * N]; tuple<int, int, int> g[M], G[M];
void Palindromic(char *str) { const char *S = str - 1;
int n = strlen(str); int G_size = 0; PL[0] = {0, 0}; rep(j, 1, n + 1) { int i, d, k, g_size = 0; swap(g_size, G_size); rep(_, 0, g_size) g[_] = G[_]; rep(_, 0, g_size) { tie(i, d, k) = g[_]; if (i > 1 && S[i - 1] == S[j]) G[G_size++] = {i - 1, d, k}; }
g_size = 0; int r = -j; rep(_, 0, G_size) { tie(i, d, k) = G[_]; if (i - r != d) { g[g_size++] = {i, i - r, 1}; if (k > 1) g[g_size++] = {i + d, d, k - 1}; } else g[g_size++] = {i, d, k}; r = i + (k - 1) * d; } if (j > 1 && S[j - 1] == S[j]) { g[g_size++] = {j - 1, j - 1 - r, 1}; r = j - 1; } g[g_size++] = {j, j - r, 1};
G_size = 0; tie(i, d, k) = g[0]; rep(_, 1, g_size) { if (get<1>(g[_]) == d) k += get<2>(g[_]); else { G[G_size++] = {i, d, k}; tie(i, d, k) = g[_]; } } G[G_size++] = {i, d, k}; PL[j] = {n + 1, 0}; if (j % 2 == 0 && S[j - 1] == S[j]) PL[j] = min(PL[j], make_pair(PL[j - 2].first, j - 2)); rep(_, 0, G_size) { tie(i, d, k) = G[_]; r = i + (k - 1) * d; pair<int, int> m = {PL[r - 1].first + 1, r - 1}; if (k > 1) m = min(m, GPL[i - d]); if (d <= i) GPL[i - d] = m; if (~j & 1) PL[j] = min(PL[j], m); } } }
bool same(int l, int r, const char *s, const char *t) { rep(i, l, r + 1) if (s[i] != t[i]) return false; return true; }
int main() { scanf(" %s %s", s, t); int n = strlen(s); rep(i, 0, n) { str[2 * i] = s[i]; str[2 * i + 1] = t[i]; } str[2 * n] = 0;
Palindromic(str);
if (PL[2 * n].first > n) { puts("-1"); } else { vector<pair<int, int> > ans; for (int r = 2 * n; r > 0; r = PL[r].second) { int L = PL[r].second / 2; int R = (r - 1) / 2; if (!same(L, R, s, t)) ans.emplace_back(make_pair(L + 1, R + 1)); } printf("%d\n", ans.size()); for (auto p : ans) printf("%d %d\n", p.first, p.second); }
return 0; }
|