Problem:
入力候補
携帯電話には、メールなどの文章を効率良く入力するために入力候補を表示する機能が搭載されています。これは、使われる頻度が高い単語を記録しておき、入力された文字を頭文字に持つ単語を入力候補として提示するものです。例えば、普段”computer”という単語を多く入力しているなら、”c”と入力するだけで候補として”computer”が提示されます。このような機能の基本部分を作成してみましょう。
文章、文字を入力とし、その文字を頭文字に持つ単語を出現数の順に出力するプログラムを作成してください。ただし、出現数が同じ単語が複数ある場合は、辞書式順序で出力します。出力する単語は最大5つまでとします。該当する単語が存在しない場合は”NA”と出力してください。
入力
複数のデータセットが与えられます。入力の終わりはゼロひとつで表されます。各データセットは以下の形式で与えられます。
n
line
1
line
2
:
line
n
k
1行目に文章の行数
n
(1 ≤
n
≤ 10) が与えられます。続く
n
行に文章が与えられます。文章は半角英小文字と半角空白からなり、１行の文字数は1文字以上1024文字以下とします。空白によって区切られる単語は1文字以上20文字以下です。
続く行に頭文字
k
が半角英字で与えられます。
データセットの数は 100 を超えません。
出力
データセットごとに、指定された文字を頭文字にもつ単語または”NA” を出力します。
入力例
1
ben likes bananas the best among many fruits because bananas are sweet and cheap
b
1
winners get to visit aizu and the university of aizu and make many friends as well
a
3
ask alex about
the answer for the
assignment on android apps
a
2
programming is both
a sport and an intellectual puzzle
c
0
出力例
bananas because ben best
aizu and as
about alex android answer apps
NA

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main(){
  for(;;){
    int n;
    cin >> n;
    if(n == 0) break;
    cin.ignore();
    map<string,int> cnt;
    vector<string> list(0);
    for(int i=0;i<n;i++){
      string S,s;
      getline(cin,S);
      stringstream ss(S);
      vector<string> v(0);
      while(getline(ss,s,' ')) v.push_back(s);
      for(int j=0;j<(int)v.size();j++){
        if(cnt[v[j]] == 0) list.push_back(v[j]);
        cnt[v[j]]++;
      }
    }
    char b;
    cin >> b;
    vector<pair<int,string> > judge(0);
    for(int i=0;i<(int)list.size();i++){
      if(list[i][0] != b) continue;
      judge.push_back(make_pair(-cnt[list[i]],list[i]));
    }
    if((int)judge.size() == 0) cout << "NA" << endl;
    else{
      sort(judge.begin(),judge.end());
      cout << judge[0].second;
      for(int i=1;i<min(5,(int)judge.size());i++) cout << " " << judge[i].second;
      cout << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3172
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <map>
#include <utility>
using namespace std;

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;

        string line;
        getline(cin, line);
        map<string, int> words;
        for(int i=0; i<n; i++){
            getline(cin, line);
            stringstream ss(line);
            string s;
            while(ss >> s){
                words[s]++;
            }
        }
        char c;
        cin >> c;
        vector<pair<int,string>> ans;
        for(auto p: words){
            if(p.first[0] == c){
                ans.emplace_back(-p.second, p.first);
            }
        }
        sort(ans.begin(), ans.end());
        if(ans.size() > 5u){
            ans.erase(ans.begin()+5, ans.end());
        }
        if(ans.empty()){
            cout << "NA" << endl;
        }else{
            for(int i=0; i<(int)ans.size()-1; i++){
                cout << ans[i].second << " ";
            }
            cout << ans.back().second << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3172
final_rank: 4

code_snippet: #include<bits//stdc++.h>
using namespace std;
int main(){
  string str;int n;
  while(cin >> n && n){
    map<string,int> data;
    vector<pair<int,string> > v;
    char c;bool flg = false;
    getchar();
    while(n--){
      getline(cin,str);
      stringstream ss(str);
      while(ss >> str)data[str]++;
    }
    cin >> c;
    for(map<string,int>::iterator i=data.begin(),l=data.end();i!=l;i++){
      if(c==i->first[0]){
	v.push_back(make_pair(-i->second,i->first));
      }
    }
    sort(v.begin(),v.end());
    if(v.empty()) cout << "NA" << endl;
    else {
      for(int i=0,l=v.size();i<l&&i<5;i++){
	if(flg)cout << " ";
	else flg = true;
	cout << v[i].second;
      }cout << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3180
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7; 
constexpr int mod2 = 998244353;
 
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
 
template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A,B>;

inline int updiv(int a,int b){ return (a + b - 1) / b; }
 
constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};


signed main(){
    /*
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    */
    cout.setf(ios_base::fixed);
    cout.precision(12);

    int n;
    while(true){
        char buff[100];

        fgets(buff, sizeof(buff), stdin);
        sscanf(buff, "%lld", &n);
        if(n == 0){
            break;
        }
        vector<string> s(n);
        rep(i,n){
            getline(cin, s[i]);
        }
        char c;
        fgets(buff, sizeof(buff), stdin);
        sscanf(buff, "%c", &c);

        umap<string, int> cnt;

        rep(i,n){
            string str;
            rep(j,s[i].size()){
                if(s[i][j] == ' '){
                    if(str.front() == c){
                        ++cnt[str];
                    }
                    str = "";
                }else{
                    str += s[i][j];
                }
            }
            if(str.front() == c){
                ++cnt[str];
            }
        }

        if(cnt.size() == 0){
            cout << "NA\n";
        }else{
            vector<pair<int, string> > res;
            for(auto&& e : cnt){
                res.emplace_back(-e.second, e.first);
            }
            sort(all(res));
            int siz = min(res.size(), size_t(5));
            rep(i,siz){
                cout << res[i].second << (i == siz - 1 ? '\n' : ' ');
            }
        }
    }

    return (0);
}
accuracy: 1
time_ms: 10
memory_kb: 3272
final_rank: 17

code_snippet: #include<stdio.h> 
#include<string.h>
#include<iostream>
#include<string>
#include <vector>
#include<algorithm>
#include <map>

using namespace std;

vector<string> Split(string str,char c)
{
	vector<string> ret;
	int e = str.find(c);
	while (e != str.npos)
	{
		ret.push_back(str.substr(0, e));
		str = str.substr(e + 1);
		e = str.find(c);
	}
	ret.push_back(str);
	return ret;
}

int main()
{
	int n;
	cin >> n;
	while (n)
	{
		string l;
		getline(cin, l);
		map<string, int> record;
		for (size_t i = 0; i < n; i++)
		{
			getline(cin, l);
			vector<string> words = Split(l, ' ');
			for (string w : words) record[w]++;
		}
		char k;
		cin >> k;
		vector <pair<string, int> > vec;
		for (auto p : record) if (p.first[0] == k) vec.push_back(p);
		if (vec.size() == 0) cout << "NA" << endl;
		else
		{
			auto sortRule = [](pair<string, int> p1, pair<string, int> p2) ->bool
			{
				return p1.second > p2.second || p1.second == p2.second && p1.first < p2.first;
			};
			sort(vec.begin(), vec.end(), sortRule);
			int m = vec.size();
			m = min(5,m);
			for (size_t i = 0; i < m - 1; i++)
				cout << vec[i].first << " ";
			cout << vec[m - 1].first << endl;
		}
		cin >> n;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3100
final_rank: 1

code_snippet: #include<bits/stdc++.h>

using namespace std;

string LineData;
vector<pair<string, int> > WordsInfo;
vector<pair<string, int> >::iterator it;


void solve() {
	string word;
	stringstream ss(LineData);
	
	while (ss>>word) {
		//cout << word << " ";
		for (it = WordsInfo.begin(); it != WordsInfo.end(); ++it) {
			if (it->first == word) {
				++it->second;
				break;
			}
		}
		if (it == WordsInfo.end()) {
			WordsInfo.push_back(make_pair(word, 1));
		}
	}
}

bool cmp(const pair<string, int>& w1, const pair<string, int>& w2) {
	if (w1.second == w2.second) {
		return w1.first < w2.first;
	}
	return w1.second > w2.second;
}

bool matchWords(char target) {
	
	for (it = WordsInfo.begin(); it != WordsInfo.end();) {
		if (it->first[0] != target) {
			it = WordsInfo.erase(it);
			continue;
		}
		++it;
	}

	if (WordsInfo.empty()) {
		return false;
	}
	sort(WordsInfo.begin(), WordsInfo.end(), cmp);
	return true;
}

void show() {
	int cnt;

	cnt = WordsInfo.size() > 5 ? 5 : WordsInfo.size();
	for (int i = 0; i < cnt; ++i) {
		if (i != 0) {
			cout << " ";
		}
		cout << WordsInfo[i].first;
	}
	cout << endl;
}

int main() {
	int n;
	char target;

	while (cin >> n, n) {
		getchar();
		WordsInfo.clear();
		for (int i = 0; i < n; ++i) {
			getline(cin, LineData);
			//cout << LineData << endl;
			solve();
		}
		cin >> target;
		//cout << "target:" << target << endl;
		if (matchWords(target)) {
			show();
		}
		else {
			cout << "NA" << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3228
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<string, int> P;

int main() {
  int n;
  while (cin >> n, n) {
    cin.ignore();
    vector<string> v;
    map<string, int> mp;
    while (n--) {
      string line;
      getline(cin, line);
      istringstream iss(line);
      string s;
      while (iss >> s) {
        v.push_back(s);
      }
    }
    char k;
    cin >> k;
    for (const auto& e : v) {
      if (e[0] == k) mp[e]++;
    }
    vector<P> u;
    for (const auto& e : mp) {
      u.push_back(e);
    }
    sort(u.begin(), u.end(), [](auto const& x, auto const& y) {
      if (x.second != y.second) return x.second > y.second;
      return x.first < y.first;
    });
    if (u.empty()) {
      cout << "NA\n";
    } else {
      for (int i = 0; i < u.size() && i < 5; i++) {
        cout << (i == 0 ? "" : " ") << u[i].first;
      }
      cout << "\n";
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<string, int> P;

bool cmp(const P& x, const P& y) {
  if (x.second != y.second) return x.second > y.second;
  return x.first < y.first;
}

int main() {
  int n;
  while (cin >> n, n) {
    cin.ignore();
    vector<string> v;
    map<string, int> mp;
    while (n--) {
      string line;
      getline(cin, line);
      istringstream iss(line);
      string s;
      while (iss >> s) {
        v.push_back(s);
      }
    }
    char k;
    cin >> k;
    for (const auto& e : v) {
      if (e[0] == k) mp[e]++;
    }
    vector<P> u;
    for (const auto& e : mp) {
      u.push_back({e.first, e.second});
    }
    sort(u.begin(), u.end(), cmp);
    if (u.empty()) {
      cout << "NA\n";
    } else {
      for (int i = 0; i < u.size() && i < 5; i++) {
        cout << (i == 0 ? "" : " ") << u[i].first;
      }
      cout << "\n";
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
#include <iomanip>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    while(cin >> n, n){
        cin.ignore();
        vector<map<string, int>> v(26);
        for (int i = 0; i < n; ++i) {
            string A; getline(cin, A);
            stringstream ss; ss << A;
            string s;
            while(ss >> s){
                v[s.front()-'a'][s]++;
            }
        }
        char c;
        cin >> c;
        if(v[c-'a'].empty()) puts("NA");
        else {
            vector<pair<int, string>> ans;
            for (auto &&i : v[c-'a']) {
                ans.emplace_back(-i.second, i.first);
            }
            sort(ans.begin(),ans.end());
            for (int i = 0; i < min<int>(ans.size(), 5); ++i) {
                if(i) putchar(' ');
                cout << ans[i].second;
            }
            puts("");
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
signed main() {
	while (1) {
		int n; cin >> n;
		if (n == 0)return 0;
		string s, st; vector<string>v; set<string>ss; map<string, int>m; vector<pair<int, string>>v2;
		for (int x = 0; x <= n; x++) {
			getline(cin, s);
			if (x == 0)continue;
			for0(i, s.size()) {
				if (s[i] == ' ') {
					v.push_back(st); st = "";
				}
				else st += s[i];
			}v.push_back(st); st = "";
		}
		char ch; cin >> ch;
		for (string i : v) {
			if (i[0] == ch) {
				if (ss.find(i) == ss.end()) {
					ss.insert(i);
					m[i] = 1;
				}
				else m[i]++;
			}
		}
		for (string i : ss) v2.push_back(make_pair(-m[i], i));
		sort(v2.begin(), v2.end());
		if (v2.size() == 0)cout << "NA";
		else for0(i, min(5, (int)v2.size())) { if (i)cout << ' '; cout << v2[i].second; }
		cout << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3192
final_rank: 9

code_snippet: #include<iostream>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
int main(){
    int n;
    while(cin >> n, n != 0){
            cin.ignore();
        vector<string> str(n);
        vector<string> w;
        rep(i,n){
            getline(cin, str[i]);
        }
        rep(i,n){
            string s;
            stringstream ss(str[i]);
            while(getline(ss, s, ' ')){
                w.emplace_back(s);
            }
        }

        map<string, int> m[26];
        for(auto i : w){
            m[i[0] - 'a'][i]++;
        }

        char k;
        cin >> k;

        vector<pair<int, string>> p;
        for(auto i : m[k - 'a']){
            p.emplace_back(-i.second, i.first);
        }

        if(p.empty()){
            cout << "NA" << endl;
        }else{
            sort(p.begin(), p.end());
            rep(i,min<int>(5, p.size())){
                if(i) cout << ' ';
                cout << p[i].second;
            }
            cout << endl;
        }




    }
}
accuracy: 1
time_ms: 10
memory_kb: 3240
final_rank: 16

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;

map<string, int> mp[32];
vector<pair<int, string> >arr;
string str;

void init(void);

int main()
{
    int n;
    char ch;

    while (cin >> n, n != 0) {
        init();
        
        for (int i = 0; i < n; i++) {
            ch = '\0';

            while (ch != '\n') {
                cin >> str;
                mp[str[0] - 'a'][str]++;

                ch = getchar();
            }

        }

        ch = getchar();

        if (mp[ch - 'a'].size() == 0) {
            cout << "NA\n";
            continue;
        }

        for (auto x : mp[ch - 'a']) {
            arr.push_back(pair<int, string>{-1 * x.second, x.first});
        }

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

        int f = 0;
        for (int i = 0; i < arr.size() && i < 5; i++) {
            if (f) cout << ' ';
            cout << arr[i].second;

            f = 1;
        }
        cout << '\n';
    }

    return 0;
}

void init(void)
{
    /*
    map<string, int> mp[32];
    vector<pair<int, string> >arr;
    string str;
    */

    for (int i = 0; i < 32; i++) {
        mp[i].clear();
    }

    arr.clear();

    return;
}
accuracy: 1
time_ms: 10
memory_kb: 3204
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
typedef vector<int> vi;
typedef long long ll;
int n;
struct Word {
    string word;
    int count;
    Word(){}
    Word(string _word,int _count){
        word=_word,count=_count;
    }
    bool operator < (const Word &a) const {
        if(a.count!=count)
        {
            return a.count<count;
        } else{
            return a.word>word;
        }
    }
};

int main(void)
{
    string s;
	string n;
    while(getline(cin,n) && n!="0") {
        vector<set<string> > appear(26);
        vector<vector<Word> > dict(26);
        for(int line=0;line<stoi(n);line++)
        {
            //if(line==0)cin.ignore();
            getline(cin,s);
            //cout << "OUT" << endl;
            //cout << s << endl;
            string nowword="";
            int len=s.size();
            //cout << s << endl;
            for(int i=0;i<len;i++) {
                if (s[i] != ' ') nowword += s[i];
                else if (s[i] == ' ') {
                    int index = int(nowword[0] - 'a');
                    auto it = appear[index].find(nowword);
                    if (it == appear[index].end()) {
                        dict[index].push_back(Word(nowword, 0));
                        appear[index].insert(nowword);
                    } else {
                        for (int j = 0; j < dict[index].size(); j++) {
                            if (dict[index][j].word == nowword)dict[index][j].count++;
                        }
                    }
                    //cout << nowword << endl;
                    nowword = "";
                }
                if (i == len - 1) {
                    int index = int(nowword[0] - 'a');
                    auto it = appear[index].find(nowword);
                    if (it == appear[index].end()) {
                        dict[index].push_back(Word(nowword, 0));
                        appear[index].insert(nowword);
                    } else {
                        for (int j = 0; j < dict[index].size(); j++) {
                            if (dict[index][j].word == nowword)dict[index][j].count++;
                        }
                    }
                    //cout << nowword << endl;
                    nowword = "";
                }
            }
        }
        string k;
    	getline(cin,k);
        int index=int(k[0]-'a');
        sort(dict[index].begin(),dict[index].end());
        int out=min(5,int(dict[index].size()));
        //cout << out << endl;
        if(out==0)
        {
            cout << "NA" << endl;
        } else {
            for (int i = 0; i < out; i++) {
                if (i != out - 1)
                {
                    cout << dict[index][i].word << ' ';
                }
                else
                {
                    cout << dict[index][i].word << endl;
                }
            }
        }
    }

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 6

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <tuple>

using namespace std;

class Solver {
};
int main() {
	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;


		char dum;
		cin >> dum;
		map<string, int> mp;
		for (int i = 0; i < n; i++) {
			string line;
			getline(cin, line);
			string ss = "";
			if (i == 0)ss += dum;
			for (int j = 0; j < line.size(); j++) {
				if (line[j] == ' ') {
					if (mp.find(ss) == mp.end()) {
						mp.emplace(ss, 0);
					}
					mp[ss]++;
					ss = "";
				}
				else {
					ss += line[j];
				}
			}
			if (mp.find(ss) == mp.end()) {
				mp.emplace(ss, 0);
			}
			mp[ss]++;
		}

		char c;
		cin >> c;
		vector<tuple<int, string>> strs;
		for (auto pair : mp) {
			if (pair.first[0] == c) {
				strs.emplace_back(make_tuple(-pair.second, pair.first));
			}
		}

		if (strs.size() == 0) {
			cout << "NA" << endl;
		}
		else {
			sort(strs.begin(), strs.end());
			int cnt = 5;
			if (strs.size() < cnt)cnt = strs.size();
			for (int i = 0; i < cnt; i++) {
				cout << get<1>(strs[i]);
				if (i+1 < cnt)cout << " ";
			}
			cout << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 12

code_snippet: #include <bits/stdc++.h>

#define ALL(a) (a).begin(), (a).end()
#define llong long long

using namespace std;

signed main(){
	while(true){
		int n; cin >> n;
		if(n == 0)break;
		vector<string> strs(n);
		getline(cin, strs[0]);
		for(int i = 0; i < n; i++){
			getline(cin, strs[i]);
		}
		string c;
		cin >> c;
		stringstream ss;
		for(int i = 0; i < n; i++){
			ss << strs[i];
			ss << " ";
		}
		map<string, int> st;
		string tmp;
		while(ss >> tmp){
			if(st.find(tmp) == st.end()) st[tmp] = 0;
			st[tmp]++;
		}
		vector<pair<string, int>> ans;
		for(auto e : st){
			if(e.first[0] == c[0])ans.emplace_back(e);
		}
		if(ans.size() == 0){
			cout << "NA" << endl;
			continue;
		}
		sort(ALL(ans), [](const pair<string, int> a, const pair<string, int> b){
				return a.second != b.second ?  a.second > b.second : a.first < b.first ;
				});
		for(size_t i = 0; i < ans.size(); i++){
			if(i == 5)break;
			if(i != 0)cout << " ";
			cout << ans[i].first;
		}
		cout << endl;
	}


	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3220
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define pp pair<int,int>
#define ll long long
#define ld long double
int inf=100000000;
ll INF=5000000000000000000;
ll MOD=1000000007;


int main() {
   
    int n;
    cin >> n;
     while (true){
         int m=0;
         string s,t;
         vector<string> a(0);
         while (true){
         
         cin >> s;
         if (s=="0" || s=="1" || s=="2" || s=="3" || s=="4" || s=="5" || s=="6" || s=="7" || s=="8" || s=="9" || s=="10") break;
         if (m==1) a.push_back(t);
         m=1;
         t=s;
         }
         char f=t.at(0);
         sort (a.begin(),a.end());
         vector<pair<int,string>> v(0);
         pair<int,string> o;
         for (int i=0;i<a.size();i++){
             if (i!=0) {
             if (a.at(i).at(0)==f && a.at(i)!=a.at(i-1)) {o.first=1;o.second=a.at(i);v.push_back(o);}
             if (a.at(i).at(0)==f && a.at(i)==a.at(i-1)) v.at(v.size()-1).first--;}
             if (i==0 && a.at(i).at(0)==f) {o.first=1;o.second=a.at(i);v.push_back(o);}
         }
         sort(v.begin(),v.end());
         rep(i,5){
             if (i==v.size()) break;
             if (i==v.size()-1 || i==4) cout << v.at(i).second << endl;
             else cout << v.at(i).second << " ";
         }
         if (v.size()==0) cout << "NA" << endl;
         if (s=="0") return 0;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3256
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (((a)%mod) * ((b)%mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);


string sub(string str, int l, int r){
  string res;
  repi(i,l,r)res.pb(str[i]);
  return res;
}

struct st{string str;int n;};
bool cmp(const st &a, const st &b){
  if(a.n > b.n)return true;
  else if(a.n < b.n)return false;
  return a.str < b.str;
}

signed main(){
  while(true){
    string line[100];
    vector<string> str;
    int n;cin >> n;
    if(n == 0)break;
    cin.ignore();
    rep(i,n)getline(cin, line[i]);

    map<string, int> mp;
    rep(i,n){
      int idx = 0;
      rep(j,line[i].size()+1){
        if(line[i][j] == ' ' or j == line[i].size()){
          string s = sub(line[i], idx, j);
          mp[s]++;
          idx = j + 1;
        }
      }
    }
    char c;cin >> c;
    vector<st> vec;
    for(auto it = mp.begin(); it != mp.end(); it++) {
      if(it->fi[0] != c)continue;
      vec.push_back(st{it->fi, it->se});
    }
    sort(all(vec), cmp);
    int size = vec.size();
    size = min(size, (int)5);
    if(size == 0){cout << "NA" << endl;continue;}
    rep(i,size){
      cout << vec[i].str;
      if(i == size-1)cout << endl;
      else cout << " ";
    }
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3172
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
using namespace std;

struct Info
{
    string word;
    int cnt;
};

int main()
{
    int n;
    char c;

    while( cin >> n )
    {
        if( !n )
            break;

        string s;
        vector<string> v;

        getline( cin, s );
        for( int i = 0; i < n; i++ )
        {
            getline( cin, s );

            s += " ";
            string t = "";
            for( int j = 0; j < s.size(); j++ )
            {
                if( s[j] == ' ' )
                {
                    v.push_back( t );
                    t = "";
                    continue;
                }
                t += s[j];
            }
        }

        cin >> c;

        vector<Info> w;

        for( int i = 0; i < v.size(); i++ )
            if( c == v[i][0] )
            {
                Info x;
                x.word = v[i];
                x.cnt = 0;

                bool judge = true;
                for( int j = 0; j < w.size(); j++ )
                    if( w[j].word == v[i] )
                    {
                        w[j].cnt++;
                        judge = false;
                        break;
                    }

                if( judge )
                    w.push_back( x );
            }

        for( int i = 0; i < w.size(); i++ )
            for( int j = w.size() - 1; j > i; j-- )
                if( w[j].cnt > w[j-1].cnt )
                {
                    Info temp = w[j];
                    w[j] = w[j-1];
                    w[j-1] = temp;
                }

        for( int i = 0; i < w.size(); i++ )
            for( int j = w.size() - 1; j > i; j-- )
                if( w[j].cnt == w[j-1].cnt && w[j].word < w[j-1].word )
                {
                    Info temp = w[j];
                    w[j] = w[j-1];
                    w[j-1] = temp;
                }
        if( w.size() == 0 )
            cout << "NA" << endl;
        else
        {
            cout << w[0].word;
            for( int i = 1; i < 5 && i < w.size(); i++ )
                cout << " " << w[i].word;
            cout << endl;  
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3212
final_rank: 11

code_snippet: //入力候補

#include<iostream>
#include<utility>
#include<string>
#include<vector>
#include<algorithm>
#include<deque>
#include<queue>
#include<set>
#include<map>

using namespace std;
#define LL long long

int main() {
	while(1) {
		int n;
		char k;
		map<string, int> dict;//単語、単語の出現数
		vector<pair<int, string>> talf;//各頭文字の先頭
		string ans;

		cin >> n;
		if (!n) break;
		cin.ignore();
		for (int i = 0; i < n; i++) {
			string s;
			getline(cin, s);


			auto pbegin = s.begin();

			while (pbegin != s.end()) {
				auto pend = pbegin;
				while (pend != s.end() && *pend != ' ')//文字列の終わりか対象の文字までイテレータを動かす
					pend++;
				string word = string(pbegin, pend);
				if (dict.find(word) == dict.end()) dict[word] = 0;
				dict[word]++;
				if (pend != s.end())
					pend++;
				pbegin = pend;
			}
		}
		cin >> k;
		
		auto itr = dict.begin();
		while (itr != dict.end()) {
			string nword = itr->first;
			if (nword[0] == k) //頭文字の一致
				talf.push_back(make_pair(-(itr->second), nword));
			itr++;
		}
		
		if (talf.empty())ans = "NA";
		else {
			sort(talf.begin(), talf.end());
			for (int i = 0; i < talf.size(); i++) {
				if (i > 4)break;
				if(i) ans += " ";
				ans += talf[i].second;
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3220
final_rank: 13

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <array>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <utility>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <bitset>
#include <complex>
#include <random>
#include <functional>
#include <sstream>
#include <iomanip>

using namespace std;

#define rep(i, n) for(int i=0; i<(n); i++)
#define FOR(i, m, n) for(int i=(m);i<(n);i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define REVE(x) reverse((x).begin(),(x).end())
#define mp make_pair
#define pb push_back
#define dump(x)  cout << #x << " = " << (x) << endl;
#define forauto(i, a) for(auto i:a)cout << i << " "; puts("");

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef complex<double> CD;

//文字列分割
VS split(string str, char del) {
	VS re;
	string sub;
	for (char c : str) {
		if (c == del) {
			re.pb(sub);
			sub.clear();
		}
		else {
			sub += c;
		}
	}
	re.pb(sub);
	return re;
}

int main() {
	while (true) {
		int n; cin >> n;
		if (n == 0)break;
		n++;
		VS line(n); rep(i, n)getline(cin, line[i]);
		char k; cin >> k;

		VS a;
		rep(i, n) {
			VS tmp = split(line[i], ' ');
			a.insert(a.end(), all(tmp));
		}

		vector<pair<int, string>> ans;
		rep(i, sz(a)) {
			if (a[i][0] == k) {

				bool flag = false;
				rep(j, sz(ans)) {
					if (ans[j].second == a[i])ans[j].first++, flag = true;
				}
				if (!flag)ans.pb(mp(1, a[i]));

			}
		}
		rep(i, sz(ans))ans[i].first = 100 - ans[i].first;
		SORT(ans);

		if (ans.empty())puts("NA");
		else {
			rep(i, min(5, sz(ans))) {
				cout << ans[i].second;
				printf(i == min(5, sz(ans)) - 1 ? "\n" : " ");
			}
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 3324
final_rank: 18