uq_57476137480


私信TA

用户名:uq_57476137480

访问量:480

签 名:

等  级
排  名 10937
经  验 1059
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

重复字符串
浏览:223

解题思路:

注意事项:

参考代码:


#include<iostream>
#include<string>
#include<vector>
#include<cstring>
using namespace std;

int main() {
int k;
string s;
cin >> k >> s;
if (k > s.size()) //特殊情况(可能没用
cout << -1 << endl; return 0; 

int n = s.size() / k;// 每组个数
vector<string> str;
for (int i = 0; i < k; i++)
    str.push_back(s.substr(i * n, n));// 将每一组拆开  0-(k-1),k-(2k-1),...

int flag[26]; // 26个字母的个数
int ans = 0;
for (int i = 0; i < n; i++) {
    memset(flag, 0, sizeof(flag));
    for (int j = 0; j < k; j++) 
        flag[str[j][i] - 'a']++; // 第j组的第i个数字

    int max1 = -1;
    for (int j = 0; j < 26; j++) 
    max1 = max(max1, flag[j]);
    ans += (k - max1);//此时该位上的出现字母数最多的次数是max1,将剩下的数字改成它,要k(每组个数)  -  max1 次
}

cout << ans << endl;
    return 0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
}


 

0.0分

3 人评分

  评论区

  • «
  • »