-1

基本的に、配列内の文字列が何回出現するかを確認したい。

私はオンラインチャレンジをしていて、これに出くわしました。

まず、配列の要素数を入力します。次に、いくつかの文字列を入力します。

例:

5
LOL
CODE
CODE
LOL
CODE

そのため、最も多く入力された文字列を出力する必要があります。この場合、それは になりますCODE

どうすればC++でそれを行うことができますか?

4

2 に答える 2

1

このプログラムは私のために働いていました。

プログラムは、次のレコードを含む配列を作成します。

"Lucio", "John", "Lucio"

次に、その情報を、最も参照されている名前を返す関数に送信します。だからそれは戻りますLucio

于 2013-02-24T21:35:02.607 に答える
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;
}
于 2013-02-24T20:50:39.537 に答える