题意

给定两个长度相同的字符串,问能否切割两个字符串的相同位置,再进行组合,得到一个回文串。

思路

  1. 首先从串a首、串b尾开始配对,检索是否对称
  2. 如果不对称,尾指针上浮到串a继续检索
  3. 如果不对称,两个指针同时下沉到串b继续检索

如果中间任意一个检索,让指针走到了length/2的位置,即可得到回文串。

完成上述操作后,重置指针为串a尾串b首继续搜索一遍。

solution

#include <bits/stdc++.h>
#define sc(x) scanf("%lld", &(x))
#define pr(x) printf("%lld\n", x)
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const ll mod = 1e9 + 7;
bool chk(string s) {
    int n = s.length();
    for (int i = 0; i < n / 2; ++i) {
        if (s[i] != s[n - 1 - i]) return 0;
    }
    return 1;
}
bool checkPalindromeFormation(string a, string b) {
    int n = a.length();
    if (chk(a) || chk(b)) return 1;
    if (a[0] != b[n - 1] && a[n - 1] != b[0]) return 0;
    int L = 0, R = n - 1;
    while (L < R && a[L] == b[R]) {
        ++L, --R;
        if (L == n / 2) return 1;
    }
    while (L < R && a[L] == a[R]) {
        ++L, --R;
        if (L == n / 2) return 1;
    }
    while (L < R && b[L] == b[R]) {
        ++L, --R;
        if (L == n / 2) return 1;
    }
    L = n - 1, R = 0;
    while (L > R && a[L] == b[R]) {
        --L, ++R;
        if (R == n / 2) return 1;
    }
    while (L > R && a[L] == a[R]) {
        --L, ++R;
        if (R == n / 2) return 1;
    }
    while (L > R && b[L] == b[R]) {
        --L, ++R;
        if (R == n / 2) return 1;
    }
    return 0;
}
int main() {
    string s, t;
    while (cin >> s >> t) cout << checkPalindromeFormation(s, t) << endl;
    return 0;
}
// abdef fecab
// cdeoo oooab
// pvhmupgqeltozftlmfjjde yjgpzbezspnnpszebzmhvp