Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, string> PIS;
typedef vector<PIS> VP;
typedef VP::iterator VPI;


void swap(PIS *d1, PIS *d2)
{
	PIS hoge;
	hoge = d1;
	d1 = d2;
	d2 = hoge;
	return;
}

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 swap(PIS*, PIS*)':
a.cc:11:16: error: no match for 'operator=' (operand types are 'PIS' {aka 'std::pair<int, std::__cxx11::basic_string<char> >'} and 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'})
   11 |         hoge = d1;
      |                ^~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:946:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int; _T2 = std::__cxx11::basic_string<char>]'
  946 |         operator=(const pair<_U1, _U2>& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:946:9: note:   template argument deduction/substitution failed:
a.cc:11:16: note:   mismatched types 'const std::pair<_T1, _T2>' and 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'}
   11 |         hoge = d1;
      |                ^~
/usr/include/c++/14/bits/stl_pair.h:957:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int; _T2 = std::__cxx11::basic_string<char>]'
  957 |         operator=(pair<_U1, _U2>&& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:957:9: note:   template argument deduction/substitution failed:
a.cc:11:16: note:   mismatched types 'std::pair<_T1, _T2>' and 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'}
   11 |         hoge = d1;
      |                ^~
/usr/include/c++/14/bits/stl_pair.h:921:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int; _T2 = std::__cxx11::basic_string<char>; std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&> = const std::pair<int, std::__cxx11::basic_string<char> >&]'
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:923:65: note:   no known conversion for argument 1 from 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'} to 'std::__conditional_t<true, const std::pair<int, std::__cxx11::basic_string<char> >&, const std::__nonesuch&>' {aka 'const std::pair<int, std::__cxx11::basic_string<char> >&'}
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  922 |                                        is_copy_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  923 |                                 const pair&, const __nonesuch&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_pair.h:931:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&>) [with _T1 = int; _T2 = std::__cxx11::basic_string<char>; std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&> = std::pair<int, std::__cxx11::basic_string<char> >&&]'
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:933:55: note:   no known conversion for argument 1 from 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'} to 'std::__conditional_t<true, std::pair<int, std::__cxx11::basic_string<char> >&&, std::__nonesuch&&>' {aka 'std::pair<int, std::__cxx11::basic_string<char> >&&'}
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  932 |                                        is_move_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  933 |                                 pair&&, __nonesuch&&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc:13:14: error: cannot convert 'PIS' {aka 'std::pair<int, std::__cxx11::basic_string<char> >'} to 'PIS*' {aka 'std::pair<int, std::__cxx11::basic_string<char> >*'} in assignment
   13 |         d2 = hoge;
      |              ^~~~
      |              |
      |              PIS {aka std::pair<int, std::__cxx11::basic_string<char> >}
a.cc: In function 'void bsort(VP)':
a.cc:22: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> > > >)'
   22 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                          ~~~~^~~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:77:
/usr/include/c++/14/any:430:15: note: candidate: 'void std::swap(any&, any&)' (near match)
  430 |   inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); }
      |               ^~~~
/usr/include/c++/14/any:430:15: note:   conversion of argument 2 would be ill-formed:
a.cc:22:70: error: cannot bind non-const lvalue reference of type 'std::any&' to an rvalue of type 'std::any'
   22 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                                   ~~~^~~
/usr/include/c++/14/any:191:7: note:   after user-defined conversion: 'std::any::any(_Tp&&) [with _Tp = __gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >; _VTp = __gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > >; _Mgr = _Manager_internal<__gnu_cxx::__normal_iterator<std::pair<int, std::__cxx11::basic_string<char> >*, std::vector<std::pair<int, std::__cxx11::basic_string<char> > > > >; typename std::enable_if<(is_copy_constructible_v<_VTp> && (! __is_in_place_type_v<_VTp>)), bool>::type <anonymous> = true]'
  191 |       any(_Tp&& __value)
      |       ^~~
In file included from /usr/include/c++/14/bits/stl_pair.h:61:
/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:22: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> > > >'
   22 |                         if (p2->first < (p2 + 1)->first) swap(p2, p2 + 1);
      |                                                                   ~~~^~~
In file included from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/stdexcept:38,
                 from /usr/include/c++/14/system_error:43,
                 from /usr/include/c++/14/bits/ios_base.h:46,
                 from /usr/include/c++/14/streambuf:43,
                 from /usr/include/c++/14/bits/streambuf_iterator.h:35,
                 from /usr/include/c++/14/iterator:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:54:
/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)
      |          ~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/c++/14/future:51,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:176:
/usr/include/c++/14/bits/std_thread.h:328:3: note: candidate: 'void std::swap(thread&, thread&)'
  328 |   swap(thread& __x, thread& __y) noexcept
      |   ^~~~
/usr/include/c++/14/bits/std_thread.h:328:16: note:   no known conversion for argument 1 from 'VPI' {aka 'std::vector<std::pair<int, std::__cxx11::basic_string<char> > >::iterator'} to 'std::thread&'
  328 |   swap(thread& __x, thread& __y) noexcept
      |        ~~~~~~~~^~~
In file included from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/sstream:1204:5: note: candidate: 'template<class _CharT, class _Traits, class _Allocator> void std::__cxx11::swap(basic_stringbuf<_CharT, _Traits, _Alloc>&, basic_stringbuf<_CharT, _Traits, _Alloc>&)'
 1204 |     swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x,
      |     ^~~~
/us