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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

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

string s[2000], temp;

char str[30][30];

int main() {
  int n, m, i, j, x, y;
  while (scanf("%d%d", &n, &m) == 2) {
    if (!n && !m)
      break;
    for (i = 0; i < n; i++)
      scanf("%s", str[i]);
    int num = 0, l = 0, ans = 0, l1, l2, d;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        for (d = 0; d < 8; d++) {
          x = i;
          y = j;
          temp = "";
          temp += str[x][y];
          while (1) {
            x += dx[d];
            y += dy[d];
            //   printf("%d %d %d %d %d\\n",i,j,d,x,y);
            if (x >= n)
              x -= n;
            if (y >= m)
              y -= m;
            if (x < 0)
              x += n;
            if (y < 0)
              y += m;
            if (x == i && y == j)
              break;
            temp += str[x][y];
          }
          s[num++] = temp;
          //     cout<<temp<<endl;
        }
      }
    }
    sort(s, s + num);
    for (i = 1; i < num; i++) {
      l1 = s[i - 1].size();
      l2 = s[i].size();
      for (j = 0; j < l1 && j < l2; j++) {
        if (s[i - 1][j] != s[i][j])
          break;
      }
      if (j > l) {
        l = j;
        ans = i;
      }
    }
    if (l < 2)
      printf("0\\n");
    else {
      for (i = 0; i < l; i++)
        printf("%c", s[ans][i]);
      printf("\\n");
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
bool vis[30][30];
char str[30][30];
vector<string> q;
int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
map<string, int> mp;
const int maxn = 1000010;
int chd[maxn][26];
int val[maxn];
string Ans;
int sz, L;
void insert(string s) {
  int l = s.length();
  int rt = 0;
  string ss = "";
  for (int i = 0; i < l; i++) {
    int t = s[i] - 'A';
    ss += s[i];
    if (!chd[rt][t]) {
      chd[rt][t] = ++sz;
    }
    rt = chd[rt][t];
    val[rt]++;
    if (val[rt] >= 2) {
      if (i + 1 > L) {
        L = i + 1;
        Ans = ss;
      } else if (i + 1 == L) {
        if (ss < Ans)
          Ans = ss;
      }
    }
  }
}
int main() {
  int N, M;
  while (cin >> N >> M && N + M) {
    memset(val, 0, sizeof(val));
    memset(chd, 0, sizeof(chd));

    for (int i = 0; i < N; i++) {
      scanf("%s", str[i]);
    }

    Ans = "";
    sz = 0, L = 0;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++) {
        for (int k = 0; k < 8; k++) {
          memset(vis, 0, sizeof(vis));
          int x = i, y = j;
          string s = "";
          s += str[x][y];
          vis[x][y] = true;
          while (true) {
            x = ((x + dir[k][0]) % N + N) % N;
            y = ((y + dir[k][1]) % M + M) % M;
            if (vis[x][y])
              break;
            s += str[x][y];
            vis[x][y] = true;
          }
          insert(s);
        }
      }
    if (Ans.length() <= 1)
      puts("0");
    else
      cout << Ans << endl;
  }
  return 0;
}