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

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, string> P;

int main(void) {
  char c;
  int n;

  while (cin >> n, n) {
    vector<P> data[30];

    while (--n, n >= 0) {
      cin >> c;
      char fc = c;

      string word;

      word += c;
      while (c = cin.get(), c != '\\n') {
        if (c == ' ') {
          bool flag = false;
          for (int i = 0; i < (signed)data[fc - 'a'].size(); i++) {
            if (data[fc - 'a'][i].second == word) {
              data[fc - 'a'][i].first--;
              flag = true;
              break;
            }
          }
          if (!flag) {
            data[fc - 'a'].push_back(P(3000, word));
          }

          // cout << word << endl;
          word.clear();
          fc = 0;
        } else {
          word += c;
          if (!fc)
            fc = c;
        }
      }
      {
        bool flag = false;
        for (int i = 0; i < (signed)data[fc - 'a'].size(); i++) {
          if (data[fc - 'a'][i].second == word) {
            data[fc - 'a'][i].first--;
            flag = true;
            break;
          }
        }
        if (!flag) {
          data[fc - 'a'].push_back(P(3000, word));
        }
      }
    }

    for (int i = 0; i < 26; i++) {
      if (data[i].size())
        sort(data[i].begin(), data[i].end());
    }

    char in;

    cin >> in;

    if (data[in - 'a'].size()) {
      cout << data[in - 'a'][0].second;
      for (int i = 1; i < (signed)data[in - 'a'].size(); i++) {
        if (i >= 5)
          break;
        cout << ' ' << data[in - 'a'][i].second;
      }
      cout << endl;
    } else
      cout << "NA" << endl;
    cin.get();
  }

  return 0;
}

// 1144

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, string> P;

int main(void) {
  char c;

  while (cin >> c, c != '0') {
    vector<P> data[30];
    char fc = c;

    string word;

    word += c;
    while (c = cin.get(), c != '\\n') {
      if (c == ' ') {
        bool flag = false;
        for (int i = 0; i < (signed)data[fc - 'a'].size(); i++) {
          if (data[fc - 'a'][i].second == word) {
            data[fc - 'a'][i].first--;
            flag = true;
            break;
          }
        }
        if (!flag) {
          data[fc - 'a'].push_back(P(3000, word));
        }

        // cout << word << endl;
        word.clear();
        fc = 0;
      } else {
        word += c;
        if (!fc)
          fc = c;
      }
    }
    {
      bool flag = false;
      for (int i = 0; i < (signed)data[fc - 'a'].size(); i++) {
        if (data[fc - 'a'][i].second == word) {
          data[fc - 'a'][i].first--;
          flag = true;
          break;
        }
      }
      if (!flag) {
        data[fc - 'a'].push_back(P(3000, word));
      }
    }

    for (int i = 0; i < 26; i++) {
      if (data[i].size())
        sort(data[i].begin(), data[i].end());
    }

    char in;

    cin >> in;

    if (data[in - 'a'].size()) {
      cout << data[in - 'a'][0].second;
      for (int i = 1; i < (signed)data[in - 'a'].size(); i++) {
        if (i >= 5)
          break;
        cout << ' ' << data[in - 'a'][i].second;
      }
      cout << endl;
    } else
      cout << "NA" << endl;
    cin.get();
  }

  return 0;
}