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

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main(void) {

  char tmp;
  int sum;
  string inp;
  string before(""), num(""), after("");

  while (scanf("%c", &tmp) != EOF) {
    if (tmp == '\\n') {
      before += inp;
      after = ("");
      num = ("");
      for (int i = 0; i < before.length(); i++) {
        switch (before[i]) {
        case ' ':
          num += "101";
          break;
        case '\\'':
          num += "000000";
          break;
        case ',':
          num += "000011";
          break;
        case '-':
          num += "10010001";
          break;
        case '.':
          num += "010001";
          break;
        case '?':
          num += "000001";
          break;
        case 'A':
          num += "100101";
          break;
        case 'B':
          num += "10011010";
          break;
        case 'C':
          num += "0101";
          break;
        case 'D':
          num += "0001";
          break;
        case 'E':
          num += "110";
          break;
        case 'F':
          num += "01001";
          break;
        case 'G':
          num += "10011011";
          break;
        case 'H':
          num += "010000";
          break;
        case 'I':
          num += "0111";
          break;
        case 'J':
          num += "10011000";
          break;
        case 'K':
          num += "0110";
          break;
        case 'L':
          num += "00100";
          break;
        case 'M':
          num += "10011001";
          break;
        case 'N':
          num += "10011110";
          break;
        case 'O':
          num += "00101";
          break;
        case 'P':
          num += "111";
          break;
        case 'Q':
          num += "10011111";
          break;
        case 'R':
          num += "1000";
          break;
        case 'S':
          num += "00110";
          break;
        case 'T':
          num += "00111";
          break;
        case 'U':
          num += "10011100";
          break;
        case 'V':
          num += "10011101";
          break;
        case 'W':
          num += "000010";
          break;
        case 'X':
          num += "10010010";
          break;
        case 'Y':
          num += "10010011";
          break;
        case 'Z':
          num += "10010000";
          break;
        }
      }
      while (num.length() % 5 != 0) {
        num += '0';
      }
      for (int i = 0; i < num.length(); i += 5) {
        sum = (num[i] - '0') * 16 + (num[i + 1] - '0') * 8 +
              (num[i + 2] - '0') * 4 + (num[i + 3] - '0') * 2 +
              (num[i + 4] - '0') * 1;
        switch (sum) {
        case 0:
          after += 'A';
          break;
        case 1:
          after += 'B';
          break;
        case 2:
          after += 'C';
          break;
        case 3:
          after += 'D';
          break;
        case 4:
          after += 'E';
          break;
        case 5:
          after += 'F';
          break;
        case 6:
          after += 'G';
          break;
        case 7:
          after += 'H';
          break;
        case 8:
          after += 'I';
          break;
        case 9:
          after += 'J';
          break;
        case 10:
          after += 'K';
          break;
        case 11:
          after += 'L';
          break;
        case 12:
          after += 'M';
          break;
        case 13:
          after += 'N';
          break;
        case 14:
          after += 'O';
          break;
        case 15:
          after += 'P';
          break;
        case 16:
          after += 'Q';
          break;
        case 17:
          after += 'R';
          break;
        case 18:
          after += 'S';
          break;
        case 19:
          after += 'T';
          break;
        case 20:
          after += 'U';
          break;
        case 21:
          after += 'V';
          break;
        case 22:
          after += 'W';
          break;
        case 23:
          after += 'X';
          break;
        case 24:
          after += 'Y';
          break;
        case 25:
          after += 'Z';
          break;
        case 26:
          after += ' ';
          break;
        case 27:
          after += '.';
          break;
        case 28:
          after += ',';
          break;
        case 29:
          after += '-';
          break;
        case 30:
          after += '\\'';
          break;
        case 31:
          after += '?';
          break;
        }
      }
      cout << after << endl;
      before = ("");
    } else
      before += tmp;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main(void) {

  char tmp;
  int sum;
  string inp;
  string before(""), num(""), after("");

  while (tmp = getchar()) {
    if (tmp == '\\n') {
      before += inp;
      after = ("");
      num = ("");
      for (int i = 0; i < before.length(); i++) {
        switch (before[i]) {
        case ' ':
          num += "101";
          break;
        case '\\'':
          num += "000000";
          break;
        case ',':
          num += "000011";
          break;
        case '-':
          num += "10010001";
          break;
        case '.':
          num += "010001";
          break;
        case '?':
          num += "000001";
          break;
        case 'A':
          num += "100101";
          break;
        case 'B':
          num += "10011010";
          break;
        case 'C':
          num += "0101";
          break;
        case 'D':
          num += "0001";
          break;
        case 'E':
          num += "110";
          break;
        case 'F':
          num += "01001";
          break;
        case 'G':
          num += "10011011";
          break;
        case 'H':
          num += "010000";
          break;
        case 'I':
          num += "0111";
          break;
        case 'J':
          num += "10011000";
          break;
        case 'K':
          num += "0110";
          break;
        case 'L':
          num += "00100";
          break;
        case 'M':
          num += "10011001";
          break;
        case 'N':
          num += "10011110";
          break;
        case 'O':
          num += "00101";
          break;
        case 'P':
          num += "111";
          break;
        case 'Q':
          num += "10011111";
          break;
        case 'R':
          num += "1000";
          break;
        case 'S':
          num += "00110";
          break;
        case 'T':
          num += "00111";
          break;
        case 'U':
          num += "10011100";
          break;
        case 'V':
          num += "10011101";
          break;
        case 'W':
          num += "000010";
          break;
        case 'X':
          num += "10010010";
          break;
        case 'Y':
          num += "10010011";
          break;
        case 'Z':
          num += "10010000";
          break;
        }
      }
      while (num.length() % 5 != 0) {
        num += '0';
      }
      for (int i = 0; i < num.length(); i += 5) {
        sum = (num[i] - '0') * 16 + (num[i + 1] - '0') * 8 +
              (num[i + 2] - '0') * 4 + (num[i + 3] - '0') * 2 +
              (num[i + 4] - '0') * 1;
        switch (sum) {
        case 0:
          after += 'A';
          break;
        case 1:
          after += 'B';
          break;
        case 2:
          after += 'C';
          break;
        case 3:
          after += 'D';
          break;
        case 4:
          after += 'E';
          break;
        case 5:
          after += 'F';
          break;
        case 6:
          after += 'G';
          break;
        case 7:
          after += 'H';
          break;
        case 8:
          after += 'I';
          break;
        case 9:
          after += 'J';
          break;
        case 10:
          after += 'K';
          break;
        case 11:
          after += 'L';
          break;
        case 12:
          after += 'M';
          break;
        case 13:
          after += 'N';
          break;
        case 14:
          after += 'O';
          break;
        case 15:
          after += 'P';
          break;
        case 16:
          after += 'Q';
          break;
        case 17:
          after += 'R';
          break;
        case 18:
          after += 'S';
          break;
        case 19:
          after += 'T';
          break;
        case 20:
          after += 'U';
          break;
        case 21:
          after += 'V';
          break;
        case 22:
          after += 'W';
          break;
        case 23:
          after += 'X';
          break;
        case 24:
          after += 'Y';
          break;
        case 25:
          after += 'Z';
          break;
        case 26:
          after += ' ';
          break;
        case 27:
          after += '.';
          break;
        case 28:
          after += ',';
          break;
        case 29:
          after += '-';
          break;
        case 30:
          after += '\\'';
          break;
        case 31:
          after += '?';
          break;
        }
      }
      cout << after << endl;
      before = ("");
    } else
      before += tmp;
  }

  return 0;
}