Happy subsequences at increasing positions in a matrix

Given a matrix M of n × m characters, a happy subsequence at increasing
positions is a triple of positions (i₁, j₁), (i₂, j₂), (i₃, j₃) such
that M[i₁][j₁]=’:’, M[i₂][j₂]=’-’, M[i₃][j₃]=’)’ and
0 ≤ i₁ < i₂ < i₃ < n and 0 ≤ j₁ < j₂ < j₃ < m.

Implement a program that reads matrices of characters from the input,
and prints the number of happy subsequences at increasing positions in
each matrix.

Input

The input has several cases. Each case begins with a line with two
positive naturals n, m. Next, there are n lines with m characters each,
chosen from {’:’,’-’,’)’}. Consecutive cases are separated by a blank
line.

Output

For each case, the program prints in one line the number of happy
subsequences at increasing positions in the input matrix.

Observation

Grading up to 10 points:

- Slow solution: 5 points.

- Fast solution: 10 points.

We understand as a fast solution one which is correct, with linear cost
and which passes the public and private tests. We understand as slow
solution one which is not fast, but it is correct and passes the public
tests.

Problem information

Author: PRO1

Generation: 2026-01-25T13:34:46.785Z

© Jutge.org, 2006–2026.
https://jutge.org
