由于每次相同的字母都是一起变的,所以相同的字母可以看作一个整体。
先在原串上跑一遍回文,用并查集维护哪些字母要变成一样的。
然后修改的时候当然就是把出现次数少的修改成出现次数多的……
一开始智障以为是像合并果子一样的…结果发现只要全都合并到最大的那个上就好了
于是代码就被我魔改成了堆排???
//tc is healthy, just do it
#include <bits/stdc++.h>
using namespace std;
const int N=26;
int n;
int f[N];
int tot[N];
priority_queue<int,vector<int>,greater<int> > q;
template<class T> void checkmin(T &a,const T &b) { if (b<a) a=b; }
template<class T> void checkmax(T &a,const T &b) { if (b>a) a=b; }
class GooseTattarrattatDiv1 {
public:
int getmin( string S ) ;
};
int gf(int x){
return f[x]==x?x:f[x]=gf(f[x]);
}
void unio(int x,int y){
int fx=gf(x),fy=gf(y);
if (fx!=fy) f[fx]=fy;
}
int GooseTattarrattatDiv1::getmin(string S) {
n=S.size();
int L=0,r=n-1;
for(int i=0;i<N;i++) f[i]=i;
while(L<r){
unio(S[L]-'a',S[r]-'a');
L++;r--;
}
for(int i=0;i<n;i++) tot[S[i]-'a']++;
int ans=0;
for(int i=0;i<N;i++)
if (f[i]==i){
while(!q.empty()) q.pop();
for(int j=0;j<N;j++)
if (gf(j)==i) q.push(tot[j]);
while(q.size()>1){
int x=q.top();q.pop();
//int y=q.top();q.pop();
ans+=x;
//q.push(x+y);
}
}
return ans;
}