Problem p00242 (C++) — buggy submission, verdict: Runtime Error

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

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;) {
      if (p->second == (p + 1)->second) {
        p->first++;
        word.erase(p + 1);
      } else
        p++;
    }

    // bsort
    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) ||
            (p2->first == (p2 + 1)->first && p2->second > (p2 + 1)->second)) {
          PIS hoge = *p2; // swap
          *p2 = *(p2 + 1);
          *(p2 + 1) = hoge;
        }
      }
    }

    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;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
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) ||
          (p2->first == (p2 + 1)->first && p2->second > (p2 + 1)->second)) {
        PIS hoge = *p2; // swap
        *p2 = *(p2 + 1);
        *p1 = hoge;
      }
    }
  }
  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;
}