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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

string L;
bool input() {
  string _;
  getline(cin, _);
  if (cin.eof())
    return false;
  L.clear();
  for (int i = 0; i < _.size(); i++) {
    if (isalpha(_[i])) {
      L.push_back(toupper(_[i]));
    }
  }
  return true;
}

#define ML 1024
int dp[ML][ML];

bool isPalindrome(int from, int to) {
  if (dp[from][to] >= 0)
    return dp[from][to];
  if (from >= to)
    return 1;
  if (L[from] == L[to]) {
    return dp[from][to] = isPalindrome(from + 1, to - 1);
  }
  return dp[from][to] = 0;
}

set<string> Ans;
set<string> Dup;

void AddDup(int from, int to) {
  if (to - from <= 1)
    return;
  Dup.insert(L.substr(from, to - from + 1));
  AddDup(from + 1, to - 1);
}

bool used[ML][ML];
void dfs(int from, int to) {
  if (to - from <= 1)
    return;
  if (used[from][to])
    return;
  // cerr << "(" << from << ", " << to << ")" << endl;
  string X = L.substr(from, to - from + 1);
  if (isPalindrome(from, to) && !Dup.count(X)) {
    // cerr << from << " " << to << endl;
    Ans.insert(X);
    AddDup(from + 1, to - 1);
  }
  dfs(from + 1, to);
  dfs(from, to - 1);
  used[from][to] = true;
}

void solve() {
  Ans.clear();
  Dup.clear();
  memset(dp, -1, sizeof(dp));
  memset(used, 0, sizeof(used));
  dfs(0, int(L.size()) - 1);
  if (Ans.empty()) {
    cout << endl;
    return;
  }
  set<string> RealAns;
  set<string>::iterator it = Ans.begin();
  for (; it != Ans.end(); it++) {
    if (!Dup.count(*it))
      RealAns.insert(*it);
  }
  it = RealAns.begin();
  cout << *it;
  for (it++; it != RealAns.end(); it++) {
    cout << " " << *it;
  }
  cout << endl;
}

int main() {
  while (input())
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

string L;
bool input() {
  string _;
  getline(cin, _);
  if (cin.eof())
    return false;
  L.clear();
  for (int i = 0; i < _.size(); i++) {
    if (isalpha(_[i])) {
      L.push_back(toupper(_[i]));
    }
  }
  return true;
}

#define ML 1024
int dp[ML][ML];

bool isPalindrome(int from, int to) {
  if (dp[from][to] >= 0)
    return dp[from][to];
  if (from >= to)
    return 1;
  if (L[from] == L[to]) {
    return dp[from][to] = isPalindrome(from + 1, to - 1);
  }
  return dp[from][to] = 0;
}

set<string> Ans;
set<string> Dup;

void AddDup(int from, int to) {
  if (to - from <= 1)
    return;
  Dup.insert(L.substr(from, to - from + 1));
  AddDup(from + 1, to - 1);
}

void dfs(int from, int to, bool flag) {
  if (to - from <= 1)
    return;
  if (flag && isPalindrome(from, to)) {
    // cerr << from << " " << to << endl;
    Ans.insert(L.substr(from, to - from + 1));
    AddDup(from + 1, to - 1);
  }
  dfs(from + 1, to, flag);
  dfs(from, to - 1, flag);
}

void solve() {
  Ans.clear();
  Dup.clear();
  memset(dp, -1, sizeof(dp));
  dfs(0, int(L.size()) - 1, true);
  if (Ans.empty()) {
    cout << endl;
    return;
  }
  set<string> RealAns;
  set<string>::iterator it = Ans.begin();
  for (; it != Ans.end(); it++) {
    if (!Dup.count(*it))
      RealAns.insert(*it);
  }
  it = RealAns.begin();
  cout << *it;
  for (it++; it != RealAns.end(); it++) {
    cout << " " << *it;
  }
  cout << endl;
}

int main() {
  while (input())
    solve();
  return 0;
}