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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const vector<int> dir = {0, 1, 0, -1};
int H, W;
int main() {
  cin >> H >> W;
  vector<string> s(H);
  vector<int> cx, cy;
  for (int i = 0; i < H; i++) {
    cin >> s[i];
    for (int j = 0; j < W; j++) {
      if (s[i][j] != '.') {
        cx.push_back(j);
        cy.push_back(i);
      }
    }
  }
  sort(cx.begin(), cx.end());
  cx.erase(unique(cx.begin(), cx.end()), cx.end());
  int rx = cx.size();
  for (int i = 1; i < rx; i++) {
    if (cx[i] - cx[i - 1] >= 2)
      cx.push_back(cx[i - 1] + 1);
  }
  sort(cx.begin(), cx.end());
  sort(cy.begin(), cy.end());
  cy.erase(unique(cy.begin(), cy.end()), cy.end());
  int ry = cy.size();
  for (int i = 1; i < ry; i++) {
    if (cy[i] - cy[i - 1] >= 2)
      cy.push_back(cy[i - 1] + 1);
  }
  sort(cy.begin(), cy.end());
  H = cy.size(), W = cx.size();
  vector<string> t(H, string(W, '.'));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[cy[i]][cx[j]] != '.')
        t[i][j] = s[cy[i]][cx[j]];
    }
  }
  int ret = 0;
  while (true) {
    bool flag = false;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (t[i][j] != '.')
          continue;
        vector<vector<pair<int, int>>> d(26);
        for (int k = 0; k < 4; k++) {
          int tx = j, ty = i;
          while (0 <= tx && tx < W && 0 <= ty && ty < H) {
            if (t[ty][tx] != '.') {
              d[t[ty][tx] - 65].push_back(make_pair(tx, ty));
              break;
            }
            tx += dir[k];
            ty += dir[k ^ 1];
          }
        }
        for (int k = 0; k < 26; k++) {
          if (d[k].size() == 2) {
            flag = true;
            ret += 2;
            t[d[k][0].second][d[k][0].first] = '.';
            t[d[k][1].second][d[k][1].first] = '.';
          }
        }
      }
    }
    if (!flag)
      break;
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const vector<int> dir = {0, 1, 0, -1};
int H, W;
int main() {
  cin >> H >> W;
  vector<string> s(H);
  vector<int> cx, cy;
  for (int i = 0; i < H; i++) {
    cin >> s[i];
    for (int j = 0; j < W; j++) {
      if (s[i][j] != '.') {
        cx.push_back(j);
        cy.push_back(i);
      }
    }
  }
  sort(cx.begin(), cx.end());
  cx.erase(unique(cx.begin(), cx.end()), cx.end());
  for (int i = 1; i < cx.size(); i++) {
    if (cx[i] - cx[i - 1] >= 2)
      cx.push_back(cx[i - 1] + 1);
  }
  sort(cx.begin(), cx.end());
  sort(cy.begin(), cy.end());
  cy.erase(unique(cy.begin(), cy.end()), cy.end());
  for (int i = 1; i < cy.size(); i++) {
    if (cy[i] - cy[i - 1] >= 2)
      cy.push_back(cy[i - 1] + 1);
  }
  sort(cy.begin(), cy.end());
  H = cy.size(), W = cx.size();
  vector<string> t(H, string(W, '.'));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[cy[i]][cx[j]] != '.')
        t[i][j] = s[cy[i]][cx[j]];
    }
  }
  int ret = 0;
  while (true) {
    bool flag = false;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (t[i][j] != '.')
          continue;
        vector<vector<pair<int, int>>> d(26);
        for (int k = 0; k < 4; k++) {
          int tx = j, ty = i;
          while (0 <= tx && tx < W && 0 <= ty && ty < H) {
            if (t[ty][tx] != '.') {
              d[t[ty][tx] - 65].push_back(make_pair(tx, ty));
              break;
            }
            tx += dir[k];
            ty += dir[k ^ 1];
          }
        }
        for (int k = 0; k < 26; k++) {
          if (d[k].size() == 2) {
            flag = true;
            ret += 2;
            t[d[k][0].second][d[k][0].first] = '.';
            t[d[k][1].second][d[k][1].first] = '.';
          }
        }
      }
    }
    if (!flag)
      break;
  }
  cout << ret << endl;
  return 0;
}