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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

long long mod = 1000000007;

int n;
ll dp[54][54][23][29];
char v[54][23];
ll saiki(int a, int b, int c, int d) {
  if (d >= 27 || c >= 21)
    return 0;
  if (dp[a][b][c][d] != -1)
    return dp[a][b][c][d];
  if (a == b && c == 20)
    return 1;
  ll ans = saiki(a, b, c, d + 1);
  for (int i = a; i <= b; i++) {
    bool flag = 1;
    if (d == 0 && v[i][c] == 0) {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans =
            (ans + (saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod) %
            mod;
      }
      flag = 0;
    }
    if (d != 0 && v[i][c] == '?') {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans =
            (ans + (saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod) %
            mod;
      }
      flag = 0;
    }
    if (d != 0 && v[i][c] == d - 1 + 'a') {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans =
            (ans + (saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod) %
            mod;
      }
      flag = 0;
    }
    if (flag)
      break;
  }
  dp[a][b][c][d] = ans;
  return dp[a][b][c][d];
}

int main() {
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, s.size()) { v[i][j] = s[j]; }
    for (int j = s.size(); j < 21; j++) {
      v[i][j] = 0;
    }
  }
  Fill(dp, -1);
  cout << saiki(0, n - 1, 0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

long long mod = 1000000007;

int n;
ll dp[54][54][23][29];
char v[54][23];
ll saiki(int a, int b, int c, int d) {
  if (d >= 27 || c >= 21)
    return 0;
  if (dp[a][b][c][d] != 0)
    return dp[a][b][c][d];
  if (a == b && c == 20)
    return 1;
  ll ans = saiki(a, b, c, d + 1);
  for (int i = a; i <= b; i++) {
    bool flag = 1;
    if (d == 0 && v[i][c] == 0) {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans = (ans + saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod;
      }
      flag = 0;
    }
    if (d != 0 && v[i][c] == '?') {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans = (ans + saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod;
      }
      flag = 0;
    }
    if (d != 0 && v[i][c] == d - 1 + 'a') {
      if (i == b) {
        ans = (ans + saiki(a, i, c + 1, 0)) % mod;
      } else {
        ans = (ans + saiki(a, i, c + 1, 0) * saiki(i + 1, b, c, d + 1)) % mod;
      }
      flag = 0;
    }
    if (flag)
      break;
  }
  return dp[a][b][c][d] = ans;
}

int main() {
  cin >> n;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, s.size()) { v[i][j] = s[j]; }
    for (int j = s.size(); j < 21; j++) {
      v[i][j] = 0;
    }
  }
  cout << saiki(0, n - 1, 0, 0) << endl;
  return 0;
}