AOJ 0242 Input Candidates

問題文

Input Candidates

感想

とても悪名高い予選問題の1つ。AOJのテストケースは知らないが本番では改行コードがCR+LFだったので見えない制御文字\rに苦しめられた人々がたくさんいた。

解法

日頃からちゃんと\r消す習慣つけとくと嬉しい。

stringとかmapとかgetlineとかstringstream(重要)とかsort使えるとすごくやるだけに感じれて、もし使えなかったら割と大変です。C++便利。


ソース

ふつー。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <sstream>
#include <algorithm>
using namespace std;
 
int main(){
    int n;
    while(cin >> n && n){
        string l;
        getline(cin,l);
        map<string,int> W;
        vector< pair<int,string> > S;
        for(int i = 0 ; i < n ; i++){
            getline(cin,l);
            if( l[l.size()-1] == '\r' ){
                l = l.substr(0,l.size()-1);
            }
             
            stringstream ss(l);
            while(ss >> l){
                W[l]--;
            }
        }
        for( map<string,int> :: iterator it = W.begin() ; it != W.end() ; it++){
            S.push_back(make_pair(it->second,it->first));
        }
        char b;
        cin >> b;
        sort(S.begin(),S.end());
        int k = 0;
        for(int i = 0 ; i < S.size() ; i++){
            if( S[i].second[0] == b && k < 5){
                cout << (k++?" ":"") << S[i].second;
            }
        }
        if( !k ) cout << "NA" << endl;
        else cout << endl;
    }
     
}