Codeforces Round 778 (Div. 1 + Div. 2, based on Technocup 2022 Final Round)
B
Prefix Removals
Codeforces
800
原题
← AC →
翻译中…

You are given a string $s$ consisting of lowercase letters of the English alphabet. You must perform the following algorithm on $s$:

翻译中…
  • Let $x$ be the length of the longest prefix of $s$ which occurs somewhere else in $s$ as a contiguous substring (the other occurrence may also intersect the prefix). If $x = 0$, break. Otherwise, remove the first $x$ characters of $s$, and repeat.
翻译中…

A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd".

翻译中…

For instance, if we perform the algorithm on $s =$ "abcabdc",

翻译中…
  • Initially, "ab" is the longest prefix that also appears somewhere else as a substring in $s$, so $s =$ "cabdc" after $1$ operation.
  • Then, "c" is the longest prefix that also appears somewhere else as a substring in $s$, so $s =$ "abdc" after $2$ operations.
  • Now $x=0$ (because there are no non-empty prefixes of "abdc" that also appear somewhere else as a substring in $s$), so the algorithm terminates.
翻译中…

Find the final state of the string after performing the algorithm.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

翻译中…

This is followed by $t$ lines, each containing a description of one test case. Each line contains a string $s$. The given strings consist only of lowercase letters of the English alphabet and have lengths between $1$ and $2 \cdot 10^5$ inclusive.

翻译中…

It is guaranteed that the sum of the lengths of $s$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print a single line containing the string $s$ after executing the algorithm. It can be shown that such string is non-empty.

翻译中…

Note

翻译中…

The first test case is explained in the statement.

翻译中…

In the second test case, no operations can be performed on $s$.

翻译中…

In the third test case,

翻译中…
  • Initially, $s =$ "bbbbbbbbbb".
  • After $1$ operation, $s =$ "b".
翻译中…

In the fourth test case,

翻译中…
  • Initially, $s =$ "codeforces".
  • After $1$ operation, $s =$ "odeforces".
  • After $2$ operations, $s =$ "deforces".
翻译中…

样例

Input #1
6
abcabdc
a
bbbbbbbbbb
codeforces
cffcfccffccfcffcfccfcffccffcfccf
zyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw
Output #1
abdc
a
b
deforces
cf
xyzzw
InputCopy OutputCopy
题目信息
题号
1654B
时间限制
2 s
内存限制
256 MB
标签
字符串

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