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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int h, int w) {
  vector<string> vs(h);
  rep(i, 0, h) cin >> vs[i];
  int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
  set<string> st;
  vector<vector<bool>> used(h, vector<bool>(w));
  string s, ans;
  function<void(int, int, int)> dfs = [&](int y, int x, int i) {
    if (st.find(s) != st.end() and s.size() >= ans.size() and
        (s.size() > ans.size() or s < ans))
      ans = s;
    st.insert(s);
    // rep(i,0,8){
    int ny = y + dy[i], nx = x + dx[i];
    if (ny == h)
      ny = 0;
    if (ny < 0)
      ny = h - 1;
    if (nx == w)
      nx = 0;
    if (nx < 0)
      nx = w - 1;
    if (used[ny][nx])
      return;
    s.push_back(vs[ny][nx]);
    used[ny][nx] = true;
    dfs(ny, nx, i);
    s.pop_back();
    used[ny][nx] = false;
    //}
    return;
  };
  rep(y, 0, h) rep(x, 0, w) rep(i, 0, 8) {
    s.push_back(vs[y][x]);
    used[y][x] = true;
    dfs(y, x, i);
    s.pop_back();
    used[y][x] = false;
  }

  if (ans.size() < 2)
    cout << 0 << endl;
  else
    cout << ans << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int h, w;
    cin >> h >> w;
    if (!h and !w)
      break;
    solve(h, w);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int h, int w) {
  vector<string> vs(h);
  rep(i, 0, h) cin >> vs[i];
  int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
  set<string> st;
  vector<vector<bool>> used(h, vector<bool>(w));
  string s, ans;
  function<void(int, int)> dfs = [&](int y, int x) {
    if (st.find(s) != st.end() and s.size() >= ans.size() and
        (s.size() > ans.size() or s < ans))
      ans = s;
    st.insert(s);
    rep(i, 0, 8) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny == h)
        ny = 0;
      if (ny < 0)
        ny = h - 1;
      if (nx == w)
        nx = 0;
      if (nx < 0)
        nx = w - 1;
      if (used[ny][nx])
        continue;
      s.push_back(vs[ny][nx]);
      used[ny][nx] = true;
      dfs(ny, nx);
      s.pop_back();
      used[ny][nx] = false;
    }
  };
  rep(y, 0, h) rep(x, 0, w) {
    s.push_back(vs[y][x]);
    used[y][x] = true;
    dfs(y, x);
    s.pop_back();
    used[y][x] = false;
  }

  if (ans.size() < 2)
    cout << 0 << endl;
  else
    cout << ans << endl;
}
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int h, w;
    cin >> h >> w;
    if (!h and !w)
      break;
    solve(h, w);
  }
  return 0;
}