Idea
Code
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<string>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define MAX_SIZE 10005
#define MOD 1000000009
int main(){
int k;
string str;
cin>>k>>str;
int n = str.size();
if(n % k != 0) {
cout<<"-1";
return 0;
}
int cnt = 0;
for(int i = 0;i< (n / k) ;i++){
int j = 0;
unordered_map<char,int> st;
while(j < k){
st[str[j * n / k + i]]++;
j++;
}
int m = -1;
for(auto item : st) m = max(m,item.second);
cnt += k - m;
}
cout<<cnt;
return 0;
}
另附:OJ评测地址