Problem p01563 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int INF = 1e9;
int R, C;

char s[128][20];
int len[128];
int dp[2][(1 << 16)];

int main() {
  scanf("%d %d", &R, &C);
  for (int i = 0; i < R; i++) {
    scanf("%s", s[i]);
    len[i] = strlen(s[i]);
  }

  fill((int *)dp[0], (int *)dp[2], -INF);
  dp[0][0] = 0;

  int ai = 1, bi = 0;
  int ans = 0;
  for (int i = 0; i <= R; i++) {

    for (int j = 0; j < C; j++) {

      swap(ai, bi);

      for (int bit = 0; bit < (1 << C); bit++)
        dp[bi][bit] = -INF;

      for (int bit = 0; bit < (1 << C); bit++) {
        if (dp[ai][bit] == -INF)
          continue;

        if (i > 0 && j == 0 && __builtin_popcount(bit) != len[i - 1])
          continue;
        if (i == R && j == 0)
          ans = max(ans, dp[ai][bit]);

        if (i == R)
          continue;
        int a = 0, b = 0, cost = dp[ai][bit];
        for (int k = 0; k < C - j; k++)
          a += (bit >> k & 1);
        for (int k = C - j; k < C; k++)
          b += (bit >> k & 1);

        int nbit = bit >> 1;
        dp[bi][nbit] = max(dp[bi][nbit], cost);

        if (b == len[i])
          continue;

        int flg = 0;
        if (bit & 1 && s[i][b] == s[i - 1][len[i - 1] - a])
          flg++;
        if (bit >> (C - 1) & 1)
          if (b > 0 && s[i][b - 1] == s[i][b])
            flg++;

        nbit = (bit | (1 << C)) >> 1;
        dp[bi][nbit] = max(dp[bi][nbit], cost + flg);
      }
    }
  }
  cout << ans * 2 << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int INF = 1e9;
int R, C;

char s[128][20];
int len[128];
int dp[129][16][(1 << 16)];

int main() {
  scanf("%d %d", &R, &C);
  for (int i = 0; i < R; i++) {
    scanf("%s", s[i]);
    len[i] = strlen(s[i]);
  }

  fill((int *)dp[0], (int *)dp[128], -INF);
  dp[0][0][0] = 0;

  int ans = 0;

  for (int i = 0; i <= R; i++) {
    for (int j = 0; j < C; j++) {
      for (int bit = 0; bit < (1 << C); bit++) {
        if (dp[i][j][bit] == -INF)
          continue;

        if (j == 0 && __builtin_popcount(bit) != len[i - 1])
          continue;
        if (i == R)
          ans = max(ans, dp[i][j][bit]);
        if (i == R)
          continue;
        int a = 0, b = 0, cost = dp[i][j][bit];
        for (int k = 0; k < C - j; k++)
          a += (bit >> k & 1);
        for (int k = C - j; k < C; k++)
          b += (bit >> k & 1);

        //   cout<<i<<' '<<j<<' '<<bit<<' '<<a<<' '<<b<<endl;

        int nbit = bit >> 1;
        if (j + 1 < C) {
          dp[i][j + 1][nbit] = max(dp[i][j + 1][nbit], cost);
        } else {
          dp[i + 1][0][nbit] = max(dp[i + 1][0][nbit], cost);
        }

        if (b == len[i])
          continue;

        int flg = 0;
        if (bit & 1 && s[i][b] == s[i - 1][len[i - 1] - a])
          flg++;
        if (bit >> (C - 1) & 1)
          if (b > 0 && s[i][b - 1] == s[i][b])
            flg++;

        nbit = (bit | (1 << C)) >> 1;
        if (j + 1 < C) {
          dp[i][j + 1][nbit] = max(dp[i][j + 1][nbit], cost + flg);
        } else {
          dp[i + 1][0][nbit] = max(dp[i + 1][0][nbit], cost + flg);
        }
      }
    }
  }
  cout << ans * 2 << endl;

  return 0;
}