5

为此,我没有发现任何特别之处。

我试图找出一个函数来计算字符串中每个字符的出现次数,以便我可以从长度的末尾将它们拉出来,以找出该字符串中使用了多少同质字符。

我试过嵌套循环,第一个应用,第二个扫描字符串,如果它没有出现在字符串的其他地方,则有条件地满足字符:

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".

4

9 回答 9

10

这是一个简单的 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(); 
} 
于 2020-04-15T15:57:04.453 回答
6

这种方法很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;
}

该方法遍历字符串中的所有字符 - 对于每个字符,它检查该字符是否出现在任何先前的字符中。如果不是,则该字符是唯一的,并且计数会增加。

于 2014-07-04T11:32:31.663 回答
3

好吧,您可以为此目的使用 HashSet 或 unordered_set,但它的最坏情况时间复杂度为 O(N)。因此,最好使用 256 个内存位置的数组或arr[256]. 这在 O(256)~ O(1) 时间内给出了所需的输出

于 2020-11-04T19:57:47.987 回答
3

我发现以下计算不同字符的方法非常简单,并且在O(n). 这里的逻辑是,只需遍历字符数组,并对每个字符进行计数1,即使它重复,也只需用1only 覆盖该值。完成遍历后,只需对所有字符的出现求和即可。

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;
}
于 2019-07-05T03:07:20.790 回答
1

创建一个链表来存储在字符串中找到的字符及其出现的节点结构如下,

struct tagCharOccurence 
{
    char ch;
    unsigned int iCount;
};

现在一一读取字符串中的所有字符,并在读取一个字符时检查它是否存在于您的链表中,如果是,则增加其计数,如果在链表中找不到字符,则插入一个带有 'ch 的新节点' 设置为读取字符并将计数初始化为一。

通过这种方式,您将仅在单遍中获得每个字符的出现次数。您现在可以使用链表打印字符的次数与遇到的次数一样多。

于 2014-07-04T11:40:15.937 回答
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。

于 2018-09-04T14:08:54.453 回答
1

如果您使用的是 C++,这里有一个具有最佳时间复杂度的单线:

int numUniqeChars = std::unordered_set<char>(std::begin(str), std::end(str)).size();

于 2021-09-27T03:15:17.613 回答
0

在该算法中,成本较低,速度较高。因为每次搜索都是在一个较小的字符串中完成的。它也不需要在字符串或“拆分”或“(列表或数组)”之间进行比较。

QString out = "";
QString str = "Does he not know that God sees?";
while (str.size() > 0) {
   out += str[0];
   str = str.replace(str[0],"",Qt::CaseInsensitive);
}
qDebug() << out << out.size();

输出: “hntkwaG 吗?” 13

忽略不区分大小写并在字母之间使用 ,。

QString out = "";
QString str = "Does he not know that God sees?";
while (str.size() > 0) {
   out += str[0];
   if(str.size() != 1)
       out += ',';
   str = str.replace(str[0],"");
}
qDebug() << out;

输出: “D,o,e,s, ,h,n,t,k,w,a,G,d,?”

要排序,请参阅此链接:Sort filenames natural with Qt

于 2021-09-29T07:58:42.730 回答
-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;
于 2020-04-15T15:59:56.427 回答