AOJ - Problem 0029 : English Sentence
AOJ - Problem 0028 : の単語バージョンです。出現回数の最も多い単語を出力します。
C++なら
#include <iostream> #include <string> #include <map> #include <vector> using namespace std; int main(){ map<string, int> word; vector<string> w; string str; int max=0,length=0; while(cin >> str){ w.push_back(str); //入力した単語がすでにあるか探す if(word.find(str) != word.end() ){ word[str]++; }else{ word.insert( pair<string, int>(str,1)); } } map<string,int>::iterator p; for(p = word.begin() ; p != word.end() ; p++){ if( max < p->second) max = p->second; } for(p = word.begin() ; p != word.end() ; p++){ if( max == p->second){ cout << p->first << " "; break; } } for(int i=0 ; i<w.size() ; i++){ if( length < w[i].size() ) length = w[i].size(); } for(int i=0 ; i<w.size() ; i++){ if( length==w[i].size() ){ cout << w[i] << endl; break; } } }