Problem p03014 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const LL MOD = 1000000007;

const int N = 2005;
char board[N][N];
int xx[N][N], yy[N][N];

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  REP(i, h) scanf("%s", &board[i + 1][1]);
  REP(i, h + 2) board[i][0] = board[i][w + 1] = '#';
  REP(i, w + 2) board[0][i] = board[h + 1][i] = '#';
  FOR(i, 1, h + 1) {
    FOR(j, 1, w + 1) {
      int sx = 1;
      if (xx[i][j] == 0) {
        while (sx <= w) {
          int tx = sx;
          while (board[i][tx] == '.')
            tx++;
          FOR(k, sx, tx) xx[i][k] = tx - sx;
          sx = tx + 1;
        }
      }
      int sy = 1;
      if (yy[i][j] == 0) {
        while (sy <= h) {
          int ty = sy;
          while (board[ty][j] == '.')
            ty++;
          FOR(k, sy, ty) yy[k][j] = ty - sy;
          sy = ty + 1;
        }
      }
    }
  }
  int ret = 0;
  FOR(i, 1, h + 1) FOR(j, 1, w + 1) ret = max(ret, xx[i][j] + yy[i][j] - 1);
  printf("%d\n", ret);
  return 0;
}

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)

const LL MOD = 1000000007;

const int N = 2005;
char board[N][N];
int xx[N][N], yy[N][N];

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  REP(i, h) scanf("%s", &board[i + 1][1]);
  REP(i, h + 2) board[i][0] = board[i][w + 1] = '#';
  REP(i, w + 2) board[0][i] = board[h + 1][i] = '#';
  FOR(i, 1, h + 1) {
    int sx = 1;
    while (sx <= w) {
      int tx = sx;
      while (board[i][tx] == '.')
        tx++;
      FOR(k, sx, tx) xx[i][k] = tx - sx;
      sx = tx + 1;
    }
  }
  FOR(i, 1, w + 1) {
    int sy = 1;
    while (sy <= h) {
      int ty = sy;
      while (board[ty][i] == '.')
        ty++;
      FOR(k, sy, ty) yy[k][i] = ty - sy;
      sy = ty + 1;
    }
  }
  int ret = 0;
  FOR(i, 1, h + 1) FOR(j, 1, w + 1) ret = max(ret, xx[i][j] + yy[i][j] - 1);
  printf("%d\n", ret);
  return 0;
}