QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 32 MB Total points: 100

#336. 口吃

Statistics

最近,Bitie 患上了一种奇怪的病:他总是结巴,而且他说的唯一词汇就是数字。他的哥哥 Bytie 注意到 Bitie 的结巴有一种奇特的规律。他怀疑 Bitie 其实是在装病,以便逃学去玩电脑游戏。这对 Bytie 来说很令人沮丧,因为这妨碍了他学习编程。因此,Bytie 决心揭穿他弟弟的伪装,希望能为自己争取到尽可能多的编程时间。

让我们将 Bytie 的怀疑形式化。假设我们给定一个数字序列 $A$。

  • $A$ 的一个子序列(subsequence)是指通过从 $A$ 中删除任意元素而形成的任何序列,例如 $1, 1, 7, 5$ 是序列 $1, 3, 1, 7, 6, 6, 5, 5$ 的一个子序列。
  • $A$ 的一个“结巴序列”(stutter)是指 $A$ 的任何由连续对(即两个相等的元素)组成的子序列,例如 $1, 1, 1, 1, 3, 3$ 是序列 $1, 2, 1, 2, 1, 2, 1, 3, 3$ 的一个结巴序列。

Bytie 承诺,如果能针对 Bitie 说的两个数字序列,求出它们的最长公共结巴序列(即同时是两个序列的结巴序列的序列)的长度,他将给予奖励。

输入格式

标准输入的第一行包含两个整数 $n$ 和 $m$ ($n, m \ge 2$),由单个空格分隔,它们分别代表 Bitie 的话语序列 $A$ 和 $B$ 的长度。输入的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$,由单个空格分隔;这些是序列 $A$ 的连续元素 ($1 \le a_i \le 10^9$)。输入的第三行包含 $m$ 个整数 $b_1, b_2, \dots, b_m$,由单个空格分隔;这些是序列 $B$ 的连续元素 ($1 \le b_i \le 10^9$)。

输出格式

你的程序应向标准输出打印一个非负整数:$A$ 和 $B$ 的最长公共结巴序列的长度。如果不存在公共结巴序列(或者说它是空的),则正确答案为 $0$。

样例

样例输入 1

7 9
1 2 2 3 1 1 1
2 4 2 3 1 2 4 1 1

样例输出 1

4

说明

样例的解释:最长公共结巴序列是 $2, 2, 1, 1$。

子任务

测试集由以下具有特定属性的子集组成。在每个子集内,可能有多个测试组。

子集 属性 分值
1 $n, m \le 2000$ 30
2 $n, m \le 15\,000$ 且每个数字在每个序列中最多出现两次 28
3 $n, m \le 15\,000$ 42

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.