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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int m, n;
vector<P> dat[100];
vector<int> xind;
vector<int> yind;
int field[280][280];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int pickl[4];
int picklx[4];
int pickly[4];

int main(void) {
  scanf("%d %d", &n, &m);
  memset(field, 0, sizeof(field));
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      if (str[j] >= 'A' && str[j] <= 'Z') {
        dat[str[j] - 'A' + 1].push_back(P(j, i));
        if (j - 1 >= 0)
          xind.push_back(j - 1);
        xind.push_back(j);
        if (j + 1 < m)
          xind.push_back(j + 1);
        if (i - 1 >= 0)
          yind.push_back(i - 1);
        yind.push_back(i);
        if (i + 1 < n)
          yind.push_back(i + 1);
      }
    }
  }
  sort(xind.begin(), xind.end());
  xind.erase(unique(xind.begin(), xind.end()), xind.end());
  sort(yind.begin(), yind.end());
  yind.erase(unique(yind.begin(), yind.end()), yind.end());
  for (int i = 1; i <= 26; i++) {
    for (int j = 0; j < dat[i].size(); j++) {
      dat[i][j].first =
          lower_bound(xind.begin(), xind.end(), dat[i][j].first) - xind.begin();
      dat[i][j].second =
          lower_bound(yind.begin(), yind.end(), dat[i][j].second) -
          yind.begin();
      field[dat[i][j].first][dat[i][j].second] = i;
    }
  }
  bool flag = true;
  int score2 = 0;
  while (flag) {
    flag = false;
    for (int i = 0; i < yind.size(); i++) {
      for (int j = 0; j < xind.size(); j++) {
        if (field[j][i] == 0) {
          memset(pickl, 0, sizeof(pickl));
          for (int k = 0; k < 4; k++) {
            int xx = j, yy = i;
            int xi = xind.size(), yi = yind.size();
            while (xx >= 0 && xx <= xi && yy >= 0 && yy <= yi) {
              if (field[xx][yy] != 0) {
                pickl[k] = field[xx][yy];
                picklx[k] = xx;
                pickly[k] = yy;
                break;
              }
              xx += dx[k];
              yy += dy[k];
            }
          }
          for (int k = 0; k < 4; k++) {
            for (int l = k + 1; l < 4; l++) {
              if (pickl[k] == pickl[l] && pickl[k] != 0) {
                score2 += 2;
                field[picklx[k]][pickly[k]] = 0;
                field[picklx[l]][pickly[l]] = 0;
                flag = true;
              }
            }
          }
        }
      }
    }
  }
  printf("%d\\n", score2);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int m, n;
vector<P> data[30];
vector<int> xindex;
vector<int> yindex;
int field[180][180];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int pickl[4];
int picklx[4];
int pickly[4];

int main(void) {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      if (str[j] >= 'A' && str[j] <= 'Z') {
        data[str[j] - 'A' + 1].push_back(P(j, i));
        if (j - 1 >= 0)
          xindex.push_back(j - 1);
        xindex.push_back(j);
        if (j + 1 < m)
          xindex.push_back(j + 1);
        if (i - 1 >= 0)
          yindex.push_back(i - 1);
        yindex.push_back(i);
        if (i + 1 < n)
          yindex.push_back(i + 1);
      }
    }
  }
  sort(xindex.begin(), xindex.end());
  xindex.erase(unique(xindex.begin(), xindex.end()), xindex.end());
  sort(yindex.begin(), yindex.end());
  yindex.erase(unique(yindex.begin(), yindex.end()), yindex.end());
  for (int i = 1; i <= 26; i++) {
    for (int j = 0; j < data[i].size(); j++) {
      data[i][j].first =
          lower_bound(xindex.begin(), xindex.end(), data[i][j].first) -
          xindex.begin();
      data[i][j].second =
          lower_bound(yindex.begin(), yindex.end(), data[i][j].second) -
          yindex.begin();
      field[data[i][j].first][data[i][j].second] = i;
    }
  }
  bool flag = true;
  int score = 0;
  while (flag) {
    flag = false;
    for (int i = 0; i < yindex.size(); i++) {
      for (int j = 0; j < xindex.size(); j++) {
        if (field[j][i] == 0) {
          memset(pickl, 0, sizeof(pickl));
          for (int k = 0; k < 4; k++) {
            int xx = j, yy = i;
            int xi = xindex.size(), yi = yindex.size();
            while (xx >= 0 && xx < xi && yy >= 0 && yy < yi) {
              if (field[xx][yy] != 0)
                break;
              xx += dx[k];
              yy += dy[k];
            }
            if (field[xx][yy] != 0) {
              pickl[k] = field[xx][yy];
              picklx[k] = xx;
              pickly[k] = yy;
            }
          }
          for (int k = 0; k < 4; k++) {
            for (int l = k + 1; l < 4; l++) {
              if (pickl[k] == pickl[l] && pickl[k] != 0) {
                score += 2;
                field[picklx[k]][pickly[k]] = 0;
                field[picklx[l]][pickly[l]] = 0;
                flag = true;
              }
            }
          }
        }
      }
    }
  }
  printf("%d\\n", score);
  return 0;
}