QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#7051. 最大公共子矩阵

统计

给定两个 $n \times m$ 的矩阵,其中每个矩阵的元素范围均为 $1$ 到 $n \times m$ 且两两不同。你需要找到这两个矩阵之间面积最大的公共子矩阵。

示例: 矩阵 $A$: $$ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 8 & 7 & 9 \end{matrix} $$ 矩阵 $B$: $$ \begin{matrix} 5 & 6 & 1 \\ 7 & 9 & 3 \\ 2 & 4 & 8 \end{matrix} $$ 最大的公共子矩阵: $$ \begin{matrix} 5 & 6 \\ 7 & 9 \end{matrix} $$

输入格式

第一行包含两个整数 $n$ ($1 \le n \le 1000$) 和 $m$ ($1 \le m \le 1000$),表示每个矩阵的行数和列数。

接下来的 $n$ 行,每行包含 $m$ 个整数,表示第一个矩阵 $A = (a_{i,j})_{n \times m}$。再接下来的 $n$ 行,每行包含 $m$ 个整数,表示第二个矩阵 $B = (b_{i,j})_{n \times m}$。

保证 $1 \le a_{i,j}, b_{i,j} \le n \times m$,且对于任意两对坐标 $(i_1, j_1)$ 和 $(i_2, j_2)$,当 $i_1 \neq i_2$ 或 $j_1 \neq j_2$ 时,$a_{i_1,j_1} \neq a_{i_2,j_2}$ 且 $b_{i_1,j_1} \neq b_{i_2,j_2}$ 恒成立。

输出格式

输出一个整数,表示最大的公共子矩阵的大小(即元素个数)。

样例

样例输入 1

3 4
5 6 7 8
1 2 3 4
9 10 11 12
5 6 8 7
1 2 4 3
12 11 10 9

样例输出 1

4

说明

样例测试中的最大公共子矩阵为: $$ \begin{matrix} 5 & 6 \\ 1 & 2 \end{matrix} $$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.