Problem p03960 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define SET(a, c) memset(a, c, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define pii pair<int, int>
#define pcc pair<char, char>
#define pic pair<int, char>
#define pci pair<char, int>
#define VS vector<string>
#define VI vector<int>
#define DEBUG(x) cout << #x << ": " << x << endl
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define pi 2 * acos(0.0)
#define INFILE() freopen("in0.txt", "r", stdin)
#define OUTFILE() freopen("out0.txt", "w", stdout)
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
struct input_returnner {
  int N;
  input_returnner(int N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(int N) { return std::move(input_returnner(N)); }
} // namespace

void solve();
/// ---template---
signed main(void) {
  SETUP;
  solve();
  return 0;
}

const ull MOD = 1e9 + 7;

string c[301];
int H, W;

int getCost(int col) {
  int x = col;
  int y = col + 1;
  int cost[301][301] = {};
  REP(d, H) {
    cost[H][d] = 0;
    cost[d][H] = 0;
    RFOR(i, 0, H - d) {
      int j = i + d;
      cost[i][j] = cost[i + 1][j + 1] + (c[H - i - 1][x] == c[H - j - 1][y]);
      if (d != 0)
        cost[j][i] = cost[j + 1][i + 1] + (c[H - j - 1][x] == c[H - i - 1][y]);
    }
  }
  int dp[301][301];
  dp[0][0] = 0;

  FOR(d, 1, 2 * H + 1) {
    FOR(i, 0, d + 1) {
      int j = d - i;
      int a, b;
      a = b = inf;
      if (i - 1 >= 0)
        a = dp[i - 1][j] + cost[i - 1][j];
      if (j - 1 >= 0)
        b = dp[i][j - 1] + cost[i][j - 1];
      dp[i][j] = min(a, b);
    }
  }

  return dp[H][H];
}

void solve() {
  cin >> H >> W;
  REP(i, H) cin >> c[i];
  ll res = 0;
  REP(i, W - 1) { res += getCost(i); }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define SET(a, c) memset(a, c, sizeof a)
#define CLR(a) memset(a, 0, sizeof a)
#define pii pair<int, int>
#define pcc pair<char, char>
#define pic pair<int, char>
#define pci pair<char, int>
#define VS vector<string>
#define VI vector<int>
#define DEBUG(x) cout << #x << ": " << x << endl
#define MIN(a, b) (a > b ? b : a)
#define MAX(a, b) (a > b ? a : b)
#define pi 2 * acos(0.0)
#define INFILE() freopen("in0.txt", "r", stdin)
#define OUTFILE() freopen("out0.txt", "w", stdout)
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define FST first
#define SEC second
#define SETUP cin.tie(0), ios::sync_with_stdio(false), cout << setprecision(15)

namespace {
struct input_returnner {
  int N;
  input_returnner(int N_ = 0) : N(N_) {}
  template <typename T> operator vector<T>() const {
    vector<T> res(N);
    for (auto &a : res)
      cin >> a;
    return std::move(res);
  }
  template <typename T> operator T() const {
    T res;
    cin >> res;
    return res;
  }
  template <typename T> T operator-(T right) {
    return T(input_returnner()) - right;
  }
  template <typename T> T operator+(T right) {
    return T(input_returnner()) + right;
  }
  template <typename T> T operator*(T right) {
    return T(input_returnner()) * right;
  }
  template <typename T> T operator/(T right) {
    return T(input_returnner()) / right;
  }
  template <typename T> T operator<<(T right) {
    return T(input_returnner()) << right;
  }
  template <typename T> T operator>>(T right) {
    return T(input_returnner()) >> right;
  }
};
template <typename T> input_returnner in() { return in<T>(); }
input_returnner in() { return input_returnner(); }
input_returnner in(int N) { return std::move(input_returnner(N)); }
} // namespace

void solve();
/// ---template---
signed main(void) {
  SETUP;
  solve();
  return 0;
}

const ull MOD = 1e9 + 7;

string c[301];
int H, W;

int getCost(int col) {
  int x = col;
  int y = col + 1;
  int cost[301][301] = {};
  REP(d, H) {
    cost[H][d] = 0;
    cost[d][H] = 0;
    RFOR(i, 0, H - d) {
      int j = i + d;
      cost[i][j] = cost[i + 1][j + 1] + (c[H - i - 1][x] == c[H - j - 1][y]);
      if (d != 0)
        cost[j][i] = cost[j + 1][i + 1] + (c[H - j - 1][x] == c[H - i - 1][y]);
    }
  }
  int dp[301][301];
  dp[0][0] = 0;

  FOR(d, 1, 2 * H + 1) {
    FOR(i, 0, d + 1) {
      int j = d - i;
      int a, b;
      a = b = inf;
      if (i > H or j > H)
        continue;
      if (i - 1 >= 0)
        a = dp[i - 1][j] + cost[i - 1][j];
      if (j - 1 >= 0)
        b = dp[i][j - 1] + cost[i][j - 1];
      dp[i][j] = min(a, b);
    }
  }

  return dp[H][H];
}

void solve() {
  cin >> H >> W;
  REP(i, H) cin >> c[i];
  ll res = 0;
  REP(i, W - 1) { res += getCost(i); }
  cout << res << endl;
}