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

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

//?¨??????\\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 999; // 1e9-1;

int dp[1001][1001] = {0};
pii from[1001][1001];

string X, Y;

int solve(int i, int j) {
  //    disp(i); disp(j);

  int ans;

  if (i < 0 or j < 0)
    return 0;

  if (dp[i][j] != 0)
    return dp[i][j];

  if (X[i] == Y[j]) {
    ans = solve(i - 1, j - 1) + 1;
    from[i][j] = make_pair(i - 1, j - 1);
  } else {
    if (solve(i - 1, j) > solve(i, j - 1)) {
      ans = solve(i - 1, j);
      from[i][j] = make_pair(i - 1, j);
    } else {
      ans = solve(i, j - 1);
      from[i][j] = make_pair(i, j - 1);
    }
  }

  dp[i][j] = ans;

  //    //test display
  //    dout<<"-------------------------------------\\n";
  //    dout<<"                ";
  //    rep(j,0,Y.size()) disP(j); dout<<endl;
  //    dout<<"                ";
  //    rep(j,0,Y.size()) disP(Y[j]); dout<<endl;
  //    rep(i,0,X.size()) {
  //        disP(i); disP(X[i]); dispAll(dp[i], Y.size());
  //    }

  return ans;
}

int main() {
  int q;
  din >> q;
  rep(i, 0, q) {
    din >> X >> Y;
    //        disp(X); disp(Y);
    //        dout<<solve(X.size()-1,Y.size()-1)<<endl;
    //
    //        rep(i,0,X.size()) {
    //            rep(j,0,Y.size()) {
    //                dp[i][j]=0;
    //                from[i][j] = make_pair(-1, -1);
    //            }
    //        }

    rep(i, 0, X.size() + 1) {
      rep(j, 0, Y.size() + 1) { dp[i][j] = 0; }
    }

    rep(i, 1, X.size() + 1) {
      rep(j, 1, Y.size() + 1) {
        if (X[i - 1] == Y[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }

    //            //test display
    //            dout<<"-------------------------------------\\n";
    //            dout<<"                ";
    //            rep(j,0,Y.size()+1) disP(j); dout<<endl;
    //            dout<<"                    ";
    //            rep(j,1,Y.size()+1) disP(Y[j-1]); dout<<endl;
    //            rep(i,0,X.size()+1) {
    //                if(i==0) {
    //                    disP(i); disP(0); dispAll(dp[i], Y.size()+1);
    //                } else {
    //                    disP(i); disP(X[i-1]); dispAll(dp[i], Y.size()+1);
    //                }
    //            }

    dout << dp[X.size()][Y.size()] << endl;
  }

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // next_permutation
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric> //accumulate
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map> //???????????\\??¢??°
#include <fstream> //ifstream, ofstream

// #define NDEBUG //NDEBUG???#include
// <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert

using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
//????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout                                                                   \\
  dummy.str("");                                                               \\
  dummy.clear(stringstream::goodbit);                                          \\
  dummy // dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§????????????????????????????????????
//http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
// #define OUTPUTFILE "output.txt"
// //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout                                                                   \\
  outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath                                                         \\
  "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/"      \\
  "output.txt"
#endif

//?¨??????\\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
// #define INPUTFILE "input.txt"
// //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif

#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A)                                                                \\
  dout << setw(3) << (A) << " " // << setw(3) ??????????????\\????????????
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define dispAll(A, n)                                                          \\
  dout << #A << " = ";                                                         \\
  rep(j, 0, (n)) { disP(A[j]); }                                               \\
  dout << endl

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;

const int INF = 999; // 1e9-1;

int dp[1000][1000] = {0};
pii from[1000][1000];

string X, Y;

int solve(int i, int j) {
  //    disp(i); disp(j);

  int ans;

  if (i < 0 or j < 0)
    return 0;

  if (dp[i][j] != 0)
    return dp[i][j];

  if (X[i] == Y[j]) {
    ans = solve(i - 1, j - 1) + 1;
    from[i][j] = make_pair(i - 1, j - 1);
  } else {
    if (solve(i - 1, j) > solve(i, j - 1)) {
      ans = solve(i - 1, j);
      from[i][j] = make_pair(i - 1, j);
    } else {
      ans = solve(i, j - 1);
      from[i][j] = make_pair(i, j - 1);
    }
  }

  dp[i][j] = ans;

  //    //test display
  //    dout<<"-------------------------------------\\n";
  //    dout<<"                ";
  //    rep(j,0,Y.size()) disP(j); dout<<endl;
  //    dout<<"                ";
  //    rep(j,0,Y.size()) disP(Y[j]); dout<<endl;
  //    rep(i,0,X.size()) {
  //        disP(i); disP(X[i]); dispAll(dp[i], Y.size());
  //    }

  return ans;
}

int main() {
  int q;
  din >> q;
  rep(i, 0, q) {
    din >> X >> Y;
    //        disp(X); disp(Y);
    dout << solve(X.size() - 1, Y.size() - 1) << endl;

    rep(i, 0, X.size()) {
      rep(j, 0, Y.size()) {
        dp[i][j] = 0;
        from[i][j] = make_pair(-1, -1);
      }
    }
  }

#ifdef INPUTFILE
  inputfile.close();
#endif

#ifdef OUTPUTFILE
  outputfile.close();
  cout << "\\"" << OutputFilePath << "\\"" << endl;
#endif

  return 0;
}