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

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>

int main() {
  int n;
  std::string str;

  while (1) {
    std::map<std::string, int> words[26];

    std::getline(std::cin, str);
    n = atoi(str.c_str());
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      int begin = 0;
      bool inword = false;
      std::getline(std::cin, str);

      for (int i = 0; i < str.size(); i++) {
        if (isalpha(str[i]) == 0) {
          if (inword) {
            inword = false;
            std::string word = str.substr(begin, i - begin);
            if (words[word[0] - 'a'].find(word) == words[word[0] - 'a'].end()) {
              words[word[0] - 'a'].insert(std::pair<std::string, int>(word, 1));
            } else {
              (words[word[0] - 'a'])[word]++;
            }
          }
        } else if (!inword) {
          inword = true;
          begin = i;
        }
      }
      if (inword) {
        std::string word = str.substr(begin);
        if (words[word[0] - 'a'].find(word) == words[word[0] - 'a'].end()) {
          words[word[0] - 'a'].insert(std::pair<std::string, int>(word, 1));
        } else {
          (words[word[0] - 'a'])[word]++;
        }
      }
    }

    std::getline(std::cin, str);
    if (words[str[0] - 'a'].size() == 0) {
      std::cout << "NA" << std::endl;
      ;
    } else {
      bool first = true;
      int i = 0;

      std::multimap<int, std::string, std::greater<int>> output;

      for (std::map<std::string, int>::iterator it =
               words[str[0] - 'a'].begin();
           it != words[str[0] - 'a'].end(); it++) {
        output.insert(std::pair<int, std::string>((*it).second, (*it).first));
      }

      for (std::multimap<int, std::string>::iterator it = output.begin();
           it != output.end(); it++) {
        if (first) {
          first = false;
        } else {
          std::cout << ' ';
        }
        std::cout << (*it).second;
        if (++i >= 5)
          break;
      }
      std::cout << std::endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <set>
#include <string>

int main() {
  int n;
  std::string str;

  while (1) {
    std::set<std::string> words[26];

    std::getline(std::cin, str);
    n = atoi(str.c_str());
    if (n == 0)
      break;

    int begin = 0;
    bool inword = false;
    for (int i = 0; i < n; i++) {
      std::getline(std::cin, str);

      for (int i = 0; i < str.size(); i++) {
        if (isalpha(str[i]) == 0 && inword) {
          inword = false;
          std::string word = str.substr(begin, i - begin);
          words[word[0] - 'a'].insert(word);
        } else if (!inword) {
          inword = true;
          begin = i;
        }
      }
      if (inword) {
        std::string word = str.substr(begin);
        words[word[0] - 'a'].insert(word);
      }
    }
    std::getline(std::cin, str);
    if (words[str[0] - 'a'].size() == 0) {
      std::cout << "NA" << std::endl;
      ;
    } else {
      bool first = true;
      int i = 0;
      for (std::set<std::string>::iterator it = words[str[0] - 'a'].begin();
           it != words[str[0] - 'a'].end(); it++) {
        if (first) {
          first = false;
        } else {
          std::cout << ' ';
        }
        std::cout << *it;
        if (++i >= 5)
          break;
      }
      std::cout << std::endl;
    }
  }
  return 0;
}