长度为偶数的串,重排连续字串变成回文串。
Problem - D - Codeforces
代码:
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const int N=1e5+110;
void solve()
{
string s;
cin>>s;
int n=s.size();
int ans=n;
array<int,26> cnt{};
for(int i=0; i<n; i++){
cnt[s[i]-'a']+=i<n/2 ? 1 :-1;
}
int x=0;
while(x<n/2 && s[x]==s[n-1-x]){
x++;
}
if(count(cnt.begin(), cnt.end(),0)==26){
int y=0;
while(x+y<n/2 && s[n/2+y]==s[n/2-1-y]){
y++;
}
ans=min(ans,n/2-x-y);
} else{
for(int t=0; t<2; t++){
array<int,26> cnt{};
for(auto c : s){
cnt[c-'a']++;
}
for(auto &x: cnt){
x/=2;
}
for(int i=0; i<n/2; i++){
if(!cnt[s[i]-'a']--){
ans=min(ans,n-x-i);
break;
}
}
reverse(s.begin(),s.end());
}
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int _=1;
cin>>_;
while(_--){
solve();
}
return 0;
}
未知算法:ManacherManacher算法详解 - BT-7274 - 博客园