Codeforces Global Round 7
D1
Prefix-Suffix Palindrome (Easy version)
Codeforces
1500
原题
← CD2 →
翻译中…

This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

翻译中…

You are given a string $s$, consisting of lowercase English letters. Find the longest string, $t$, which satisfies the following conditions:

翻译中…
  • The length of $t$ does not exceed the length of $s$.
  • $t$ is a palindrome.
  • There exists two strings $a$ and $b$ (possibly empty), such that $t = a + b$ ( "$+$" represents concatenation), and $a$ is prefix of $s$ while $b$ is suffix of $s$.
翻译中…

Input

翻译中…

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$), the number of test cases. The next $t$ lines each describe a test case.

翻译中…

Each test case is a non-empty string $s$, consisting of lowercase English letters.

翻译中…

It is guaranteed that the sum of lengths of strings over all test cases does not exceed $5000$.

翻译中…

Output

翻译中…

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

翻译中…

Note

翻译中…

In the first test, the string $s = $"a" satisfies all conditions.

翻译中…

In the second test, the string "abcdfdcba" satisfies all conditions, because:

翻译中…
  • Its length is $9$, which does not exceed the length of the string $s$, which equals $11$.
  • It is a palindrome.
  • "abcdfdcba" $=$ "abcdfdc" $+$ "ba", and "abcdfdc" is a prefix of $s$ while "ba" is a suffix of $s$.
翻译中…

It can be proven that there does not exist a longer string which satisfies the conditions.

翻译中…

In the fourth test, the string "c" is correct, because "c" $=$ "c" $+$ "" and $a$ or $b$ can be empty. The other possible solution for this test is "s".

翻译中…

样例

Input #1
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
Output #1
a
abcdfdcba
xyzyx
c
abba
InputCopy OutputCopy
题目信息
题号
1326D1
时间限制
2 s
内存限制
256 MB
标签
哈希
后缀结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。