QOJ.ac

QOJ

Límite de tiempo: 10 s Límite de memoria: 512 MB Puntuación total: 100

#176. 隐藏的变位词

Estadísticas

“Anagram” 是指通过重排另一个词或短语的字母而形成的词或短语。例如,通过重排 “William Shakespeare” 的字母,我们可以得到它的变位词 “I am a weakish speller”、“I’ll make a wise phrase” 等。注意,当 $A$ 是 $B$ 的变位词时,$B$ 也是 $A$ 的变位词。

在上述例子中,字母的大小写被忽略,且可以自由插入或删除单词空格和标点符号。但这些规则在此处并不适用;我们只考虑字母的精确匹配。

对于两个字符串 $s_1$ 和 $s_2$,如果 $s_1$ 的一个子串 $s_1'$ 是 $s_2$ 的一个子串 $s_2'$ 的变位词,我们称 $s_1'$ 为这两个字符串 $s_1$ 和 $s_2$ 的一个“隐藏变位词”。当然,$s_2'$ 也是它们的一个隐藏变位词。

你的任务是编写一个程序,对于给定的两个字符串,计算它们最长隐藏变位词的长度。

例如,假设给定 “anagram” 和 “grandmother”。它们的子串 “nagr” 和 “gran” 是隐藏变位词,因为通过移动字母,你可以从一个得到另一个。它们是最长的,因为 “grandmother” 中任何长度为 5 或以上的子串都必须包含 “d” 或 “o”,而 “anagram” 中没有这些字母。因此,在这种情况下,最长隐藏变位词的长度为 4。注意,子串必须是原字符串中连续出现的字母序列,因此 “nagrm” 和 “granm” 不是隐藏变位词。

输入格式

输入包含两行,表示一个测试用例。

$s_1$ $s_2$

$s_1$ 和 $s_2$ 是由小写字母(a 到 z)组成的字符串,它们的长度在 1 到 4000 之间(包含 1 和 4000)。

输出格式

输出 $s_1$ 和 $s_2$ 的最长隐藏变位词的长度。如果没有隐藏变位词,则输出 0。

样例

样例输入 1

anagram
grandmother

样例输出 1

4

样例输入 2

williamshakespeare
iamaweakishspeller

样例输出 2

18

样例输入 3

aaaaaaaabbbbbbbb
xxxxxabababxxxxxabab

样例输出 3

6

样例输入 4

abababacdcdcd
efefefghghghghgh

样例输出 4

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.