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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const static int tx[] = {0, 1, 0, -1};
const static int ty[] = {-1, 0, 1, 0};

const static double EPS = 1e-8;

int main() {
  string cypher;
  while (cin >> cypher) {
    if (cypher == "#END")
      break;

    string res = "";
    string dp_str[301][301];

    for (int split_i = 1; split_i < cypher.size(); split_i++) {
      //[0,split_i),[split_i,cypher.size())
      if (res.size() >= min(split_i, (int)cypher.size() - split_i)) {
        continue;
      }
      string A = cypher.substr(0, split_i - 0);
      string B = cypher.substr(split_i, cypher.size() - split_i);

      dp_str[0][0] = "";
      for (int y = 0; y < A.size(); y++) {
        for (int x = 0; x < B.size(); x++) {
          if (A[y] == B[x]) {
            dp_str[y + 1][x + 1] = dp_str[y][x] + A[y];
          } else {
            if (dp_str[y][x + 1].size() > dp_str[y + 1][x].size()) {
              dp_str[y + 1][x + 1] = dp_str[y][x + 1];
            } else {
              dp_str[y + 1][x + 1] = dp_str[y + 1][x];
            }
          }
        }
      }
      if (res.size() < dp_str[A.size()][B.size()].size()) {
        res = dp_str[A.size()][B.size()];
      }
    }
    printf("%s\\n", res.c_str());
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

const static int tx[] = {0, 1, 0, -1};
const static int ty[] = {-1, 0, 1, 0};

const static double EPS = 1e-8;

int main() {
  string cypher;
  while (cin >> cypher) {
    if (cypher == "#END")
      break;

    string res = "";
    for (int split_i = 1; split_i < cypher.size(); split_i++) {
      //[0,split_i),[split_i,cypher.size())
      if (res.size() >= min(split_i, (int)cypher.size() - split_i)) {
        continue;
      }
      string A = cypher.substr(0, split_i - 0);
      string B = cypher.substr(split_i, cypher.size() - split_i);

      string dp_str[301][301];
      dp_str[0][0] = "";
      for (int y = 0; y < A.size(); y++) {
        for (int x = 0; x < B.size(); x++) {
          if (A[y] == B[x]) {
            dp_str[y + 1][x + 1] = dp_str[y][x] + A[y];
          } else {
            if (dp_str[y][x + 1].size() > dp_str[y + 1][x].size()) {
              dp_str[y + 1][x + 1] = dp_str[y][x + 1];
            } else {
              dp_str[y + 1][x + 1] = dp_str[y + 1][x];
            }
          }
        }
      }
      if (res.size() < dp_str[A.size()][B.size()].size()) {
        res = dp_str[A.size()][B.size()];
      }
    }
    printf("%s\\n", res.c_str());
  }
}