C++ 具有频率大于其他字符总和的字符的字符串连接数量
我们的主要目标是确定可以连接的字符串的最大数量,以确保只有一个字母的频率超过所有其他字符的总和,提供一个包含M个字符串的数组arr []。
在继续之前,让我们了解一些数组和字符串的基本概念。
数组就是一组相同数据类型的元素,在连续的内存段中保存。
C编程语言中的数组具有固定的大小,这意味着一旦指定了大小,就无法更改;你不能收缩或扩展它。
现在让我们来看看什么是字符串。字符串是一组以C编程语言中的空字符”\0″终止的字符。来自C字符串的字符被保存在字符数组中。与字符数组相反,C字符串以特殊字符null结束,这是它与字符数组的不同之处。
问题陈述
实现一个程序,确定应将具有频率大于其他字符总和的字符连接的字符串数。
示例1
Let us take the input array
arr[]: {“xyz", “yyyyx", “q”}
Output obtained is: 3
说明
这里元素”x”的频率为2。
元素”y”的频率为5,元素”z”的频率为1。最后,字符”q”的频率为1。
通过将数组中的所有三个字符串连接起来,我们得到”xyzyyyyxq”。
在这里,字符”y”的频率为5,其余字符的频率之和为4。
示例2
Let us take the input array
arr[]: {“mnoml", “lmll", “nln”, "mnlmn"}
Output obtained is : 2
解释
这里元素或字符”m”的频率是5。
元素”n”的频率是5,元素”l”的频率是6,字符”o”的频率是1。
这里我们只能将2个字符串“lmllnl”连接起来。
这里字符”l”的频率是4。其他字符”m”和”n”的频率之和是2。为了确保连接的字符串中字符的频率大于其他字符频率之和,这是唯一可能的连接方式。
方法
为了确定需要与频率大于其他字符之和的字符连接的字符串的数量,我们采用了以下方法。
解决这个问题并得到需要与频率大于其他字符之和的字符连接的字符串的数量的方法是进行迭代。
也就是说,我们通过迭代所有字符(从“a”到“z”)确定所有字符串中每个字符的净频率。在这种情况下,净频率可以通过从总频率中减去每个其他频率来计算,因此如果总净频率大于0,则说明该元素的频率超过了所有其他频率之和。
步骤
以下是确定需要与频率大于其他字符之和的字符连接的字符串的数量的算法。
- 步骤1 - 开始
-
步骤2 - 定义函数来确定所有字符的频率并减少字符串中其他频率的总和。
-
步骤3 - 迭代字符串数组a
-
步骤4 - 定义一个整数变量来存储频率
-
步骤5 - 将频率存储在数组v中
-
步骤6 - 定义一个变量来存储最大计数
-
步骤7 - 遍历所有字母或元素
-
步骤8 - 返回最大值
-
步骤9 - 停止
示例:C程序
下面是上述方法的C程序实现,用于确定需要与频率大于其他字符之和的字符连接的字符串的数量。
#include <stdio.h>
#include <stdlib.h>
//input strings to be non-empty and not more //than 100 characters
#define MAX_STR_LEN 100
// Function to determine the frequencies of all the characters and reducing the sum of other frequencies in the strings
int* frequency(char** a, int len, char c){
// We use array to store the frequency
int* v = (int*)calloc(len, sizeof(int));
if(v == NULL) {
printf("Error: Memory allocation failed");
exit(1);
}
// Iterating the array a of strings
for (int i = 0; i < len; i++) {
char* str = a[i];
// defining an integer variable for storing //the frequencies
int net_fre = 0;
// Iterating through the string str
for (int j = 0; str[j] != '\0'; j++) {
// If str[j] is equal to the current character increment the net_fre by 1
if (str[j] == c)
net_fre++;
// otherwise decrement net_fre by 1
else
net_fre--;
}
// After iterating the string store this frequency in the array v
v[i] = net_fre;
}
//return the array v
return v;
}
// Function to determine the count of the longest or the lengthiest string that could be obtained from the given array of strings
int longestConcatenatedString(char** a, int len){
// An integer variable to store the maximum count Also it is set to zero
int mxm = 0;
// Iterating through all of the alphabets
for (char c = 'a'; c <= 'z'; c++) {
// Array to store the net_frequency of the character c after reducing the sum of every other frequencies in all of the strings
int* v = frequency(a, len, c);
// Array is stored in the order of descendants
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (v[i] < v[j]) {
int temp = v[i];
v[i] = v[j];
v[j] = temp;
char* temp_str = a[i];
a[i] = a[j];
a[j] = temp_str;
}
}
}
// Variable res is defined to store the //result
int res = 0;
int sum = 0;
for (int i = 0; i < len; i++) {
sum += v[i];
// If sum is greater than 0 then increment res by 1
if (sum > 0) {
res++;
}
}
// Keeping the track of the maximum one
mxm = mxm > res ? mxm : res;
free(v);
}
// Returning the maximum value obtained
return mxm;
}
int main(){
char* a[] = { "mnoml", "lmll", "nln", "mnlmn" };
printf("Count of strings to be concatenated with a character having frequency greater than sum of others: ");
int len = sizeof(a) / sizeof(a[0]);
printf("%d", longestConcatenatedString(a, len));
return 0;
}
输出
Count of strings to be concatenated with a character having frequency greater than sum of others: 2
结论
同样地,我们可以计算要与频率大于其他字符之和的字符进行串联的字符串的数量。
解决了获取程序来计算要与频率大于其他字符之和的字符进行串联的字符串的数量的挑战。
本文提供了C++编程代码以及确定要与频率大于其他字符之和的字符进行串联的字符串数量的算法。