Problem p00088 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

vector<string> split(string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main() {
  map<char, string> ID;
  ID[' '] = "101";
  ID['\\''] = "000000";
  ID[','] = "000011";
  ID['-'] = "10010001";
  ID['.'] = "010001";
  ID['?'] = "000001";
  ID['A'] = "100101";
  ID['B'] = "10011010";
  ID['C'] = "0101";
  ID['D'] = "0001";
  ID['E'] = "110";
  ID['F'] = "01001";
  ID['G'] = "10011011";
  ID['H'] = "010000";
  ID['I'] = "0111";
  ID['J'] = "10011000";
  ID['K'] = "0110";
  ID['L'] = "00100";
  ID['M'] = "10011001";
  ID['N'] = "10011110";
  ID['O'] = "00101";
  ID['P'] = "111";
  ID['Q'] = "10011111";
  ID['R'] = "1000";
  ID['S'] = "00110";
  ID['T'] = "00111";
  ID['U'] = "10011100";
  ID['V'] = "10011101";
  ID['W'] = "000010";
  ID['X'] = "10010010";
  ID['Y'] = "10010011";
  ID['Z'] = "10010000";

  map<string, string> Id;
  Id["00000"] = "A";
  Id["00001"] = "B";
  Id["00010"] = "C";
  Id["00011"] = "D";
  Id["00100"] = "E";
  Id["00101"] = "F";
  Id["00110"] = "G";
  Id["00111"] = "H";
  Id["01000"] = "I";
  Id["01001"] = "J";
  Id["01010"] = "K";
  Id["01011"] = "L";
  Id["01100"] = "M";
  Id["01101"] = "N";
  Id["01110"] = "O";
  Id["01111"] = "P";
  Id["10000"] = "Q";
  Id["10001"] = "R";
  Id["10010"] = "S";
  Id["10011"] = "T";
  Id["10100"] = "U";
  Id["10101"] = "V";
  Id["10110"] = "W";
  Id["10111"] = "X";
  Id["11000"] = "Y";
  Id["11001"] = "Z";
  Id["11010"] = " ";
  Id["11011"] = ".";
  Id["11100"] = ",";
  Id["11101"] = "-";
  Id["11110"] = "'";
  Id["11111"] = "?";

  string s;
  string str = "";
  while (getline(cin, s)) {
    if (cin.eof())
      break;
    rep(i, s.size()) { str += ID[s[i]]; }

    while (str.length() % 5 != 0) {
      str += "0";
    }

    string ans = "";
    while (str.length()) {
      string temp = str.substr(0, 5);
      ans += Id[temp];
      str = str.substr(5);
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

vector<string> split(string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while ((found = str.find_first_of(delim, current)) != string::npos) {
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main() {
  string s;
  map<char, string> ID;
  ID[' '] = "101";
  ID['\\''] = "000000";
  ID[','] = "000011";
  ID['-'] = "100010001";
  ID['.'] = "010001";
  ID['?'] = "000001";
  ID['A'] = "100101";
  ID['B'] = "10011010";
  ID['C'] = "0101";
  ID['D'] = "0001";
  ID['E'] = "110";
  ID['F'] = "01001";
  ID['G'] = "10011011";
  ID['H'] = "010000";
  ID['I'] = "0111";
  ID['J'] = "10011000";
  ID['K'] = "0110";
  ID['L'] = "00100";
  ID['M'] = "10011001";
  ID['N'] = "100011110";
  ID['O'] = "00101";
  ID['P'] = "111";
  ID['Q'] = "10011111";
  ID['R'] = "1000";
  ID['S'] = "00110";
  ID['T'] = "00111";
  ID['U'] = "10011100";
  ID['V'] = "10011101";
  ID['W'] = "000010";
  ID['X'] = "10010010";
  ID['Y'] = "10010011";
  ID['Z'] = "10010000";

  map<string, string> Id;
  Id["00000"] = "A";
  Id["00001"] = "B";
  Id["00010"] = "C";
  Id["00011"] = "D";
  Id["00100"] = "E";
  Id["00101"] = "F";
  Id["00110"] = "G";
  Id["00111"] = "H";
  Id["01000"] = "I";
  Id["01001"] = "J";
  Id["01010"] = "K";
  Id["01011"] = "L";
  Id["01100"] = "M";
  Id["01101"] = "N";
  Id["01110"] = "O";
  Id["01111"] = "P";
  Id["10000"] = "Q";
  Id["10001"] = "R";
  Id["10010"] = "S";
  Id["10011"] = "T";
  Id["10100"] = "U";
  Id["10101"] = "V";
  Id["10110"] = "W";
  Id["10111"] = "X";
  Id["11000"] = "Y";
  Id["11001"] = "Z";
  Id["11010"] = " ";
  Id["11011"] = ".";
  Id["11100"] = ",";
  Id["11101"] = "-";
  Id["11110"] = "'";
  Id["11111"] = "?";

  vector<string> temp;
  string str = "";
  while (getline(cin, s)) {
    if (cin.eof())
      break;
    rep(i, s.size()) { str += ID[s[i]]; }

    int count = 1;
    for (int i = 0; i < str.size(); i++) {
      if (count == 5) {
        str.insert(i + 1, " ");
        count = 0;
      } else {
        count++;
      }
    }

    vector<string> str2 = split(str, ' ');
    while (str2[str2.size() - 1].size() < 5) {
      str2[str2.size() - 1] += "0";
    }

    string ans = "";
    rep(i, str2.size()) { ans += Id[str2[i]]; }

    cout << ans << endl;
  }
  return 0;
}