QOJ.ac

QOJ

Time Limit: 0.3 s Memory Limit: 32 MB Total points: 100

#319. Periodicity

Statistics

Byteasar, the king of Bitotia, has ordained a reform of his subjects' names. The names of Bitotians often contain repeating phrases, e.g., the name Abiabuabiab has two occurrences of the phrase abiab. Byteasar intends to change the names of his subjects to sequences of bits matching the lengths of their original names. Also, he would very much like to reflect the original repetitions in the new names.

In the following, for simplicity, we will identify the upper- and lower-case letters in the names. For any sequence of characters (letters or bits) $w=w_{1}w_{1}…w_{k}$ we say that the integer $p$ ($1 ≤ p < k$) is a period of $w$ if $w_{i}=w_{i+p}$ for all $i=1,…,k-p$. We denote the set of all periods of $w$ by $\text{Per}(w)$. For example, $\text{Per}(\texttt{ABIABUABIAB})={6,9}$, $\text{Per}(\texttt{01001010010})=\{5,8,10\}$, and $\text{Per}(\texttt{0000})=\{1,2,3\}$.

Byteasar has decided that every name is to be changed to a sequence of bits that:

  • has length matching that of the original name,
  • has the same set of periods as the original name,
  • is the smallest (lexicographically^{1}) sequence of bits satisfying the previous conditions.

For example, the name ABIABUABIAB should be changed to 01001101001, BABBAB to 010010, and BABURBAB to 01000010.

Byteasar has asked you to write a program that would facilitate the translation of his subjects' current names into new ones. If you succeed, you may keep your current name as a reward!

^{1}: The sequence of bits $x_{1}x_{2}…x_{k}$ is lexicographically smaller than the sequence of bits $y_{1}y_{2}…y_{k}$ if for some $i$, $1 ≤ i ≤ k$, we have $x_{i} < y_{i}$ and for all $j=1,…,i-1$ we have $x_{j}=y_{j}$.

Input Format

In the first line of the standard input there is a single integer $k$ - the number of names to be translated ($1 ≤ k ≤ 20$). The names are given in the following lines, one in each line. Each name consists of no less than $1$ and no more than $200\,000$ upper-case (capital) letters (of the English alphabet).

In the test worth $30\%$ of the points each name consists of at most 20 letters.

Output Format

Your program should print $k$ lines to the standard output. Each successive line should hold a sequence of zeroes and ones (without spaces in between) corresponding to the names given on the input. If an appropriate sequence of bits does not exists for some names, then "XXX" (without quotation marks) should be printed for that name.

Sample Data

Sample Input

3
ABIABUABIAB
BABBAB
BABURBAB

Sample Output

01001101001
010010
01000010