![](/img/trans.png)
[英]how can i concatenate determined number of characters with another string?
[英]How can I find the number of unique characters in a string?
我沒有發現任何特別適合這個目的。
我試圖找出一個函數來計算字符串中每個字符的出現次數,以便我可以在最后從長度中取出它們以找出該字符串中使用了多少同質字符。
我試過嵌套循環,第一個應用,第二個掃描字符串,如果它沒有出現在字符串的其他地方,則有條件地滿足該字符:
size_t CountUniqueCharacters(char *str)
{
int i,j;
char unique[CHAR_MAX];
for(i=strlen(str); i>=0; i--)
{
for(j=strlen(str); j>=0; j--)
{
if(str[i] != unique[j])
unique[j] = str[i];
}
}
return strlen(unique);
}
這並不奏效。
如果您願意限制某人鍵入諸如"aaaaaaaaaaaaa"
類的懶惰名稱,這將很有用。
這是一個簡單的 C++ 解決方案。 該方法的復雜度為 O(n):
int countDistinct(string s)
{
unordered_map<char, int> m;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
return m.size();
}
這個方法有O(n^2)
復雜度,但是很可能(雖然有點復雜)在O(n)
做到這一點。
int CountUniqueCharacters(char* str){
int count = 0;
for (int i = 0; i < strlen(str); i++){
bool appears = false;
for (int j = 0; j < i; j++){
if (str[j] == str[i]){
appears = true;
break;
}
}
if (!appears){
count++;
}
}
return count;
}
該方法迭代字符串中的所有字符——對於每個字符,它檢查該字符是否出現在任何前面的字符中。 如果不是,則該字符是唯一的,並且計數遞增。
好吧,您可以為此使用 HashSet 或 unordered_set,但它的最壞情況時間復雜度為 O(N)。 因此,最好使用 256 個內存位置的數組或arr[256]
。 這在 O(256)~ O(1) 時間內給出了所需的輸出
我發現以下計算不同字符的方法非常簡單,並且在O(n)
。 這里的邏輯是,只需遍歷字符數組,並為每個字符設置其計數1
,即使它重復,也只用1
覆蓋該值。 完成遍歷后,只需將所有字符出現次數相加即可。
int count_distinc_char(const char *a){
int c_arr[MAX_CHAR] = {0};
int i, count = 0;
for( i = 0; a[i] != '\0'; i++){
c_arr[a[i] - 'a'] = 1;
}
for( i = 0; i < MAX_CHAR; i++){
count += c_arr[i];
}
return count;
}
創建一個鏈表來存儲在字符串中找到的字符及其出現的節點結構,如下所示,
struct tagCharOccurence
{
char ch;
unsigned int iCount;
};
現在一一讀取字符串中的所有字符,並在讀取一個字符時檢查它是否存在於鏈表中,如果是,則增加其計數,如果在鏈表中找不到字符,則插入一個帶有 'ch 的新節點' 設置為讀取字符並將計數初始化為 1。
通過這種方式,您將僅在單次傳遞中獲得每個字符的出現次數。 您現在可以使用鏈表打印遇到的字符次數。
我剛剛在 Stack Overflow 上尋找其他一些東西時遇到了這個問題。 但我仍然發布了一個可能對某些人有幫助的解決方案:
這也用於實現 huffman conding here 。 在那里你需要知道每個字符的頻率,所以比你需要的多一點。
#include <climits>
const int UniqueSymbols = 1 << CHAR_BIT;
const char* SampleString = "this is an example for huffman encoding";
左移運算符將 lhs(即 1) CHAR_BIT
次向左移動,因此乘以 2^8(在大多數計算機上)為 256,因為 UTF-8 中有 256 個唯一符號
在你的main
你有
int main() {
// Build frequency table
int frequencies[UniqueSymbols] = {0};
const char* ptr = SampleString;
while (*ptr != '\0') {
++frequencies[*ptr++];
}
}
我發現它非常小而且很有幫助。 唯一的缺點是frequencies
的大小在這里是 256,唯一性只是檢查哪個值是 1。
這是計算唯一字數的 C 程序的源代碼。 C程序在Linux系統上編譯成功並運行
int i = 0, e, j, d, k, space = 0;
char a[50], b[15][20], c[15][20];
printf("Read a string:\n");
fflush(stdin);
scanf("%[^\n]s", a);
for (i = 0;a[i] != '\0';i++) //loop to count no of words
{
if (a[i] = = ' ')
space++;
}
i = 0;
for (j = 0;j<(space + 1);i++, j++) //loop to store each word into an 2D array
{
k = 0;
while (a[i] != '\0')
{
if (a[i] == ' ')
{
break;
}
else
{
b[j][k++] = a[i];
i++;
}
}
b[j][k] = '\0';
}
i = 0;
strcpy(c[i], b[i]);
for (e = 1;e <= j;e++) //loop to check whether the string is already present in the 2D array or not
{
for (d = 0;d <= i;d++)
{
if (strcmp(c[i], b[e]) == 0)
break;
else
{
i++;
strcpy(c[i], b[e]);
break;
}
}
}
printf("\nNumber of unique words in %s are:%d", a, i);
return 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.