Problem p04026 (C++)

Buggy submission (status: Time Limit Exceeded):
// OM
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define x first
#define y second
#define pb push_back
#define mp make_pair
#define PI (acos(-1.0))
#define mem(a, b) memset(a, b, sizeof(a))
#define Sort(x) sort(x.begin(), x.end())
#define FOR(i, b, e) for (int i = b; i <= (int)(e); i++)
#define FORR(i, b, e) for (int i = b; i >= (int)(e); i--)
#define FORI(i, s)                                                             \
  for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i)
#define pr(x) cout << x << "\n"
#define prs(x) cout << x << " "
#define pr2(x, y) cout << x << " " << y << "\n"
#define pr3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define ppr(a) cout << a.x << " " << a.y << "\n"

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpii;

// int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1};
// int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

#define EPS 1e-9
#define MAX 100007
char s[MAX];
vi a[50];
int main() {
  int T, n;
  scanf("%s", s);
  int len = strlen(s);
  FOR(i, 0, len - 1) { a[s[i] - 'a'].pb(i); }
  //    FOR(i,0,len-1){
  //
  //        FOR(c,0,25)
  //        {
  //            int s=-1;
  //            FOR(k,0,a[c].size()-1)
  //            {
  //                if(a[c][k]<k)continue;
  //                else{
  //                    if(s==-1)s=a[c][k];
  //                }
  //            }
  //        }
  //    }
  FOR(c, 0, 25) {
    int s = -1;
    FOR(i, 0, a[c].size() - 1) {
      FOR(j, i, a[c].size() - 1) {
        int cha = j - i + 1;
        int str = a[c][j] - a[c][i] + 1;
        if (cha > str / 2 && cha >= 2) {
          printf("%d %d", a[c][i] + 1, a[c][j] + 1);
          return 0;
        }
      }
    }
  }
  printf("-1 -1");
  return 0;
}


Fixed submission:
// OM
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define x first
#define y second
#define pb push_back
#define mp make_pair
#define PI (acos(-1.0))
#define mem(a, b) memset(a, b, sizeof(a))
#define Sort(x) sort(x.begin(), x.end())
#define FOR(i, b, e) for (int i = b; i <= (int)(e); i++)
#define FORR(i, b, e) for (int i = b; i >= (int)(e); i--)
#define FORI(i, s)                                                             \
  for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i)
#define pr(x) cout << x << "\n"
#define prs(x) cout << x << " "
#define pr2(x, y) cout << x << " " << y << "\n"
#define pr3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define ppr(a) cout << a.x << " " << a.y << "\n"

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpii;

// int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1};
// int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction

#define EPS 1e-9
#define MAX 100007
char s[MAX];
vi a[50];
int main() {
  int T, n;
  scanf("%s", s);
  int len = strlen(s);
  FOR(i, 0, len - 1) { a[s[i] - 'a'].pb(i); }
  //    FOR(i,0,len-1){
  //
  //        FOR(c,0,25)
  //        {
  //            int s=-1;
  //            FOR(k,0,a[c].size()-1)
  //            {
  //                if(a[c][k]<k)continue;
  //                else{
  //                    if(s==-1)s=a[c][k];
  //                }
  //            }
  //        }
  //    }
  FOR(c, 0, 25) {
    int s = -1;
    FOR(i, 0, a[c].size() - 2) {
      int j = i + 1;
      int cha = j - i + 1;
      int str = a[c][j] - a[c][i] + 1;
      if (cha > str / 2 && cha >= 2) {
        printf("%d %d", a[c][i] + 1, a[c][j] + 1);
        return 0;
      }
    }
  }
  printf("-1 -1");
  return 0;
}