我们的主要目标是确定最多的字符串能够被连接起来,以确保只有一个字母的频率超过所有其他字符的总和,前提是有一个名为arr[]的包含M个字符串的数组。
在继续之前,让我们了解一些数组和字符串的基本概念。
数组就是一组相同数据类型的元素,存储在连续的内存区域中。
C编程语言中的数组具有固定的大小,这意味着一旦指定了大小,就无法更改;您无法缩小或扩展它。
让我们现在来研究一下什么是字符串。在C编程语言中,字符串是一组以空字符"0"结尾的字符。C字符串中的字符被保存在字符数组中。与字符数组不同,C字符串之所以与字符数组相矛盾,是因为它以特殊的空字符结尾。
问题陈述
实现一个程序,用于确定要与频率大于其他字符之和的字符连接的字符串的数量。
示例 1
'Let us take the input array
arr[]: {“xyz", “yyyyx", “q”}
'Output obtained is: 3
Explanation
的中文翻译为:解释
这里元素 "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
Explanation
的中文翻译为:解释
这里元素或字符“m”的频率为5。
元素 "n" 的频率为 5,元素 "l" 的频率为 6,最后字符 "o" 的频率为 1。
在这里,我们只能将两个字符串“lmllnl”连接起来。
字符l的频率为4。其他字符m和n的频率之和为2。为了依赖于拼接具有频率大于其他字符频率之和的字符的字符串,这是唯一可能的拼接。
方法
为了确定要与频率大于其他字符总和的字符连接的字符串的数量,我们采用以下方法。
解决这个问题的方法是通过迭代来获取要与频率大于其他字符频率之和的字符进行拼接的字符串的数量。
也就是说,我们通过迭代所有字符(即从"a"到"z")来确定所有字符串中每个字符的净频率。在这种情况下,净频率可以通过从中减去每个字符的其他频率来计算,因此如果总净频率大于0,则表示该元素的频率超过了所有其他频率的总和。
算法
下面给出了确定要与频率大于其他字符之和的字符连接的字符串计数的算法。
第一步 - 开始
第二步 − 定义函数以确定所有字符的频率,并减少字符串中其他频率的总和。
第三步 - 迭代字符串数组 a
第四步 - 定义一个整数变量来存储频率
第五步 - 将频率存储在数组 v 中
第六步 - 定义一个变量来存储最大计数
第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++编程代码以及算法,用于确定要与频率大于其他字符之和的字符连接的字符串的数量。