字元頻率大於其他字元頻率之和的字串連線計數
我們這裡的主要目標是確定可以連線的最大字串數量,以確保在名為 arr[] 的包含 M 個字串的陣列中,只有一個字母的頻率超過所有其他字元的總和。
在繼續之前,讓我們瞭解一些陣列和字串的基本概念。
陣列不過是一組資料型別相同的元素,儲存在連續的記憶體段中。
C 程式語言中的陣列具有固定大小,這意味著一旦指定了大小,就無法更改;您無法縮小或擴充套件它。
現在讓我們檢查一下什麼是字串。字串是一組字元,在 C 程式語言中以空字元“\0”結尾。來自 C 字串的字元儲存在字元陣列中。C 字串與字元陣列的區別在於,它以獨特的空字元結尾,而字元陣列則沒有。
問題陳述
實現一個程式來確定需要連線的字串數量,以使一個字元的頻率大於其他字元的頻率之和。
示例 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++ 程式設計程式碼以及確定需要連線的字串數量的演算法,以使一個字元的頻率大於其他字元的頻率之和。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP