HDOJ 1004--Let the Balloon Rise

题目的意思就是要统计一系列字符串中某一个字符串出现的最多次数。

刚开始的时候,想到使用map来处理map<string,int> 字符串,出现的次数,读入一个就在其中查找如果存在 int  +1;

最后找到最大的int对应的string 就ok了……

本人技拙,不知道怎么处理,望大神们赐教……

还是说说,AC的做法:

使用vector<string>来处理

读入全部的元素之后,对所有的元素排序(方便查找相同的元素),然后在判断每个元素的次数,找出次数最大的对应的string,OK!

#include<iostream>

#include<string>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

    string str;

    int n;

    vector<string> svec;

    while(cin>>n && n != 0)

    {

        int maxn = 1;

		string maxstr;

        vector<string>::size_type index = 0;

        while(n--)

        {

            cin>>str;

            svec.push_back(str);

        }

        sort(svec.begin(),svec.end());



        maxstr = svec[index];

        str = svec[index];



        int c = 0;

        while(index < svec.size())

        {

            while( index < svec.size() && str == svec[index])

            {

                c++;

                index++;

            }

            if(c>maxn)

            {

                maxn = c;

                c =0;

                maxstr = svec[index-1];

            }

            if(index == svec.size())

                break;

            else

                str = svec[index];

			c=0;

        }

        cout<<maxstr<<endl;

        svec.clear();



    }



    return 0;

}

 

你可能感兴趣的:(OO)