Hossam and a Letter (Codeforces 1771E)
Tags: Implement, two pointers

Hossam bought a new piece of ground with length $$$n$$$ and width $$$m$$$, he divided it into an $$$n \cdot m$$$ grid, each cell being of size $$$1\times1$$$.Since Hossam's name starts with the letter 'H', he decided to draw the capital letter 'H' by building walls of size $$$1\times1$$$ on some squares of the ground. Each square $$$1\times1$$$ on the ground is assigned a quality degree: perfect, medium, or bad.The process of building walls to form up letter 'H' has the following constraints:  The letter must consist of one horizontal and two vertical lines.  The vertical lines must not be in the same or neighboring columns.  The vertical lines must start in the same row and end in the same row (and thus have the same length).  The horizontal line should connect the vertical lines, but must not cross them.  The horizontal line can be in any row between the vertical lines (not only in the middle), except the top and the bottom one. (With the horizontal line in the top row the letter looks like 'n', and in the bottom row like 'U'.)  It is forbidden to build walls in cells of bad quality.  You can use at most one square of medium quality.  You can use any number of squares of perfect quality. Find the maximum number of walls that can be used to draw the letter 'H'.Check the note for more clarification.

Input: The first line of the input contains two integer numbers $$$n$$$, $$$m$$$ ($$$1 \le n, m \le 400$$$).The next $$$n$$$ lines of the input contain $$$m$$$ characters each, describing the grid. The character '.' stands for a perfect square, the character 'm' stands for a medium square, and the character '#' stands for a bad square.

Output: Print a single integer — the maximum number of walls that form a capital letter 'H'.If it is not possible to draw any letter 'H', print $$$0$$$.

Note: In the first test case, we can't build the letter 'H'.For the second test case, the figure below represents the grid and some of the valid letters 'H'. Perfect, medium, and bad squares are represented with white, yellow, and black colors respectively.  

Sample Inputs:
2 3
#m.
.#.

7 8
...#.m..
..m...m.
.#..#.m#
...m..m.
m.......
..#.m.mm
......m.

Sample Outputs:
0

16

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * (m + 1) + v
(n, m) = map(int, input().split())
z = (n + 2) * (m + 1)
x = [0] * z
for i in range(n):
    x0 = list(input().rstrip())
    for j in range(m):
        u = x0[j]
        if u & 1:
            x[f(i + 1, j + 1)] = (min(u ^ 35, 1) ^ 1) + 1
y = list(x)
for i in range(1, z):
    y[i] += y[i - 1]
ans = 0
cnt = [0] * (n + 2)
(cnt[0], cnt[n + 1]) = (2, 2)
(l0, r0) = ([0] * (n + 2), [0] * (n + 2))
for l in range(1, m + 1):
    for r in range(l + 2, m + 1):
        for i in range(1, n + 1):
            cnt[i] = x[f(i, l)] + x[f(i, r)]
        c = 0
        for i in range(1, n + 1):
            c = 0 if cnt[i] else c + 1
            l0[i] = c
        c = 0
        for i in range(n, 0, -1):
            c = 0 if cnt[i] else c + 1
            r0[i] = c
        for i in range(2, n):
            c = y[f(i, r)] - y[f(i, l - 1)]
            if c >= 2:
                continue
            (fl, fr) = (l0[i - 1], r0[i + 1])
            ans0 = r - l + 1 + 2 * (l0[i - 1] + r0[i + 1])
            if fl and fr:
                ans = max(ans, ans0)
            if not c:
                if cnt[i - 1 - l0[i - 1]] == 1 and fr:
                    ans = max(ans, ans0 + 2 * l0[i - 2 - l0[i - 1]] + 2)
                if cnt[i + 1 + r0[i + 1]] == 1 and fl:
                    ans = max(ans, ans0 + 2 * r0[i + 2 + r0[i + 1]] + 2)
print(ans)

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * (m + 1) + v
(n, m) = map(int, input().split())
z = (n + 2) * (m + 1)
x = [0] * z
for i in range(n):
    x0 = list(input().rstrip())
    for j in range(m):
        u = x0[j]
        if u & 1:
            x[f(i + 1, j + 1)] = (min(u ^ 35, 1) ^ 1) + 1
y = list(x)
for i in range(1, z):
    y[i] += y[i - 1]
ans = 0
cnt = [0] * (n + 2)
(cnt[0], cnt[n + 1]) = (2, 2)
(l0, r0) = ([0] * (n + 2), [0] * (n + 2))
for l in range(1, m + 1):
    for r in range(l + 2, m + 1):
        for i in range(1, n + 1):
            cnt[i] = x[f(i, l)] + x[f(i, r)]
        c = 0
        for i in range(1, n + 1):
            c = 0 if cnt[i] else c + 1
            l0[i] = c
        c = 0
        for i in range(n, 0, -1):
            c = 0 if cnt[i] else c + 1
            r0[i] = c
        for i in range(2, n):
            c = y[f(i, r)] - y[f(i, l - 1)]
            if c >= 2:
                continue
            (fl, fr) = (l0[i - 1], r0[i + 1])
            ans0 = r - l + 1 + 2 * (l0[i - 1] + r0[i + 1])
            if fl and fr:
                ans = max(ans, ans0)
            if not c:
                if cnt[i - 1 - l0[i - 1]] == 1 and fr:
                    ans = max(ans, ans0 + 2 * l0[i - 2 - l0[i - 1]] + 2)
                if cnt[i + 1 + r0[i + 1]] == 1 and fl:
                    ans = max(ans, ans0 + 2 * r0[i + 2 + r0[i + 1]] + 2)
print(ans)

Solution (Python):
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def f(u, v):
    return u * (m + 1) + v
(n, m) = map(int, input().split())
z = (n + 2) * (m + 1)
x = [0] * z
for i in range(n):
    x0 = list(input().rstrip())
    for j in range(m):
        u = x0[j]
        if u & 1:
            x[f(i + 1, j + 1)] = (min(u ^ 35, 1) ^ 1) + 1
y = list(x)
for i in range(1, z):
    y[i] += y[i - 1]
ans = 0
cnt = [0] * (n + 2)
(cnt[0], cnt[n + 1]) = (2, 2)
(l0, r0) = ([0] * (n + 2), [0] * (n + 2))
for l in range(1, m + 1):
    for r in range(l + 2, m + 1):
        for i in range(1, n + 1):
            cnt[i] = x[f(i, l)] + x[f(i, r)]
        c = 0
        for i in range(1, n + 1):
            c = 0 if cnt[i] else c + 1
            l0[i] = c
        c = 0
        for i in range(n, 0, -1):
            c = 0 if cnt[i] else c + 1
            r0[i] = c
        for i in range(2, n):
            c = y[f(i, r)] - y[f(i, l - 1)]
            if c >= 2:
                continue
            (fl, fr) = (l0[i - 1], r0[i + 1])
            ans0 = r - l + 1 + 2 * (l0[i - 1] + r0[i + 1])
            if fl and fr:
                ans = max(ans, ans0)
            if not c:
                if cnt[i - 1 - l0[i - 1]] == 1 and fr:
                    ans = max(ans, ans0 + 2 * l0[i - 2 - l0[i - 1]] + 2)
                if cnt[i + 1 + r0[i + 1]] == 1 and fl:
                    ans = max(ans, ans0 + 2 * r0[i + 2 + r0[i + 1]] + 2)
print(ans)