题解:P14161 [ICPC 2022 Nanjing R] 完美回文
本题对于完美回文的描述已经说明在字符串中所有元素都必须相同,否则没有任何一个字符串能满足定义,那么将字符串中每个字符的数量进行统计,记录最大值,再与字符串长度相减就是答案。
代码如下:
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N=1e6+6,inf=2e18;
typedef pair<int,int>PII;
int T,len,mx;
string s;
unordered_map<int,int>mp;//与桶效果一样
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>T;
while(T--){
mx=0;//记得清零
cin>>s;
len=s.size();
for(int i=1;i<=len;i++)
mp[s[i-1]-'a']++;
for(auto t:mp)
mx=max(t.second,mx);//寻找最大值
cout<<len-mx<<endl;
mp.clear();//一定要清空
}
return 0;
}