Problem p02937 (C++)

Buggy submission (status: Time Limit Exceeded):
// vim: shiftwidth=2 expandtab foldmethod=marker

// include {{{
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// }}}

// misc {{{
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
// }}}

// debug(...) {{{
static bool debug_enabled = false;
static int debug_max_elms = 10;

static int debug_max_count = 50;
static int debug_current_count = 0;

void debug1(char const *x) { cout << x; }
template <class T> void debug1(const T &x) { cout << x; }
template <class T1, class T2> void debug1(const pair<T1, T2> &x) {
  cout << "(" << x.first << ", " << x.second << ")";
}
template <class T> void debug1(const vector<T> &x) {
  cout << "[";
  auto i = x.begin();
  while (i != x.end()) {
    debug1(*i);
    ++i;
    if (i != x.end())
      cout << ", ";
    if (std::distance(x.begin(), i) > debug_max_elms) {
      cout << "...";
      break;
    }
  }
  cout << "]";
}
template <class K, class V> void debug1(const map<K, V> &x) {
  cout << "{";
  auto i = x.begin();
  while (i != x.end()) {
    debug1(get<0>(*i));
    cout << ": ";
    debug1(get<1>(*i));
    ++i;
    if (i != x.end())
      cout << ", ";
  }
  cout << "}";
}

void debug2() { cout << endl; }
template <class T, class... Ts> void debug2(const T &x, const Ts &...xs) {
  cout << ' ';
  debug1(x);
  debug2(xs...);
}

template <class T, class... Ts> void debug(const T &x, const Ts &...xs) {
  if (debug_enabled) {
    ++debug_current_count;
    if (debug_current_count > debug_max_count) {
      cout << "... (debug log disabled)" << endl;
      debug_enabled = false;
    }
    cout << "[D] ";
    debug1(x);
    debug2(xs...);
  }
}
// }}}

// main {{{
void solve();
int main() {
  auto v = getenv("DEBUG");
  if (v && string(v) == "1")
    debug_enabled = true;
  if (false)
    debug("dummy for suppress warnings", vector<int>(0));
  solve();
}
// }}}

int dp[30][100001];

int next(unsigned char c, const string &s, int i) {
  if (i == s.size())
    return -1;

  int idx = c - 'a';

  if (dp[idx][i] > -2)
    return dp[idx][i];

  if (s[i] == c) {
    return dp[idx][i] = i;
  }

  return dp[idx][i] = next(c, s, i + 1);
}

ll calc(const string &s, const string &t, int i, int j) {
  debug("calc:", i, s[i], j, t[j]);
  if (j == t.size())
    return 0;
  debug(s.substr(i, s.size() - i), t.substr(j, t.size() - j));
  int nx = next(t[j], s, i);
  debug("nx", nx);
  if (nx == -1) {
    if (i == 0)
      return -1;
    ll rest = calc(s, t, 0, j);
    if (rest == -1)
      return -1;
    else
      return s.length() - i + rest;
  }

  ll rest = calc(s, t, nx + 1, j + 1);
  if (rest == -1)
    return -1;
  return nx - i + 1 + rest;
}

void solve() {
  string s, t;
  getline(cin, s);
  getline(cin, t);

  rep(i, 30) rep(j, 100001) dp[i][j] = -2;

  cout << calc(s, t, 0, 0) << endl;
}


Fixed submission:
// vim: shiftwidth=2 expandtab foldmethod=marker

// include {{{
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// }}}

// misc {{{
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
// }}}

// debug(...) {{{
static bool debug_enabled = false;
static int debug_max_elms = 10;

static int debug_max_count = 50;
static int debug_current_count = 0;

void debug1(char const *x) { cout << x; }
template <class T> void debug1(const T &x) { cout << x; }
template <class T1, class T2> void debug1(const pair<T1, T2> &x) {
  cout << "(" << x.first << ", " << x.second << ")";
}
template <class T> void debug1(const vector<T> &x) {
  cout << "[";
  auto i = x.begin();
  while (i != x.end()) {
    debug1(*i);
    ++i;
    if (i != x.end())
      cout << ", ";
    if (std::distance(x.begin(), i) > debug_max_elms) {
      cout << "...";
      break;
    }
  }
  cout << "]";
}
template <class K, class V> void debug1(const map<K, V> &x) {
  cout << "{";
  auto i = x.begin();
  while (i != x.end()) {
    debug1(get<0>(*i));
    cout << ": ";
    debug1(get<1>(*i));
    ++i;
    if (i != x.end())
      cout << ", ";
  }
  cout << "}";
}

void debug2() { cout << endl; }
template <class T, class... Ts> void debug2(const T &x, const Ts &...xs) {
  cout << ' ';
  debug1(x);
  debug2(xs...);
}

template <class T, class... Ts> void debug(const T &x, const Ts &...xs) {
  if (debug_enabled) {
    ++debug_current_count;
    if (debug_current_count > debug_max_count) {
      cout << "... (debug log disabled)" << endl;
      debug_enabled = false;
    }
    cout << "[D] ";
    debug1(x);
    debug2(xs...);
  }
}
// }}}

// main {{{
void solve();
int main() {
  auto v = getenv("DEBUG");
  if (v && string(v) == "1")
    debug_enabled = true;
  if (false)
    debug("dummy for suppress warnings", vector<int>(0));
  solve();
}
// }}}

int dp[30][100001];

int next(unsigned char c, const string &s, int i) {
  if (i == s.size())
    return -1;

  int idx = c - 'a';

  if (dp[idx][i] > -2)
    return dp[idx][i];

  if (s[i] == c) {
    return dp[idx][i] = i;
  }

  return dp[idx][i] = next(c, s, i + 1);
}

ll calc(const string &s, const string &t, int i, int j) {
  debug("calc:", i, s[i], j, t[j]);
  if (j == t.size())
    return 0;
  // debug(s.substr(i, s.size() - i), t.substr(j, t.size() - j));
  int nx = next(t[j], s, i);
  debug("nx", nx);
  if (nx == -1) {
    if (i == 0)
      return -1;
    ll rest = calc(s, t, 0, j);
    if (rest == -1)
      return -1;
    else
      return s.length() - i + rest;
  }

  ll rest = calc(s, t, nx + 1, j + 1);
  if (rest == -1)
    return -1;
  return nx - i + 1 + rest;
}

void solve() {
  string s, t;
  getline(cin, s);
  getline(cin, t);

  rep(i, 30) rep(j, 100001) dp[i][j] = -2;

  cout << calc(s, t, 0, 0) << endl;
}