해시지도 등의 접근 방법에 대해서는 잘 모르겠지만 본질적으로 일을하는 무차별 한 방법 인 프로그램을 작성했습니다. 기본적으로 각 문자열과 동적 배열을 사용하여 나타나는 유형을 추적해야합니다. 입력을 완료하고 각 문자열과 횟수를 분석하면 동적 배열을 통해 어떤 문자열이 가장 많이 표시되는지 확인할 수 있습니다. 그런 다음 출력하십시오.
내 프로그램의 도움없이 직접 시도하고 수행하십시오. 문제가 발생할 경우 또는 당신이 물어 무엇을하는 아래의 작업 프로그램을 참조 할 수없는 경우 :
#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;
}
저는 현재 아이디어가 없습니다 .... 저는 전문 코더가 아닙니다. 저는 현재 고등학교에 다니고 있으며 미래의 대학을위한 C++ 지식을 확장하고 싶습니다. 따라서 온라인 문제와 eBook에서만 학습하고 있습니다. –
어쨌든 원하는 질문에 대한 답변이 아닙니다. 기본적으로, 나는 무엇을해야할지 모르기 때문에 아무 것도 시도하지 않았습니다. 나는 해시 테이블을 찾았지만, 내 마음 속에는 아무 것도 얻지 못했습니다. 그래서 어떤 종류의 예제 나 힌트를 할 수 있습니다. –
더 표현력이 있거나 표현력이 좋아야합니다. 문자열에 문자열이 몇 번 나타나는지 알고 싶습니까? 다음과 같은 함수 :'{A = "hi word!"; Count ('word', A);}'result : 1 – Lucio