QOJ.ac

QOJ

Time Limit: 1.5 s Memory Limit: 64 MB Total points: 20

#23774. 公共子串

Statistics

题目描述

给定 $2$ 个字符串,请编写程序找出同时包含在这两个字符串中的最长字符串,并回答其长度。

这里,称字符串 $s$ 包含在字符串 $t$ 中,是指 $s$ 在 $t$ 中连续出现。空字符串,即长度为 $0$ 的字符串,包含在任何字符串中。例如,字符串 ABRACADABRA 中包含以下字符串:ABRA、RAC、D、ACADABRA、ABRACADABRA、空字符串等。另一方面,字符串 ABRACADABRA 中不包含以下字符串:ABRC、RAA、BA、K 等。

示例

例 $1$:当给定字符串 ABRACADABRA 与 ECADADABRBCRDARA 时,同时包含在两者中的字符串有 CA、CADA、ADABR、空字符串等。其中最长的是 ADABR,其长度为 $5$。包含在 $2$ 个字符串中的 ADABR 的位置如图 $2\text{-}1$ 所示。

$$\begin{aligned} \texttt{ABRAC}&\underline{\texttt{ADABR}}\texttt{A} \\ \texttt{ECAD}&\underline{\texttt{ADABR}}\texttt{BCRDARA} \end{aligned}$$

图 $2\text{-}1$:例 $1$ 的字符串中出现的 ADABR 的位置

例 $2$:当给定字符串 UPWJCIRUCAXIIRGL 与 SBQNYBSBZDFNEV 时,同时包含在两者中的字符串只有空字符串,其长度为 $0$。

输入格式

输入由 $2$ 行组成,第 $1$ 行给出第 $1$ 个字符串,第 $2$ 行给出第 $2$ 个字符串。字符串由大写英文字母组成,每个字符串的长度均在 $1$ 以上 $4\,000$ 以下。

在评测数据中,占总分 $30\%$ 的数据满足每个字符串的长度均在 $1$ 以上 $50$ 以下。

输出格式

输出仅包含 $1$ 行,为同时包含在给定的 $2$ 个字符串中的最长字符串的长度。

样例输入与输出

与上述示例对应的输入输出如下。

样例输入 1

ABRACADABRA
ECADADABRBCRDARA

样例输出 1

5

样例输入 2

UPWJCIRUCAXIIRGL
SBQNYBSBZDFNEV

样例输出 2

0

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.