B. Longest Palindrome
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤n≤100, 1≤m≤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.

Examples

inputCopy
3 3
tab
one
bat
outputCopy
6
tabbat
inputCopy
4 2
oo
ox
xo
xx
outputCopy
6
oxxxxo
inputCopy
3 5
hello
codef
orces
outputCopy
0

inputCopy
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
outputCopy
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.

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define paii pair<int,int>
#define pali pair<ll,int>
#define pail pair<int,ll>
#define pall pair<ll,ll>
#define fi first
#define se second
int v;
char s;
vector<int> a,b;
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>s[i];
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int flag=0;
for(int k=0;k<m;k++){
if(s[i][k]!=s[j][m-1-k]) {flag=1;break;}
}
if(!flag){
v[i]=v[j]=1;
a.pb(i),b.pb(j);

}
}
}
int q=-1;
for(int i=0;i<n;i++){

if(!v[i]){
int flag=0;
for(int j=0;j<m;j++){
if(s[i][j]!=s[i][m-1-j]) {flag=1;break;}
}
if(!flag) {q=i;break;}
}
}
cout<<a.size()*2*m+(q!=-1)*m<<endl;
for(int i=0;i<a.size();i++)
cout<<s[a[i]];
if(q!=-1) cout<<s[q];
for(int i=b.size()-1;i>=0;i--)
cout<<s[b[i]];

return 0;
}

``````