P15606 [ICPC 2021 Jakarta R] Uniform Maker
化简题目
有 个长度为 的字符串,每次操作可将任意一个字符改为另一个小写字母。求最少修改次数,使得所有字符串完全相同。
思路分析
要使所有字符串相同,最终字符串的每一位可以独立考虑。
对于第 位,共有 个字符。要使这些字符相同,应选择出现次数最多的字符作为目标字符,这样需要修改的字符数最少,为 。
对每一位都这样处理,将每一位的最小修改次数累加,即为总答案。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 110;
int n, m;
string a[N];
int cnt[30];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> a[i];
}
int ans = 0;
for(int i = 0; i < m; i++){ //枚举每一位
memset(cnt, 0, sizeof(cnt)); //不要忘了初始化
for(int j = 0; j < n; j++){ //统计该位各字符出现次数
cnt[a[j][i] - 'a']++;
}
int mmax = 0;
for(int k = 0; k < 26; k++){
mmax = max(mmax, cnt[k]);
}
ans += n - mmax; //该位最少的修改次数
}
cout << ans;
return 0;
}
恳求
求管理员审核通过,求各位点赞评论加关注再走吧qwq