ABB
题意:n个字符串,求 使得从左往右和从右往左看的字符一样的 添加字符的最少的个数,即添加最少的字符串使其构成回文串
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int n,ans;
char s[maxn * 2], str[maxn * 2];
int Len[maxn * 2], len;
void getstr() {//重定义字符串
int k = 0;
str[k++] = '@';//开头加个特殊字符防止越界
for (int i = 0; i < len; i++) {
str[k++] = '#';
str[k++] = s[i];
}
str[k++] = '#';
len = k;
str[k] = 0;//字符串尾设置为0,防止越界
}
void manacher() {
int mx = 0, id;//mx为最右边,id为中心点
int maxx = 0;
for (int i = 1; i < len; i++) {
if (mx > i) Len[i] = min(mx - i, Len[2 * id - i]);//判断当前点超没超过mx
else Len[i] = 1;//超过了就让他等于1,之后再进行查找
while (str[i + Len[i]] == str[i - Len[i]]) Len[i]++;//判断当前点是不是最长回文子串,不断的向右扩展
if (Len[i] + i > mx) {//更新mx
mx = Len[i] + i;
id = i;//更新中间点
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> s;
len = strlen(s);
getstr();manacher();
ans = maxn;
for(int i = 1; i < len; i++){
if(i + Len[i] - 1 == len -1){
if(str[i] == '#') ans = min(ans,i / 2 - (n - i / 2));
else ans = min(ans,i / 2 - 1 - (n - i / 2));
}
}
cout << ans << endl;
return 0;
}