Problem p03298 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  ni(a[i])
#define nal(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 18;
const double eps = 1e-9;
using namespace std;
string str;
ll dp[MAXN][MAXN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n >> str;
  ll ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    memset(dp, 0, sizeof dp);
    string t1, t2;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        t1 = str[j] + t1;
      else
        t2 = str[j] + t2;
    int l1 = t1.length(), l2 = t2.length();
    for (int j = 0; j <= l1; j++)
      for (int k = 0; k <= l2; k++) {
        if (j == 0 && k == 0)
          dp[j][k] = 1;
        else if (j == 0 && t2[k - 1] == str[n + k - 1])
          dp[j][k] = dp[j][k - 1];
        else if (k == 0 && t1[j - 1] == str[n + j - 1])
          dp[j][k] = dp[j][k - 1];
        else if (t1[j - 1] == str[n + j + k - 1] &&
                 t2[k - 1] != str[n + j + k - 1])
          dp[j][k] = dp[j - 1][k];
        else if (t1[j - 1] != str[n + j + k - 1] &&
                 t2[k - 1] == str[n + j + k - 1])
          dp[j][k] = dp[j][k - 1];
        else if (t1[j - 1] == str[n + j + k - 1] &&
                 t2[k - 1] == str[n + j + k - 1])
          dp[j][k] = dp[j - 1][k] + dp[j][k - 1];
      }
    ans += dp[l1][l2];
  }
  cout << 2ll * ans << "\n";
  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  ni(a[i])
#define nal(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 20;
const double eps = 1e-9;
using namespace std;
string str;
ll dp[MAXN][MAXN];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n >> str;
  ll ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    memset(dp, 0, sizeof dp);
    string t1, t2;
    for (int j = 0; j < n; j++)
      if ((i >> j) & 1)
        t1 = str[j] + t1;
      else
        t2 = str[j] + t2;
    int l1 = t1.length(), l2 = t2.length();
    for (int j = 0; j <= l1; j++)
      for (int k = 0; k <= l2; k++) {
        if (j == 0 && k == 0)
          dp[j][k] = 1;
        else if (j == 0 && t2[k - 1] == str[n + k - 1])
          dp[j][k] = dp[j][k - 1];
        else if (k == 0 && t1[j - 1] == str[n + j - 1])
          dp[j][k] = dp[j][k - 1];
        else if (t1[j - 1] == str[n + j + k - 1] &&
                 t2[k - 1] != str[n + j + k - 1])
          dp[j][k] = dp[j - 1][k];
        else if (t1[j - 1] != str[n + j + k - 1] &&
                 t2[k - 1] == str[n + j + k - 1])
          dp[j][k] = dp[j][k - 1];
        else if (t1[j - 1] == str[n + j + k - 1] &&
                 t2[k - 1] == str[n + j + k - 1])
          dp[j][k] = dp[j - 1][k] + dp[j][k - 1];
      }
    ans += dp[l1][l2];
  }
  cout << 2ll * ans << "\n";
  return 0;
}