3 abcd bbaa jsdhfjkshdfjksahdfjkhsajkf
a a j
3 abcd bbaa jsdhfjkshdfjksahdfjkhsajkf
a a j
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; struct WordCnt{ char ch; int cnt; WordCnt(int aCh, int aCnt):ch(aCh),cnt(aCnt){} bool operator <(const WordCnt &a) const{ if(cnt!=a.cnt) return cnt > a.cnt; else return ch < a.ch; } }; int main(){ int T; cin >> T; for (int icase = 0; icase < T; ++icase) { string str; cin >>str; vector<WordCnt> word; for (int i = 0; i < str.length(); ++ i) { int j = 0; for (j = 0; j < word.size(); ++ j) { if(word[j].ch == str[i]) word[j].cnt++; } if(j == word.size()) word.push_back(WordCnt(str[i],1)); } sort(word.begin(),word.end()); cout<<word[0].ch<<endl; } }
原文:http://www.cnblogs.com/xiongqiangcs/p/3642783.html