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.