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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define PQ priority_queue
#define VT vector
#define MP make_pair
#define FOR(i, b, e) for (int i = b; i < e; i++)
#define CaseRead                                                               \\
  int _TT;                                                                     \\
  scanf("%d", &_TT);                                                           \\
  for (int kase = 1; kase <= _TT; kase++)

typedef long long ll;

char ma[25][25];
int n, m;
vector<string> v;

int dx[] = {0, 0, -1, -1, -1, 1, 1, 1};
int dy[] = {1, -1, 0, 1, -1, 0, 1, -1};

void solve(int x, int y) {
  //    printf("solve(%d, %d)\\n", x, y);
  for (int i = 0; i < 8; i++) {
    string s = "";
    s += ma[x][y];
    int nx, ny;
    nx = (x + n + dx[i]) % n;
    ny = (y + m + dy[i]) % m;

    while (nx != x || ny != y) {
      s += ma[nx][ny];
      nx = (nx + n + dx[i]) % n;
      ny = (ny + m + dy[i]) % m;
    }
    v.push_back(s);
    //        cout << s << endl;
  }
}

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    v.clear();
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      scanf("%s", ma[i]);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        solve(i, j);
    sort(v.begin(), v.end());
    int len = 0;
    string ans;

    for (int i = 1; i < v.size(); i++) {
      //            cout << "compare " << v[i] << " and " << v[i-1] << endl;
      int len1 = 0;
      for (int j = 0;
           j < min(v[i].length(), v[i - 1].length()) && v[i][j] == v[i - 1][j];
           j++) {
        len1++;
      }
      if (len1 > len) {
        len = len1;
        ans = v[i].substr(0, len1);
      }
    }
    if (ans.length() < 2)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;

char s[1605][203];
char ans[203], sp[203];
char mp[11][22];
int maxv, h, w, indx, mx[8] = {1, -1, 0, 0, 1, -1, 1, -1},
                      my[8] = {0, 0, 1, -1, 1, -1, -1, 1};

struct node {
  int num;
  node *next[26];
  node() {
    num = 0;
    for (int i = 0; i < 26; i++)
      next[i] = NULL;
  }
};

node *root;

void update(node *head, int i, int j) {
  head->num++;
  if (s[i][j] == '\\0')
    return;
  if (head->next[s[i][j] - 'A'] == NULL) {
    head->next[s[i][j] - 'A'] = new node();
  }
  update(head->next[s[i][j] - 'A'], i, j + 1);
}

void refresh(int depth) {
  if (depth > maxv) {
    int i;
    maxv = depth;
    for (i = 0; i < depth; i++)
      ans[i] = sp[i];
    ans[i] = '\\0';
  }
}

void dfs(node *head, int depth) {
  if (head == NULL)
    refresh(depth - 1);
  else if ((head->num) < 2)
    refresh(depth - 2);
  else {
    int i;
    for (i = 0; i < 26; i++) {
      sp[depth] = i + 'A';
      dfs(head->next[i], depth + 1);
    }
  }
}

void getstr(int x, int y) {
  int i, j, k, p;
  for (k = 0; k < 8; k++) {
    s[indx][0] = mp[x][y];
    i = x + mx[k];
    j = y + my[k];
    i = (i + h) % h;
    j = (j + w) % w;
    p = 1;
    while (i != x || j != y) {
      s[indx][p++] = mp[i][j];
      i += mx[k];
      j += my[k];
      i = (i + h) % h;
      j = (j + w) % w;
    }
    s[indx][p] = '\\0';
    // cout <<s[indx]<<endl;
    indx++;
  }
}

int main() {
  int i, j;
  freopen("B.out", "w", stdout);
  while (scanf("%d %d", &h, &w) != EOF && (h != 0 || w != 0)) {
    root = new node;
    indx = 0;
    maxv = -1;
    for (i = 0; i < h; i++)
      scanf("%s", mp[i]);
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++) {
        getstr(i, j);
      }
    for (i = 0; i < indx; i++)
      update(root, i, 0);
    dfs(root, 0);
    if (strlen(ans) != 1)
      printf("%s\\n", ans);
    else
      printf("0\\n");
  }
  return 0;
}