Preparando MOJI

Longest Palindrome

1000ms 262144K

Description:

Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.

Gildong loves this concept so much, so he wants to play with it. He has $$$n$$$ distinct strings of equal length $$$m$$$. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

Input:

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 100$$$, $$$1 \le m \le 50$$$) — the number of strings and the length of each string.

Next $$$n$$$ lines contain a string of length $$$m$$$ each, consisting of lowercase Latin letters only. All strings are distinct.

Output:

In the first line, print the length of the longest palindrome string you made.

In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.

Sample Input:

3 3
tab
one
bat

Sample Output:

6
tabbat

Sample Input:

4 2
oo
ox
xo
xx

Sample Output:

6
oxxxxo

Sample Input:

3 5
hello
codef
orces

Sample Output:

0

Sample Input:

9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji

Sample Output:

20
ababwxyzijjizyxwbaba

Note:

In the first example, "battab" is also a valid answer.

In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.

In the third example, the empty string is the only valid palindrome string.

Informação

Codeforces

Provedor Codeforces

Código CF1304B

Tags

brute forceconstructive algorithmsgreedyimplementationstrings

Submetido 0

BOUA! 0

Taxa de BOUA's 0%

Datas 09/05/2023 09:59:09

Relacionados

Nada ainda