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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
// #include "util.h"

using namespace std;
typedef unsigned uint;
typedef long long lint;
typedef unsigned long long ulint;

//??????
template <typename _KTy, typename _Ty>
ostream &operator<<(ostream &ostr, const pair<_KTy, _Ty> &m) {
  cout << "{" << m.first << ", " << m.second << "}";
  return ostr;
}
template <typename _KTy, typename _Ty>
ostream &operator<<(ostream &ostr, const map<_KTy, _Ty> &m) {
  if (m.empty()) {
    cout << "{ }";
    return ostr;
  }
  cout << "{" << *m.begin();
  for (auto itr = ++m.begin(); itr != m.end(); itr++) {
    cout << ", " << *itr;
  }
  cout << "}";
  return ostr;
}
template <typename _Ty>
ostream &operator<<(ostream &ostr, const vector<_Ty> &v) {
  if (v.empty()) {
    cout << "{ }";
    return ostr;
  }
  cout << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    cout << ", " << *itr;
  }
  cout << "}";
  return ostr;
}
template <typename _Ty> ostream &operator<<(ostream &ostr, const set<_Ty> &s) {
  if (s.empty()) {
    cout << "{ }";
    return ostr;
  }
  cout << "{" << *(s.begin());
  for (auto itr = ++s.begin(); itr != s.end(); itr++) {
    cout << ", " << *itr;
  }
  cout << "}";
  return ostr;
}
// template <typename T> void print(T* v, int N) { if (N == 0) cout << "{ }";
// cout << "{" << v[0]; for (int i = 1; i < N; i++) cout << ", " << v[i]; cout
// << "}"; }
#define PI 3.14159265358979323846
#define EPS 1e-8
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()

string binary(int n, int len = 32) {
  string ret;
  for (int i = len - 1; i >= 0; i--) {
    ret.push_back((n & (1 << i)) ? '1' : '0');
  }
  return ret;
}

int main() {
  map<char, string> ctob;
  for (char ch = 'A'; ch <= 'Z'; ch++) {
    ctob[ch] = binary(ch - 'A', 5);
  }
  ctob[' '] = "11010";
  ctob['.'] = "11011";
  ctob[','] = "11100";
  ctob['-'] = "11101";
  ctob['\\''] = "11110";
  ctob['?'] = "11111";

  char alpha[32] = {' ', '\\'', ',', '-', '.', '?'};
  for (int i = 6; i < 32; i++)
    alpha[i] = char('A' + i - 6);
  string code[32] = {"101",      "000000",   "000011",   "10010001", "010001",
                     "000001",   "100101",   "10011010", "0101",     "0001",
                     "110",      "01001",    "10011011", "010000",   "0111",
                     "10011000", "0110",     "00100",    "10011001", "10011110",
                     "00101",    "111",      "10011111", "1000",     "00110",
                     "00111",    "10011100", "10011101", "000010",   "10010010",
                     "10010011", "10010000"};

  string S;
  while (getline(cin, S)) {
    string src, dst;
    for (int i = 0; i < S.size(); i++)
      src += ctob[S[i]];

    bool flag = true;
    while (flag) {
      flag = false;
      for (int i = 0; i < 32; i++) {
        int len = code[i].size();
        if (len > src.size())
          continue;
        if (code[i] == src.substr(0, code[i].size())) {
          dst.push_back(alpha[i]);
          src.erase(src.begin(), src.begin() + len);
          flag = true;
        }
      }
    }
    cout << dst << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>

using namespace std;

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

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

void Decode(string &bin) {
  string sub;
  bool flag = false;

  while (!bin.empty()) {

    if (bin == "0" || bin == "00" || bin == "000" || bin == "0000" ||
        bin == "00000" || bin == "000000" || bin == "0000000")
      break;

    sub = bin.substr(0, 3);
    if (sub == "111") {
      cout << 'P';
      flag = true;
    } else if (sub == "110") {
      cout << 'E';
      flag = true;
    } else if (sub == "101") {
      cout << ' ';
      flag = true;
    }
    if (flag) {
      flag = false;
      bin.erase(0, 3);
      continue;
    }

    sub = bin.substr(0, 4);
    if (sub == "1000") {
      cout << 'R';
      flag = true;
    } else if (sub == "0111") {
      cout << 'I';
      flag = true;
    } else if (sub == "0110") {
      cout << 'K';
      flag = true;
    } else if (sub == "0101") {
      cout << 'C';
      flag = true;
    } else if (sub == "0001") {
      cout << 'D';
      flag = true;
    }
    if (flag) {
      flag = false;
      bin.erase(0, 4);
      continue;
    }

    sub = bin.substr(0, 5);
    if (sub == "01001") {
      cout << 'F';
      flag = true;
    } else if (sub == "00111") {
      cout << 'T';
      flag = true;
    } else if (sub == "00110") {
      cout << 'S';
      flag = true;
    } else if (sub == "00101") {
      cout << 'O';
      flag = true;
    } else if (sub == "00100") {
      cout << 'L';
      flag = true;
    }
    if (flag) {
      flag = false;
      bin.erase(0, 5);
      continue;
    }

    sub = bin.substr(0, 6);
    if (sub == "100101") {
      cout << 'A';
      flag = true;
    } else if (sub == "010001") {
      cout << '.';
      flag = true;
    } else if (sub == "010000") {
      cout << 'H';
      flag = true;
    } else if (sub == "000011") {
      cout << ',';
      flag = true;
    } else if (sub == "000010") {
      cout << 'W';
      flag = true;
    } else if (sub == "000001") {
      cout << '?';
      flag = true;
    } else if (sub == "000000") {
      cout << '\\'';
      flag = true;
    }
    if (flag) {
      flag = false;
      bin.erase(0, 6);
      continue;
    }

    sub = bin.substr(0, 8);
    if (sub == "10011111") {
      cout << 'Q';
      flag = true;
    } else if (sub == "10011110") {
      cout << 'N';
      flag = true;
    } else if (sub == "10011101") {
      cout << 'V';
      flag = true;
    } else if (sub == "10011100") {
      cout << 'U';
      flag = true;
    } else if (sub == "10011011") {
      cout << 'G';
      flag = true;
    } else if (sub == "10011010") {
      cout << 'B';
      flag = true;
    } else if (sub == "10011001") {
      cout << 'M';
      flag = true;
    } else if (sub == "10011000") {
      cout << 'J';
      flag = true;
    } else if (sub == "10010011") {
      cout << 'Y';
      flag = true;
    } else if (sub == "10010010") {
      cout << 'X';
      flag = true;
    } else if (sub == "10010001") {
      cout << '-';
      flag = true;
    } else if (sub == "10010000") {
      cout << 'Z';
      flag = true;
    }
    if (flag) {
      flag = false;
      bin.erase(0, 8);
      continue;
    }
  }
  cout << endl;
}

int main() {
  map<char, string> encode;
  make_encode(encode);

  map<string, char> decode;
  make_decode(decode);

  string str, bin;
  while (getline(cin, str)) {
    for (int i = 0; i < str.size(); i++) {
      bin.append(encode[str[i]]);
    }
    Decode(bin);
  }
  return 0;
}