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

#include <bits/stdc++.h>
#define N 55
#define int long long
using namespace std;
const int s = ('a' - 1);
const int mod = (1e9) + 7;
int n;
string str[N];

int mem[N][N][22][30], used[N][N][22][30];
int calc(int y, int x, int ch) {
  int res = 0;
  if (x >= (int)str[y].size() || ch >= 27)
    return 0;

  if (str[y][x] != '?')
    res = 1;
  if (str[y][x] == '?')
    res = 27 - ch;
  for (++x; x < (int)str[y].size(); x++)
    if (str[y][x] == '?')
      res = (res * 26) % mod;
  return res;
}

bool check(int u, int d, int x, char ch) {
  static bool memc[N][N][22][30], usedc[N][N][22][30];
  if (usedc[u][d][x][ch - s])
    return memc[u][d][x][ch - s];
  usedc[u][d][x][ch - s] = 1;
  for (int y = u; y <= d; y++)
    if (str[y][x] != '?' && str[y][x] != ch)
      return memc[u][d][x][ch - s] = 0;
  return memc[u][d][x][ch - s] = 1;
}

bool check2(int u, int d, int x, char ch) {
  static bool memc[N][N][22][30], usedc[N][N][22][30];
  if (usedc[u][d][x][ch - s])
    return memc[u][d][x][ch - s];
  usedc[u][d][x][ch - s] = 1;
  for (int y = u; y <= d; y++)
    if (str[y][x] != '?' && str[y][x] <= ch)
      return memc[u][d][x][ch - s] = 0;
  return memc[u][d][x][ch - s] = 1;
}

bool check3(int u, int d, int x, int ch) {
  while (u <= d && str[u][x] == s)
    u++;
  for (int y = u; y <= d; y++)
    if (str[y][x] == s)
      return 0;
  return 1;
}

int dfs(int u, int d, int x, int c) {
  if (u > d)
    return 1;
  if (u == d)
    return calc(u, x, c);
  if (x >= 21)
    return 0;
  if (str[u][x] == s)
    return c == 1 && str[u + 1][x] != s ? dfs(u + 1, d, x, c) : 0;
  if (used[u][d][x][c]++)
    return mem[u][d][x][c];

  int res = 0;
  //[u,y]?????????????????????????????????????????????
  for (int ch = c; ch <= 26; ch++) {
    for (int y = u; y <= d; y++) {
      if (check(u, y, x, ch + s) == false)
        continue;
      if (check2(y + 1, d, x, ch + s) == false)
        continue;
      res =
          (res + (dfs(u, y, x + 1, 1) * dfs(y + 1, d, x, ch + 1)) % mod) % mod;
    }
  }
  return mem[u][d][x][c] = res;
}

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str[i];
    while (str[i].size() < 21)
      str[i] += s;
  }
  cout << dfs(0, n - 1, 0, 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 55
#define int long long
using namespace std;
const int s = ('a' - 1);
const int mod = (1e9) + 7;
int n;
string str[N];

int mem[N][N][22][30], used[N][N][22][30];
int calc(int y, int x, int ch) {
  int res = 0;
  if (x >= (int)str[y].size() || ch >= 27)
    return 0;

  if (str[y][x] != '?')
    res = 1;
  if (str[y][x] == '?')
    res = 27 - ch;
  for (++x; x < (int)str[y].size(); x++)
    if (str[y][x] == '?')
      res = (res * 26) % mod;
  return res;
}

bool check(int u, int d, int x, char ch) {
  for (int y = u; y <= d; y++)
    if (str[y][x] != '?' && str[y][x] != ch)
      return 0;
  return 1;
}

bool check2(int u, int d, int x, char ch) {
  for (int y = u; y <= d; y++)
    if (str[y][x] != '?' && str[y][x] <= ch)
      return 0;
  return 1;
}

int dfs(int u, int d, int x, int c) {
  // cout<<u<<" "<<d<<" "<<x<<" "<<c<<endl;
  if (u > d)
    return 1;
  if (u == d)
    return calc(u, x, c);
  if (x >= 21)
    return 0;
  if (used[u][d][x][c]++)
    return mem[u][d][x][c];
  int res = 0;

  //[u,y]?????????????????????????????????????????????
  for (int ch = c; ch <= 26; ch++) {
    for (int y = u; y <= d; y++) {
      if (check(u, y, x, ch + s) == false)
        continue;
      if (check2(y + 1, d, x, ch + s) == false)
        continue;
      res =
          (res + (dfs(u, y, x + 1, 1) * dfs(y + 1, d, x, ch + 1)) % mod) % mod;
    }
  }
  return mem[u][d][x][c] = res;
}

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str[i];
    while (str[i].size() < 20)
      str[i] += s;
  }
  cout << dfs(0, n - 1, 0, 1) << endl;
  return 0;
}