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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct State {
  int x[4][4];
};
char F[4][4];
vector<pair<int, int>> v;
string U[100];
int V[100], dp[10009];
int solve(string S, int depth, int cx, int cy, State Y) {
  if (depth == S.size())
    return 1;
  int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  int cnt = 0;
  for (int i = 0; i < 8; i++) {
    int ex = cx + dx[i], ey = cy + dy[i];
    if (ex < 0 || ey < 0 || ex >= 4 || ey >= 4)
      continue;
    if (Y.x[ex][ey] == 0 && F[ex][ey] == S[depth]) {
      State Z = Y;
      Z.x[ex][ey] = 1;
      cnt += solve(S, depth + 1, ex, ey, Z);
    }
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> U[i] >> V[i];
  }
  for (int i = 0; i < 16; i++)
    cin >> F[i / 4][i % 4];
  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int j = 0; j < 16; j++) {
      if (F[j / 4][j % 4] != U[i][0])
        continue;
      State R;
      for (int k = 0; k < 16; k++)
        R.x[k / 4][k % 4] = 0;
      R.x[j / 4][j % 4] = 1;
      c += solve(U[i], 1, j / 4, j % 4, R);
    }
    for (int j = 0; j < 20; j++) {
      if (c >= (1 << j)) {
        v.push_back(make_pair(V[i] * (1 << j), U[i].size() * (1 << j)));
        c -= (1 << j);
      } else {
        v.push_back(make_pair(V[i] * c, U[i].size() * c));
        break;
      }
    }
  }
  int T;
  cin >> T;
  int maxn = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = T; j >= v[i].second; j--) {
      dp[j] = max(dp[j], dp[j - v[i].second] + v[i].first);
      maxn = max(maxn, dp[j]);
    }
  }
  cout << maxn << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct State {
  int x[4][4];
};
char F[4][4];
vector<pair<int, int>> v, w;
string U[100];
int V[100], dp[10009];
int solve(string S, int depth, int cx, int cy, State Y) {
  if (depth == S.size())
    return 1;
  int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  int cnt = 0;
  for (int i = 0; i < 8; i++) {
    int ex = cx + dx[i], ey = cy + dy[i];
    if (ex < 0 || ey < 0 || ex >= 4 || ey >= 4)
      continue;
    if (Y.x[ex][ey] == 0 && F[ex][ey] == S[depth]) {
      State Z = Y;
      Z.x[ex][ey] = 1;
      cnt += solve(S, depth + 1, ex, ey, Z);
    }
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> U[i] >> V[i];
  }
  for (int i = 0; i < 16; i++)
    cin >> F[i / 4][i % 4];
  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int j = 0; j < 16; j++) {
      if (F[j / 4][j % 4] != U[i][0])
        continue;
      State R;
      for (int k = 0; k < 16; k++)
        R.x[k / 4][k % 4] = 0;
      R.x[j / 4][j % 4] = 1;
      c += solve(U[i], 1, j / 4, j % 4, R);
    }
    for (int j = 0; j < c; j++)
      v.push_back(make_pair(V[i], U[i].size()));
  }
  int T;
  cin >> T;
  int maxn = 0;
  for (int i = 0; i < v.size(); i++) {
    for (int j = T; j >= v[i].second; j--) {
      dp[j] = max(dp[j], dp[j - v[i].second] + v[i].first);
      maxn = max(maxn, dp[j]);
    }
  }
  cout << maxn << endl;
  return 0;
}