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 nn distinct strings of equal length mm. 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 nn and mm (1≤n≤1001≤n≤100, 1≤m≤501≤m≤50) — the number of strings and the length of each string.
Next nn lines contain a string of length mm 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
input
Copy
3 3 tab one bat
output
Copy
6 tabbat
input
Copy
4 2 oo ox xo xx
output
Copy
6 oxxxxo
input
Copy
3 5 hello codef orces
output
Copy
0
input
Copy
9 4 abab baba abcd bcde cdef defg wxyz zyxw ijji
output
Copy
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<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int a[N],sum[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<string,int>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
int n,m;
string s[N],sss;
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>s[i];
mp[s[i]]++;
}
int maxx=0,flag=0;
string sr;
for(int i=1;i<=n;i++)
{
string ss=s[i];
reverse(ss.begin(),ss.end());
if(ss==s[i])
{
if(ss.size()>maxx)// 找到最长的一串只有一种字母的字符串放最中间
{
flag=1;
sr=ss;
maxx=ss.size();
}
}
if((mp[ss]&&ss!=s[i]))
{
sss+=s[i];
mp[s[i]]--;
mp[ss]--;
}
}
if(flag)
cout<<sss.size()*2+sr.size()<<endl;
else cout<<sss.size()*2<<endl;
cout<<sss;
if(flag)cout<<sr;
reverse(sss.begin(),sss.end());
cout<<sss;
}
signed main()
{
int t=1;
//cin>>t;
while(t--)
{
solve();
}
}