3

我有字符串说“walk talk, can't won't won't woN'T talk.” 我想计算重复的单词并显示。注意:它不区分大小写。

我用过分隔符

strtok(string, ",.;:\"!? -_\n\t*()@#=+");

并将其保存在

char *temp[100];

现在我如何检查单词的重复?并显示如下

3 won't
2 talk
1 can't
1 walk

它应该从最高重复显示到最低。如果重复相同,则显示字母顺序。

对不起,我的英语不好。

4

4 回答 4

3

使用 std::string 来保存strtok(). 然后创建一个std::map<string, int>来保存字符串(键)发生的次数。

您可以使用以下内容填充地图:

std::map<string, int> myMap;
myMap[tokenizedWord]++; //Increase count of word.

然后,您可以循环浏览地图内容并打印出整数值大于 2 的任何位置。

for (std::map<string, int>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    if (iter->second > 1)
        std::cout << "Duplicated word: " << iter->first << " count = " << iter->second;
}

我会让你弄清楚如何按顺序遍历它。您可以将值放在向量或其他东西中,并std::sort在打印或其他任何您喜欢的东西之前使用。不幸的是,地图是关联容器,您无法对它们进行排序,因为它破坏了它们的内部排序。

std::map 上的背景信息

映射是一个关联数组,这意味着每个键都映射到一个特定的值,并且键是唯一的。您实际上可以创建一个键不唯一的多重映射,这就是为什么这很重要。

基本上,由于键是唯一的,因此您只需将键用作数组索引即可访问或创建元素。

例如:

//Create a map and insert a couple things into it - prices of meat?
std::map<string, float> myMap;
myMap["Chicken"] = 4.99;
myMap["Turkey"] = 6.99;

//Retrieve the price of something using the key.
std::cout << "Chicken costs " << myMap["Chicken"] << std::end;

你也可以在地图上进行标准的插入和定位操作,但是关联数组的语法更简单,为什么还要麻烦呢?:)

PS:为了完全回答您的评论,以防万一,myMap[tokenizedWord]++ 末尾的 ++ 只是说将存储的该键的整数值的值增加 1。您也可以这样做 myMap [tokenizedWord] = myMap[tokenizedWord] + 1 或者你也可以做 myMap[tokenizedWord] += 1。

于 2012-08-06T18:48:31.863 回答
1

您的问题的完整实现(如果您需要用于排序的示例代码,请告诉我):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define ARRAY_ELEMS_COUNT(A)    sizeof(A)/sizeof(*A)

typedef struct _word_t
{
        char    *word;
        int     occurr_count;
        struct _word_t  *next;
} word_t;

typedef struct _word_list_t
{
        struct  _word_t *head;
        struct  _word_t *tail;
        int     elems_count;
} word_list_t;

/* Creation of the words list */
word_list_t *make_list(void)
{
        word_list_t *w_list = (word_list_t *)malloc(sizeof (struct _word_list_t));
        if (w_list == NULL)
        {
                fprintf(stderr, "malloc faild --> %s\n", strerror(errno));

                return NULL;
        }
        w_list->head = w_list->tail = NULL;
        w_list->elems_count = 0;

        return w_list;
}

int list_word_lookup(word_list_t *w_list, char *word)
{
        word_t *temp_word = w_list->head;
        while(temp_word)
        {
                if (strcmp(temp_word->word, word) == 0)
                {
                        /* We got it before, increment the count */
                        temp_word->occurr_count++;

                        return 1;
                }
                else
                {
                        temp_word = temp_word->next;
                }
        }

        return 0;
}

/* Adding new words to the list of words if they are not present, otherwise increment their occurrence count */
/* TODO : Sort the list using Merge sort for performance */
int adding_to_list(word_list_t *w_list, char *word)
{
        int     return_status = 0;
        char    *tmp_word = (char *)malloc(sizeof(char)*(strlen(word) + 1));
        word_t  *new_word = (word_t *)malloc(sizeof(struct _word_t));
        /* Empty list */
        if (w_list->head == NULL)
        {
                strcpy(tmp_word, word);
                new_word->word = tmp_word;
                new_word->occurr_count = 1;
                w_list->head = w_list->tail = new_word;
                w_list->head->next = NULL;
                w_list->elems_count++;
        }
        else
        {
                /* The list is not empty */
                /* Checking if the word exist in the list */
                return_status = list_word_lookup(w_list, word);
                if (return_status == 1)
                {
                        fprintf(stdout, "WE got this word before --> increment count\n");
                }
                else
                {
                        strcpy(tmp_word, word);
                        new_word->word = tmp_word;
                        new_word->occurr_count = 1;
                        w_list->tail->next = new_word;
                        w_list->tail = new_word;
                        w_list->tail->next = NULL;
                }
        }

        return 0;
}

void words_list_dump(word_list_t *w_list)
{
        word_t *temp;

        for (temp = w_list->head; temp; temp = temp->next) {
                fprintf(stdout, "Word : %s -- Count = %d\n", temp->word, temp->occurr_count);
        }
}

/* Destroying all words */
void free_words(word_list_t *w_list)
{
        word_t *temp;

        for (temp = w_list->head; temp; temp = temp->next) {
                /* Freeing the word string */
                free(temp->word);
                /* Freeing the word */
                free(temp);
        }
        w_list->head = NULL;
        w_list->tail = NULL;
}

/* Destroying the words list */
void free_words_list(word_list_t *w_list)
{
        if (!w_list)
        {
                return;
        }
        free_words(w_list);
        free(w_list);
}

/* TODO : create a function that converts your input text to a char ** array, so you can pass it to adding_to_list */
/* For testing */
int main(int argc, char **argv)
{
        const char *string[] = {"Hello", "World", "Stackoverflow", "C", "Hello", "C", "WORDS", "words", "List", "list", "Hello", "World", "Count"};
        word_list_t *my_list = make_list();
        int i;

        for (i = 0; i < ARRAY_ELEMS_COUNT(string); i++)
                adding_to_list(my_list, string[i]);
        words_list_dump(my_list);
        free_words_list(my_list);

        return 0;
}
于 2012-08-06T21:00:53.277 回答
0

这是使用strtok但不使用的答案std::map。在一次字符串中,每个单词都会与之前的单词进行检查并计算重复次数。

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <vector>
using std::vector;

#include <cstring>

using std::tolower;

int main()
{
    char *strin;
    string inputstr;
    vector<string> svec;
    vector<int> cvec;
    char *pch;
    int unique_word_count=0;
    while(getline(cin,inputstr))
    {
        //token-ize the string
        //First string
        strin = &inputstr[0];
        pch = std::strtok(strin," ,-");
        bool unique_word_found = true;
        //subsequent words
        while (pch != NULL)
        {
            string word(pch);
            for(string::size_type i=0; i < word.size(); i++)
                word[i]=tolower(word[i]);
            //first word
            //just add to svec and no comparisons
            if(unique_word_count==0)
            {
                svec.push_back(word);
                cvec.push_back(1);
                cvec[unique_word_count++]=1; //init count of first word
                //next word
                pch = std::strtok(NULL, " ,-");
                unique_word_found = true; //reset flag
                continue;
            }

            //start comparing with other words currently in string vector
            //do not do this if only 1 word present
            vector<string>::iterator iter=svec.begin();
            while(iter < svec.end())
            {
                if(word == *iter)
                {
                    //match found
                    cvec[iter-svec.begin()]++; //increment count of that word
                    unique_word_found = false;
                }
                iter++;
            }
            if(unique_word_found)
            {
                //add to unique word list and increment count
                svec.push_back(word);
                cvec.push_back(1);
                cvec[unique_word_count++]=1;
            }

            //next word
            pch = std::strtok(NULL, " ,-");
            unique_word_found = true; //reset flag
        }
    }

    cout << "Word" << " ---> " << "Occurences" << endl;
    for(vector<string>::size_type i=0; i < svec.size(); i++)
    {
        cout << svec[i] << "  --->  " << cvec[i] << endl;
    }
    return 0;
}
于 2016-08-11T13:44:01.207 回答
0

一般策略可以如下:

  • 清理输入(将所有字符转换为小写,删除不需要的标点符号等)
  • 遍历输入
  • 将每个字符添加到字符串中,在遇到空格时完成
  • 将字符串添加到键值结构中。字符串是关键。如果这是一个尚未包含在结构中的新条目,则将值设置为 1。否则将其设置为当前值 + 1(以便计算到目前为止遇到的次数)。
  • 对每个单词重复
  • 遍历键值结构并打印每个条目。
于 2016-08-11T14:12:45.650 回答