查找获取"Good"字符串的最小移动次数
Find the minimum number of moves to get a "Good" string
当且仅当"字符串中的所有不同字符都重复相同次数"时,调用字符串是好的。
现在,给定一个长度为n的字符串,为了使字符串变得好,我们必须对这个字符串进行的最小更改次数是多少。
注意:我们只允许使用小写英文字母,并且我们可以将任何字母更改为任何其他字母。
示例:让字符串为yyxzzxxx
那么这里的答案是2。
解释:一种可能的解决方案yyxyyxx。我们已将2'z'更改为2'y'。现在"x"answers"y"都重复4次。
我的方法:
- 对所有26个小写字母的出现情况进行散列
- 还要查找字符串中不同字母的数目
- 对这个哈希数组进行排序,并开始检查字符串的长度是否可以被不同字符的数量整除。如果是,那么我们得到了答案
- 否则,将不同字符减少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;
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 如何将不可移动和不可复制的函数返回值获取到数组中
- 如何获取类型是否真正可移动可构造
- 使用Arduino获取移动IMEI
- 如何获取wxGrid中移动列的新索引
- 算法,用于查找获取排序列表所需的最小移动元素到结束步骤
- 为了支持移动语义,应通过unique_ptr,值或rvalue获取功能参数
- 如何重写 AcDbEntity::transformBy 以在移动或缩放时获取自定义实体转换
- 在Qt中获取原始鼠标移动
- 如何在C++中获取快速排序的移动,交换和比较计数
- 如何在鼠标移动期间获取 QGraphicsRectItem 的位置?
- QML:如何在不实际移动任何可见组件的情况下,在鼠标Aeria顶部获取鼠标移动的方向
- 在移动之前获取右值引用的地址被认为是安全的
- 查找获取"Good"字符串的最小移动次数
- 在窗口循环中,在窗口控件上获取鼠标移动比子类化更容易
- 正在获取要使用的移动构造函数/赋值运算符
- Qt:从Qsplitter获取鼠标移动事件
- 如何在指定的注册表值更改时在ATL ActiveX控件中获取事件通知以用作windows移动ActiveX控件
- 获取鼠标滚轮移动数据
- 移除移动物体,从多个图像中获取背景模型