# UVA12467「Secret Word」

## 1. 题目

### 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 $S$ in a piece of paper and gave Roberto the following clues:

• The secret word is a non-empty substring (A substring of $S$ is defined as any consecutive sequence of characters belonging to $S$. For example, if $S$ = abcd then a, abcd, ab, bc and bcd are some of the substrings of $S$ but ac, aa, aabbccdd and dcba are not substrings of $S$) of $S$ (possibly equal to $S$).
• $S$ 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 $T \leq 150$, the number of test cases.

$T$ lines follow, each with a single string $S$. $S$ will only contain lowercase English letters. The length of $S$ 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 take c and reverse it you get c. colombia starts with c, 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 take ba and reverse it you get ab. abcdba starts with ab and there’s no longer substring that satisfies the two clues.
• neversayeven: if you take even and reverse it you get neve. neversayeven starts with neve and there’s no longer substring that satisfies the two clues.
• neveroddoreven: this case is a palindrome so if we reverse it we get the same string. neveroddoreven starts with neveroddoreven (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.

## 2. 题目

### 2.1 分析

• 由题意可知，最终的 secret 满足 secret 和 secret 的反转 secret' 均在字符串 $s$ 中。将字符串 $s$ 反转后的得到 $s'$，然后将 $s$$s'$ 拼接在一起得到 $S = s + \# + s'$，在对 $S$ 求前缀函数。然后统计 $\pi[n+1..2n]$ 中最大的，此即为满足题意的最长 secret 的长度，设对应下标为 $pos$
• 由于 $s$$s'$ 关于下标 $x = n$ 对称，故最终答案为 $s[2n-pos..2n-pos+\pi[pos]-1]$