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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int dp[2010][2010];
int rev[2010][2010];

int main(void) {
  string pa, ra;
  while (cin >> pa) {
    if (cin.eof())
      break;

    rep(i, 2010) rep(j, 2010) dp[i][j] = 0;

    ra = pa;
    reverse(ra.begin(), ra.end());
    int n = pa.size();
    rep(i, n) rep(j, n) {
      if (pa[i] == ra[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        rev[i + 1][j + 1] = 0;
      } else if (dp[i][j + 1] > dp[i + 1][j]) {
        dp[i + 1][j + 1] = dp[i][j + 1];
        rev[i + 1][j + 1] = 1;
      } else {
        dp[i + 1][j + 1] = dp[i + 1][j];
        rev[i + 1][j + 1] = -1;
      }
    }
    string ans = "";

    for (int i = n, j = n; i > 0 && j > 0;) {
      // dump(i) dump(j)
      if (rev[i][j] > 0)
        i--;
      else if (rev[i][j] < 0)
        j--;
      else {
        ans += pa[i - 1];
        i--;
        j--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int dp[2010][2010];

int main(void) {
  string pa, ra;
  while (cin >> pa) {
    if (cin.eof())
      break;

    rep(i, 2010) rep(j, 2010) dp[i][j] = 0;
    ra = pa;
    reverse(ra.begin(), ra.end());
    int n = pa.size();
    rep(i, n) rep(j, n) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);
      dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + (pa[i] == ra[j]));
    }
    string ans = "";
    int cur = dp[n][n];
    // dump(cur)

    for (int i = n, j = n; i > 0 && j > 0;) {
      if (dp[i][j] == dp[i - 1][j])
        i--;
      else if (dp[i][j] == dp[i][j - 1])
        j--;
      else if (dp[i][j] == dp[i - 1][j - 1] + 1) {
        ans += pa[i - 1];
        i--;
        j--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}