制造偶串的O(n)算法,应用KMP的next数组。
#include <bits/stdc++.h>
using namespace std;

int solve(string& s) {
    vector<int> next(s.size(), 0);
    int j = 0, i = 1;
    while (i < s.size()) {
        if (s[i] == s[j]) {
            next[i] = j+1;
            ++i;
            ++j;
        }
        else if (j != 0) {
            j = next[j-1];
        } else {
            ++i;
        }
    }
    for (i = next.size() - 3; i >= 0; i -= 2) {
        if (next[i] * 2 == i + 1 || next[i] == i) {
            return i + 1;
        }
    }
    return 0;
}

int main() {
    freopen("in.txt", "r", stdin);
    string s;
    while (cin >> s) {
        cout << solve(s) << endl;
    }
    return 0;
}