CF164B.Ancient Berland Hieroglyphs
普及+/提高
通过率:0%
时间限制:1.50s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them).
Polycarpus wants to save these pictures on his laptop, but the problem is, laptops do not allow to write hieroglyphs circles. So Polycarp had to break each circle and write down all of its hieroglyphs in a clockwise order in one line. A line obtained from the first circle will be called a, and the line obtained from the second one will be called b.
There are quite many ways to break hieroglyphic circles, so Polycarpus chooses the method, that makes the length of the largest substring of string a, which occurs as a subsequence in string b, maximum.
Help Polycarpus — find the maximum possible length of the desired substring (subsequence) if the first and the second circles are broken optimally.
The length of string s is the number of characters in it. If we denote the length of string s as |s|, we can write the string as s = _s_1_s_2... s|s|.
A substring of s is a non-empty string x = s[a... b] = s__a__s__a + 1... s__b (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings of "codeforces", while "coders" is not.
A subsequence of s is a non-empty string y = s[_p_1_p_2... p|y|] = _s__p_1_s__p_2... s__p|y| (1 ≤ _p_1 < _p_2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces".
波利卡普斯喜欢研究贝尔兰象形文字。有一次,波利卡普斯得到了两幅古老的贝尔兰图画,每幅图上都画有一个象形文字圆环。我们知道:在第一个圆环中,没有任何象形文字出现两次;在第二个圆环中,同样没有任何象形文字出现两次(但某个象形文字可能在两个圆环中各出现一次)。
波利卡普斯想将这两幅图画保存到他的笔记本电脑上,但问题在于,笔记本电脑无法直接存储象形文字圆环。因此,波利卡普斯不得不将每个圆环从某处断开,并按顺时针顺序将其中所有象形文字写成一行字符串。由第一个圆环得到的字符串记为 a,由第二个圆环得到的字符串记为 b。
由于断开圆环的位置有多种选择,波利卡普斯将选择一种断开方式,使得字符串 a 的某个子串(即连续子序列)在字符串 b 中作为子序列(subsequence)出现的最大长度达到最大。
请帮助波利卡普斯——当两个圆环均被最优地断开时,求出该最大可能长度(即 a 的某个子串作为 b 的子序列所能达到的最大长度)。
字符串 s 的长度指其包含的字符个数。若记字符串 s 的长度为 ∣s∣,则可将 s 表示为 s=s1s2…s∣s∣。
s 的一个子串(substring)是指一个非空字符串 x=s[a…b]=sasa+1…sb,其中 1≤a≤b≤∣s∣。例如,“code”和“force”都是“codeforces”的子串,而“coders”不是。
s 的一个子序列(subsequence)是指一个非空字符串 y=s[p1p2…p∣y∣]=sp1sp2…sp∣y∣,其中 1≤p1<p2<⋯<p∣y∣≤∣s∣。例如,“coders”是“codeforces”的一个子序列。
输入格式
The first line contains two integers l__a and l__b (1 ≤ l__a, l__b ≤ 1000000) — the number of hieroglyphs in the first and second circles, respectively.
Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106.
The second line contains l__a integers — the hieroglyphs in the first picture, in the clockwise order, starting with one of them.
The third line contains l__b integers — the hieroglyphs in the second picture, in the clockwise order, starting with one of them.
It is guaranteed that the first circle doesn't contain a hieroglyph, which occurs twice. The second circle also has this property.
第一行包含两个整数 la 和 lb(1≤la,lb≤1000000),分别表示第一个和第二个圆圈中象形文字的数量。
由于 Berland 象形文字的编码存在困难,以下将它们表示为 1 到 106 之间的整数。
第二行包含 la 个整数——第一个图中的象形文字,按顺时针顺序给出,起始位置为其中某一个。
第三行包含 lb 个整数——第二个图中的象形文字,按顺时针顺序给出,起始位置为其中某一个。
保证第一个圆圈中不包含重复出现的象形文字;第二个圆圈同样满足该性质。
输出格式
Print a single number — the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0.
输出一个整数——公共子串与公共子序列的最大长度。如果以任意方式断开圆环后均不存在,则输出 0。
输入输出样例
输入#1
5 4 1 2 3 4 5 1 3 5 6
输出#1
2
输入#2
4 6 1 3 5 2 1 2 3 4 5 6
输出#2
3
输入#3
3 3 1 2 3 3 2 1
输出#3
2
说明/提示
In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample — from hieroglyphs 1, 3 and 5.
在第一个测试用例中,Polycarpus 选择了一个仅由象形文字 5 和 1 组成的字符串;而在第二个样例中,他选择了一个仅由象形文字 1、3 和 5 组成的字符串。
输入解题思路,AI测评打分。不知道怎么写?