跳到正文

题解

题解:P14161 [ICPC 2022 Nanjing R] 完美回文

Gartjock约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年10月7日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟
题目
P14161

版本

  1. v1当前

本题对于完美回文的描述已经说明在字符串中所有元素都必须相同,否则没有任何一个字符串能满足定义,那么将字符串中每个字符的数量进行统计,记录最大值,再与字符串长度相减就是答案。

代码如下:

C++
#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;
}
原文