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

#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>

using namespace std;

#define dirmax 8
int l[2];
int dir[8][2] = {{0, 1}, {1, 0},  {-1, 0}, {0, -1},
                 {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
string ans;

void search(set<string> *v, int pos[2], char ch[10][20]) {

  for (int i = 0; i < dirmax; i++) {
    string str;
    int cpos[] = {pos[0], pos[1]};

    str += ch[pos[0]][pos[1]];

    while (true) {
      for (int j = 0; j < 2; j++) {
        cpos[j] += dir[i][j];
        if (cpos[j] < 0)
          cpos[j] = l[j] - 1;
        if (cpos[j] >= l[j])
          cpos[j] = 0;
      }
      if (cpos[0] == pos[0] && cpos[1] == pos[1])
        break;
      str += ch[cpos[0]][cpos[1]];

      if (v->find(str) != v->end()) {
        if ((str.length() > ans.length()) ||
            (str.length() == ans.length() && str < ans)) {
          ans = str;
        }
      }
      v->insert(str);
    }
  }
}

int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);
  while (1) {
    cin >> l[0] >> l[1];
    if (l[0] == 0 && l[1] == 0)
      break;
    char donut[10][20];
    ans = "";

    set<string> strs;

    // 入力を取得する
    for (int i = 0; i < l[0]; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < l[1]; j++) {
        donut[i][j] = str[j];
      }
    }

    for (int i = 0; i < l[0]; i++) {
      for (int j = 0; j < l[1]; j++) {
        int pos[2];
        pos[0] = i;
        pos[1] = j;
        search(&strs, pos, donut);
      }
    }

    if (ans == "")
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  // while(1){}
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define dirmax 8
int l[2];
int dir[8][2] = {{0, 1}, {1, 0},  {-1, 0}, {0, -1},
                 {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};

void search(vector<string> *v, int pos[2], char ch[10][20]) {

  for (int i = 0; i < dirmax; i++) {
    string str;
    int cpos[] = {pos[0], pos[1]};

    str += ch[pos[0]][pos[1]];
    v->push_back(str);

    while (true) {
      for (int j = 0; j < 2; j++) {
        cpos[j] += dir[i][j];
        if (cpos[j] < 0)
          cpos[j] = l[j] - 1;
        if (cpos[j] >= l[j])
          cpos[j] = 0;
      }
      if (cpos[0] == pos[0] && cpos[1] == pos[1])
        break;
      str += ch[cpos[0]][cpos[1]];
      v->push_back(str);
    }
  }
}

int main(void) {
  // FILE* fp_in = freopen("data.txt", "r", stdin);

  while (1) {
    cin >> l[0] >> l[1];
    if (l[0] == 0 && l[1] == 0)
      break;
    char donut[10][20];

    vector<string> strs;

    // 入力を取得する
    for (int i = 0; i < l[0]; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < l[1]; j++) {
        donut[i][j] = str[j];
      }
    }

    for (int i = 0; i < l[0]; i++) {
      for (int j = 0; j < l[1]; j++) {
        int pos[2];
        pos[0] = i;
        pos[1] = j;
        search(&strs, pos, donut);
      }
    }
    sort(strs.begin(), strs.end());

    string ans = "";
    for (int i = 0; i < strs.size(); i++) {
      for (int j = i + 1; j < strs.size(); j++) {
        if (strs[i] == strs[j] && ans.length() < strs[i].length() &&
            strs[i].length() > 1) {
          ans = strs[i];
        }
      }
    }
    if (ans == "")
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
  // while(1){}
  return 0;
}