UVA12467「Secret Word」
1. 题目
题目链接:UVA12467「Secret Word」 。
Description
Alicia and Roberto like to play games. Today, Roberto is trying to guess a secret word that Alicia chose. Alicia wrote a long string in a piece of paper and gave Roberto the following clues:
- The secret word is a non-empty substring (A substring of is defined as any consecutive sequence of characters belonging to . For example, if =
abcd
thena
,abcd
,ab
,bc
andbcd
are some of the substrings of butac
,aa
,aabbccdd
anddcba
are not substrings of ) of (possibly equal to ). - starts with the secret word reversed.
Roberto knows Alicia very well, and he’s sure that if there are several possible secret words that satisfy the clues above, Alicia must have chosen the longest one.
Can you help him guess the secret word?
Input
The first line of the input file contains a single integer number , the number of test cases.
lines follow, each with a single string . will only contain lowercase English letters. The length of will not exceed one million characters.
Output
For each test case, output the secret word in one line.
Explanation of the sample cases:
colombia
: if you takec
and reverse it you getc
.colombia
starts withc
, so this satisfies the two clues above. Furthermore,c
is the longest word that satisfies the two clues so it must be the secret word.abcdba
: if you takeba
and reverse it you getab
.abcdba
starts withab
and there’s no longer substring that satisfies the two clues.neversayeven
: if you takeeven
and reverse it you getneve
.neversayeven
starts withneve
and there’s no longer substring that satisfies the two clues.neveroddoreven
: this case is apalindrome
so if we reverse it we get the same string.neveroddoreven
starts withneveroddoreven
(since they are the same) and obviously there’s no longer substring that satisfies that, so this is the secret word.listentothesilence
: Notice the secret word might be somewhere in the middle of the big word.
Sample Input
1 |
|
Sample Output
1 |
|
2. 题目
2.1 分析
- 由题意可知,最终的 secret 满足 secret 和 secret 的反转 secret' 均在字符串 中。将字符串 反转后的得到 ,然后将 和 拼接在一起得到 ,在对 求前缀函数。然后统计 中最大的,此即为满足题意的最长 secret 的长度,设对应下标为 。
- 由于 和 关于下标 对称,故最终答案为 。
2.2 代码
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!