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 n n$ distinct strings of equal length $ m m 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
n
n$ and $
m
m
m$ ($
1
≤
n
≤
100
1 \le n \le 100
1≤n≤100$, $
1
≤
m
≤
50
1 \le m \le 50
1≤m≤50$) — the number of strings and the length of each string.
Next $ n n n$ lines contain a string of length $ m m 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
Input
3 3
tab
one
bat
Output
6
tabbat
Input
4 2
oo
ox
xo
xx
Output
6
oxxxxo
Input
3 5
hello
codef
orces
Output
0
Input
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
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.
在家上课就没那么多时间写题了,巨烦!!
虽然是个B题,但是要注意的地方还是挺多的。
录入一个字符串之后,我们要判断它的反转字符串是否存在。但是呢,要注意一点,就是自己是自己的反转字符串,这样的情况只能出现一次。但是如果有两个相同的这样的串,例如有两个xx,这就可以都拼接上了,直接暴力就可以。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=101;
string s[maxx];
map<string,int>mp;
int n,m;
int main()
{
cin>>n>>m;
mp.clear();
string t1="",t2="";
for(int i=0;i<n;i++) cin>>s[i],mp[s[i]]++;
string x;
int flag=0;
for(int i=0;i<n;i++)
{
x=s[i];
reverse(s[i].begin(),s[i].end());
swap(x,s[i]);
if(mp[s[i]]<=0) continue;
if(mp[x])
{
if(x==s[i]&&mp[x]==1&&flag==0) t1=t1+x,flag=1,mp[x]--;
else if(x!=s[i]||(x==s[i]&&mp[x]>1))t1=s[i]+t1,t2=t2+x,mp[x]--,mp[s[i]]--;
}
}
t1=t1+t2;
cout<<t1.length()<<endl;
cout<<t1<<endl;
return 0;
}
努力加油a啊,(o)/~