查找获取"Good"字符串的最小移动次数

Find the minimum number of moves to get a "Good" string

本文关键字:移动 获取 Good 字符串 查找      更新时间:2023-10-16

当且仅当"字符串中的所有不同字符都重复相同次数"时,调用字符串是好的。

现在,给定一个长度为n的字符串,为了使字符串变得好,我们必须对这个字符串进行的最小更改次数是多少。

注意:我们只允许使用小写英文字母,并且我们可以将任何字母更改为任何其他字母。

示例:让字符串为yyxzzxxx

那么这里的答案是2。

解释:一种可能的解决方案yyxyyxx。我们已将2'z'更改为2'y'。现在"x"answers"y"都重复4次。

我的方法:

  1. 对所有26个小写字母的出现情况进行散列
  2. 还要查找字符串中不同字母的数目
  3. 对这个哈希数组进行排序,并开始检查字符串的长度是否可以被不同字符的数量整除。如果是,那么我们得到了答案
  4. 否则,将不同字符减少1

但是,它对某些结果给出了错误的答案,因为在删除一些没有出现的字符时,可能会出现错误。最短时间可以在较少的移动中提供一个好的字符串。

那么如何做这个问题。请帮忙。

限制条件:字符串长度可达2000。

我的方法:

string s;
    cin>>s;
    int hash[26]={0};
    int total=s.length();
    for(int i=0;i<26;i++){
        hash[s[i]-'a']++;
    }
    sort(hash,hash+total);
    int ans=0;
    for(int i=26;i>=1;i--){
        int moves=0;
        if(total%i==0){
            int eachshouldhave=total/i;
            int position=26;
            for(int j=1;j<26;j++){
                if(hash[j]>eachshouldhave && hash[j-1]<eachshouldhave){
                    position=j;
                    break;
                }
            }
            int extrasymbols=0;
            //THE ONES THAT ARE BELOW OBVIOUSLY NEED TO BE CHANGED TO SOME OTHER SYMBOL
            for(int j=position;j<26;j++){
                extrasymbols+=hash[j]-eachshouldhave;
            }
            //THE ONES ABOVE THIS POSITION NEED TO GET SOME SYMBOLS FROM OTHERS
            for(int j=0;j<position;j++){
                moves+=(eachshouldhave-hash[j]);
            }
            if(moves<ans)
            ans=moves;
        }
        else
        continue;
    }

以下应该修复您的实现:

std::size_t compute_change_needed(const std::string& s)
{
    int count[26] = { 0 };
    for(char c : s) {
        // Assuming only valid char : a-z
        count[c - 'a']++;
    }
    std::sort(std::begin(count), std::end(count), std::greater<int>{});
    std::size_t ans = s.length();
    for(std::size_t i = 1; i != 27; ++i) {
        if(s.length() % i != 0) {
            continue;
        }
        const int expected_count = s.length() / i;
        std::size_t moves = 0;
        for(std::size_t j = 0; j != i; j++) {
            moves += std::abs(count[j] - expected_count);
        }
        ans = std::min(ans, moves);
    }
    return ans;
}