Language: C++

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
typedef pair<int, string> PIS;
typedef vector<PIS> VP;
typedef VP::iterator VPI;

void bsort(VP word){
	VPI p1, p2;
	for (p1 = word.begin(); p1 != word.end(); p1++){
		for (p2 = word.begin(); p2 != word.end() - 1; p2++){
			if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
			else if (p2->first == (p2 + 1)->first && p2->second > (p2 + 1)->second) swap(p2, p2 + 1);
		}
	}
	return;
}

int main()
{
	while (true){
		int n;
		cin >> n;
		if (!n) break;
		VP word;
		VPI p;
		string s = "";
		char sample;
		cin.ignore();
		for (int i = 0; i < n; i++){
			string in;
			getline(cin, in);
			in += ' ';
			s += in;
		}
		cin >> sample;
		string hoge = "";
		for (int i = 0; i < s.size(); i++){
			if (s[i] == ' '){
				if (hoge[0] == sample) word.push_back(make_pair(1, hoge));
				hoge = "";
			}
			else hoge += s[i];
		}
		if (word.empty()){
			cout << "NA" << endl;
			continue;
		}

		sort(word.begin(), word.end());
		
		for (p = word.begin(); p != word.end() - 1; p++){
			if (*p == *(p + 1)){
				p->first++;
				word.erase(p + 1);
			}
		}

		bsort(word);

		p = word.begin();
		for (int i = 0; p != word.end() && i < 5; i++, p++){
			cout << p->second;
			if (p == word.end() - 1 || i == 4) cout << endl;
			else cout << " ";
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'void bsort(VP)':
a.cc:15:62: error: no matching function for call to 'swap(VPI&, __gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >)'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/move.h:226:5: note: candidate: 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = __gnu_cxx::__normal_iterator<pair<int, __cxx11::basic_string<char> >*, vector<pair<int, __cxx11::basic_string<char> > > >; _Require<__not_<__is_tuple_like<_Tp> >, is_move_constructible<_Tp>, is_move_assignable<_Tp> > = void]' (near match)
  226 |     swap(_Tp& __a, _Tp& __b)
      |     ^~~~
/usr/include/c++/14/bits/move.h:226:5: note:   conversion of argument 2 would be ill-formed:
a.cc:15:70: error: cannot bind non-const lvalue reference of type '__gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >&' to an rvalue of type '__gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                                   ~~~^~~
/usr/include/c++/14/bits/exception_ptr.h:229:5: note: candidate: 'void std::__exception_ptr::swap(exception_ptr&, exception_ptr&)'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |     ^~~~
/usr/include/c++/14/bits/exception_ptr.h:229:25: note:   no known conversion for argument 1 from 'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} to 'std::__exception_ptr::exception_ptr&'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |          ~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/move.h:250:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> std::__enable_if_t<((bool)std::__is_swappable<_Tp>::value)> std::swap(_Tp (&)[_Nm], _Tp (&)[_Nm])'
  250 |     swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
      |     ^~~~
/usr/include/c++/14/bits/move.h:250:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   mismatched types '_Tp [_Nm]' and 'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'}
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44:
/usr/include/c++/14/bits/stl_pair.h:1089:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)'
 1089 |     swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1089:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::pair<_T1, _T2>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note: candidate: 'template<class _T1, class _T2> typename std::enable_if<(! std::__and_<std::__is_swappable<_T1>, std::__is_swappable<_T2> >::value)>::type std::swap(pair<_T1, _T2>&, pair<_T1, _T2>&)' (deleted)
 1106 |     swap(pair<_T1, _T2>&, pair<_T1, _T2>&) = delete;
      |     ^~~~
/usr/include/c++/14/bits/stl_pair.h:1106:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::pair<_T1, _T2>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:4039:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> void std::swap(__cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 4039 |     swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~
/usr/include/c++/14/bits/basic_string.h:4039:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68:
/usr/include/c++/14/tuple:2805:5: note: candidate: 'template<class ... _Elements> typename std::enable_if<std::__and_<std::__is_swappable<_Elements>...>::value>::type std::swap(tuple<_UTypes ...>&, tuple<_UTypes ...>&)'
 2805 |     swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
      |     ^~~~
/usr/include/c++/14/tuple:2805:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::tuple<_UTypes ...>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
/usr/include/c++/14/tuple:2823:5: note: candidate: 'template<class ... _Elements> typename std::enable_if<(! std::__and_<std::__is_swappable<_Elements>...>::value)>::type std::swap(tuple<_UTypes ...>&, tuple<_UTypes ...>&)' (deleted)
 2823 |     swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
      |     ^~~~
/usr/include/c++/14/tuple:2823:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::tuple<_UTypes ...>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_vector.h:2122:5: note: candidate: 'template<class _Tp, class _Alloc> void std::swap(vector<_Tp, _Alloc>&, vector<_Tp, _Alloc>&)'
 2122 |     swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
      |     ^~~~
/usr/include/c++/14/bits/stl_vector.h:2122:5: note:   template argument deduction/substitution failed:
a.cc:15:62: note:   'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} is not derived from 'std::vector<_Tp, _Alloc>'
   15 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
a.cc:16:101: error: no matching function for call to 'swap(VPI&, __gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >)'
   16 |                         else if (p2->first == (p2 + 1)->first && p2->second > (p2 + 1)->second) swap(p2, p2 + 1);
      |                                                                                                 ~~~~^~~~~~~~~~~~
/usr/include/c++/14/bits/move.h:226:5: note: candidate: 'std::_Require<std::__not_<std::__is_tuple_like<_Tp> >, std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp> > std::swap(_Tp&, _Tp&) [with _Tp = __gnu_cxx::__normal_iterator<pair<int, __cxx11::basic_string<char> >*, vector<pair<int, __cxx11::basic_string<char> > > >; _Require<__not_<__is_tuple_like<_Tp> >, is_move_constructible<_Tp>, is_move_assignable<_Tp> > = void]' (near match)
  226 |     swap(_Tp& __a, _Tp& __b)
      |     ^~~~
/usr/include/c++/14/bits/move.h:226:5: note:   conversion of argument 2 would be ill-formed:
a.cc:16:109: error: cannot bind non-const lvalue reference of type '__gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >&' to an rvalue of type '__gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >'
   16 |                         else if (p2->first == (p2 + 1)->first && p2->second > (p2 + 1)->second) swap(p2, p2 + 1);
      |                                                                                                          ~~~^~~
/usr/include/c++/14/bits/exception_ptr.h:229:5: note: candidate: 'void std::__exception_ptr::swap(exception_ptr&, exception_ptr&)'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |     ^~~~
/usr/include/c++/14/bits/exception_ptr.h:229:25: note:   no known conversion for argument 1 from 'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} to 'std::__exception_ptr::exception_ptr&'
  229 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |          ~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/