【題解】Codeforces 1506C. Double-ended Strings

【題目敘述】https://codeforces.com/contest/1506/problem/C

#include <iostream>
#include <string>
using namespace std;

int main() {
    int t;
    string a, b;
    cin >> t;
    while (t--) {
        cin >> a >> b;
        int len_a = (int)a.size();
        int len_b = (int)b.size();
        int ans = 0;
        for (int len = 1; len <= min(len_a, len_b); len++) {
            //枚舉len:最終 a 和 b 的長度
            for (int left_a = 0; left_a + len <= len_a; left_a++) {
                for (int left_b = 0; left_b + len <= len_b; left_b++) {
                    //枚舉 a 和 b 的左端點
                    if (a.substr(left_a, len) == b.substr(left_b, len)) {
                        ans = max(ans, len);
                    }
                }
            }
        }
        cout << len_a + len_b - 2 * ans << "\n";
        
    }
    return 0;
}
分享本文 Share with friends