在最少的操作中实现字符串所有字符的相同频率。(所有字符的范围从'a'到"z")
Achieve same frequency of all characters of string in minimum operations. (All characters are from range 'a' to 'z')
我们需要找到使给定字符串中所有字符的频率相等所需的最小替换次数,其中替换意味着我们可以用其他字符替换字符串的任何字符。
**所有字符应从范围"A"到"z"。
我找到字符串中所有字符的频率,然后对其进行排序以找到中位数,然后以中位数作为参考来计算实现相同频率所需的成本。我不知道我哪里出错了。不是实际的逻辑,但最轻微的想法将受到赞赏.
#include <bits/stdc++.h>
using namespace std;
long int min(int A[], long int n)
{
long int cost = 0;
sort(A, A + n);
long int K = A[n / 2];
for (long int i = 0; i < n; ++i) {
if (A[i] - K)
cost += abs(A[i] - K);
}
if (n % 2 == 0) {
long int tempcost = 0;
K = A[(n / 2) - 1];
for (long int i = 0; i < n; ++i) {
if (A[i] - K)
tempcost += abs(A[i] - K);
}
cost = min(cost, tempcost);
}
return cost;
}
int main()
{
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
long int arr[26];
for (int i = 0; i < 26; i++) {
arr[i] = 0;
}
long int n = s.length();
long int count = 0;
for (long int i = 0; i < n; i++) {
arr[s[i] - 'a']++;
}
for (int i = 0; i < n; i++) {
if (arr[i]) {
count++;
}
}
int a[count], j = 0;
for (long int i = 0; i < n; i++) {
if (arr[i]) {
a[j++] = arr[i];
}
}
cout << min(a, count) << endl;
}
return 0;
}
预期成果 : 输入 : 1 aaaaabbbccd 输出: 3
注意:我在最后给出了一个建议,但关于OP程序:
所有字符应从范围"a"到"z"
在主
arr[s[i] - 'A']++;
必须是
arr[s[i] - 'a']++;
这样做改变执行是:
pi@raspberrypi:/tmp $ ./a.out
1
aab
1
long int arr[26];
for (int i = 0; i < 26; i++) {
arr[i] = 0;
}
可以就是:
long int arr[26] = { 0 };
使用野蛮武力的解决方案
#include <iostream>
#include <string>
using namespace std;
bool isAsolution(const string & s)
{
int count['z' - 'a' + 1] = { 0 };
for (auto c : s)
count[c - 'a'] += 1;
string::const_iterator it = s.begin();
int count0 = count[*it++ - 'a'];
while (it != s.end())
if (count[*it++ - 'a'] != count0)
return false;
return true;
}
void search(string & s, size_t pos, unsigned nChanges, unsigned & minChanges, string & sol)
{
if (pos == s.length()) {
// nChanges < minChanges, useless to check
if (isAsolution(s)) {
minChanges = nChanges;
sol = s;
}
}
else if (nChanges < (minChanges - 1)) {
for (char c = 'a'; c <= 'z'; c += 1) {
// the string is modified to avoid a lot of copies
char old = s[pos];
s[pos] = c;
search(s, pos + 1, (old == c) ? nChanges : (nChanges + 1), minChanges, sol);
s[pos] = old;
}
}
}
int main(int argc, char ** argv)
{
if (argc == 1) {
cout << "Usage : " << *argv << " <string> ... <string>" << endl;
return 0;
}
while (*++argv) {
string s = *argv;
for (auto c : s) {
if ((c < 'a') || (c > 'z')) {
cout << "invalid input, chars must be between a and z" << endl;
return 0;
}
}
if (s.empty() || isAsolution(s))
cout << s << " is already a solution" << endl;
else {
unsigned min = ~0u;
string sol;
search(s, 0, 0, min, sol);
cout << min << " changes to do " << sol << " from " << s << endl;
}
}
return 0;
}
而不是读取字符串的数量,然后读取它们在参数中给出,更实用
例:
pi@raspberrypi:/tmp $ g++ -pedantic -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out aab aze aaaaabbbbccd
1 changes to do aaa from aab
aze is already a solution
2 changes to do aaaacbbbbccc from aaaaabbbbccd
在瓦尔格林德:
pi@raspberrypi:/tmp $ valgrind ./a.out aab aze aaaaabbbbccd
==8364== Memcheck, a memory error detector
==8364== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8364== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8364== Command: ./a.out aab aze aaaaabbbbccd
==8364==
1 changes to do aaa from aab
aze is already a solution
2 changes to do aaaacbbbbccc from aaaaabbbbccd
==8364==
==8364== HEAP SUMMARY:
==8364== in use at exit: 0 bytes in 0 blocks
==8364== total heap usage: 2 allocs, 2 frees, 21,248 bytes allocated
==8364==
==8364== All heap blocks were freed -- no leaks are possible
==8364==
==8364== For counts of detected and suppressed errors, rerun with: -v
==8364== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
正如您所看到的,aaaabbbbccd 只需要 2 个更改,而不是 3 个 :-)
相关文章:
- 将超出范围的整数分配给有符号字符类型
- C++ - 使用基于范围的 for 循环将字符值分配给向量中的字符串不会分配值
- 将字符移出范围的危险
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 访问字符阵列中不可用的内存位置(超出范围值)
- 如何在 C++ 的字符串中打印从一个索引到另一个索引的字符范围
- 提升精神范围字符规则创建空字符
- 在最少的操作中实现字符串所有字符的相同频率。(所有字符的范围从'a'到"z")
- 输出在我的字符数组范围之外,同时使用 rand()
- C++ - "(常量字符*)"未在范围内声明?
- 如何将字符的范围从字符数组复制到矢量
- 提升正则和字符范围
- 循环直到整数输入在所需范围内无法处理非数字字符输入
- 如何检查字符是否在范围内
- 使用cctype库搜索函数,以查找某个范围内的数字字符数
- C++向量、常量字符*、变量范围和生存期
- 如何在c++中检查字符的unicode值的范围
- 将元素范围从字符数组提取到字符串中
- 在c++中指定一个ASCII小写字符范围
- 如何检查字符是否在给定的字符范围内