基本上,我想检查一个字符串在数组中出现了多少次。
我正在做在线挑战,我遇到了这个。
首先,输入数组有多少个元素。然后你输入一些字符串。
例子:
5
LOL
CODE
CODE
LOL
CODE
所以,我必须输出输入次数最多的字符串。在这种情况下,将是CODE
。
我怎么能用 C++ 做到这一点?
我不确定使用哈希映射或类似方法的方法,但我编写了一个程序,它本质上是蛮力的做事方式。基本上,您需要使用动态数组跟踪每个字符串以及它出现的类型。一旦您完成了输入并分析了每个字符串以及它出现了多少次,您就可以查看动态数组并查看哪个字符串出现最多。然后你只需输出它。
在没有我的程序帮助的情况下尝试自己做。如果你不能或者如果你卡住了,请参考下面的工作程序,它可以满足你的要求:
#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;
}