如何知道数组中字符串的出现次数?

时间:2013-02-24 19:00:38

标签: c++ arrays string

基本上,我想检查数组中出现一个字符串的次数。

我正在进行在线挑战,我遇到了这个问题。

首先,输入数组所包含的元素数。然后你输入一些字符串。

示例:

5
LOL
CODE
CODE
LOL
CODE

所以,我必须输出大多数时候输入的字符串。在这种情况下,CODE

我怎样才能用C ++做到这一点?

2 个答案:

答案 0 :(得分:1)

我不确定哈希映射等方法,但是我制定了一个基本上是蛮力做事方式的程序。基本上,您需要跟踪每个字符串以及使用动态数组显示的类型数。完成输入并分析每个字符串及其显示次数后,您将浏览动态数组并查看最多出现的字符串。然后你只需输出它。

在没有我的程序帮助的情况下尝试自己动手。如果您不能或者如果您遇到困难,请参考下面的工作程序,该程序可以满足您的要求:

#include <vector>
#include <string>
#include <iostream>

using namespace std;

//This struct represents a string and how many times it appears
struct strRefCount { //String and Reference Count
    unsigned int count;
    string str;
};

strRefCount strMode(string data) //String mode: returns the string which appears most often
{

    vector<strRefCount> allStrings; //Count of each time a string appears and what the string is
    string curString = ""; //The string we are currently reading (initialize to be empty)
    unsigned int strPos = 0; //The position (in the form of data[strPos]) which represents how far we have gotten in analyzing the string
    strRefCount *modeStringp; //Pointer to the string that appears most often

    while(data[strPos] != NULL) { //We will advance through data until we hit the null terminator in this loop
        curString.clear();
        while(data[strPos] != ' ' && data[strPos] != NULL) //Advance in the string until we hit a space or terminating null byte
        {
            curString += data[strPos]; //Append the string
            strPos++; //Advance one byte in data
        }

        bool flagStringFound = false; //This flag indicates that the string was already found before
        for(unsigned int i = 0; i < allStrings.size(); i++)
        {
            if(allStrings[i].str == curString) //If this string is the same as the current entry
            {
                allStrings[i].count++;
                flagStringFound = true;
                break;
            }
        }

        if(flagStringFound == false) //If the string is not present in allStrings, put it there and initialize it
        {
            strRefCount addElem; //Element to add to the end of the vector
            addElem.str = curString; //Last element's string is curString
            addElem.count = 1; //Last element's reference count is curString
            allStrings.push_back(addElem); //Add the element
        }

        //Make sure we don't repeat the loop if we are at the end of the string
        if(data[strPos] != NULL)
        {
            break;
        }
    }

    //Now we have every string which appears in data and the number of times it appears
    //Go on to produce the correct output
    modeStringp = &(allStrings[0]); //Set modeStringp to the first string
    for(unsigned int i = 1; i < allStrings.size(); i++) //Note that by setting i to 1 we skip the first element which is already in modeStringp
    {   
        if(allStrings[i].count > modeStringp->count) //If the current entry in allStrings is bigger than 
        {
            modeStringp = &(allStrings[i]); //Replace modeStringp with the current entry in allStrings
        }
    }

    return *modeStringp;
}

int main()
{
    string data;
    getline(cin, data); //Get the input (can't use cin as it doesn't allow for an entire line just space seperated string)

    strRefCount dataModeString = strMode(data); //Call out strMode function

    cout << endl << dataModeString.str << " appears most often with a total of " << dataModeString.count << " appearances.";

    getchar(); //This line is only here to make sure we don't quit before we see the output.

    return 0;
}

答案 1 :(得分:1)

This program曾为我效劳过。

程序创建一个包含下一条记录的数组:

"Lucio", "John", "Lucio"

然后它将该信息发送给返回引用最多名称的函数。所以它返回Lucio