#abc315d. D - Magical Cookies
D - Magical Cookies
Score : points
问题描述
有 块饼干排列成 行和 列。 从上到下第 行、从左到右第 列的饼干的颜色用小写英文字母 表示。
我们将执行以下步骤:
- 对于每一行,执行以下操作:若该行剩余两个或更多饼干且它们颜色相同,则标记这些饼干。
- 对于每一列,执行以下操作:若该列剩余两个或更多饼干且它们颜色相同,则标记这些饼干。
- 若存在被标记的饼干,移除所有被标记的饼干并返回步骤 1;否则,终止该过程。
请找出该过程结束后剩余的饼干数量。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
There are cookies in rows and columns.
The color of the cookie at the -row from the top and -th column from the left is represented by a lowercase English letter .
We will perform the following procedure.
1. For each row, perform the following operation: if there are two or more cookies remaining in the row and they all have the same color, mark them.
2. For each column, perform the following operation: if there are two or more cookies remaining in the column and they all have the same color, mark them.
3. If there are any marked cookies, remove them all and return to 1; otherwise, terminate the procedure.
Find the number of cookies remaining at the end of the procedure.
Constraints
- is a lowercase English letter.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4 3
aaa
aaa
abc
abd
Sample Output 1
2
The procedure is performed as follows.
- 1. Mark the cookies in the first and second rows.
- 2. Mark the cookies in the first column.
- 3. Remove the marked cookies.
At this point, the cookies look like the following, where .
indicates a position where the cookie has been removed.
...
...
.bc
.bd
- 1. Do nothing.
- 2. Mark the cookies in the second column.
- 3. Remove the marked cookies.
At this point, the cookies look like the following, where .
indicates a position where the cookie has been removed.
...
...
..c
..d
- 1. Do nothing.
- 2. Do nothing.
- 3. No cookies are marked, so terminate the procedure.
The final number of cookies remaining is .
Sample Input 2
2 5
aaaaa
abcde
Sample Output 2
4
Sample Input 3
3 3
ooo
ooo
ooo
Sample Output 3
0
update @ 2024/3/10 09:00:46