Loading

Codeforces Round #620 (Div. 2) B. Longest Palindrome

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 (1n1001≤n≤100, 1m501≤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
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

大意是给定一些字符串,让你用他们尽可能的拼成最长的回文串。考stl的题,用string类会比较方便。可以先用一个vector存原string,另一个vector存原string reverse后的string(直接用reverse(s.begin(),s.end()),再建两个vector,一个存最终答案左半部分的string,一个存右边的。首先两重for循环找出能成对的string,对称的放进两个答案容器,并在该位置打上标记,然后再On扫一遍原字符串,遇到自身就是回文的可以直接加入
左半部分的尾或者右半部分的头(没有区别)最后统计完总长后依次输出两个答案容器的元素即可。
#include <bits/stdc++.h>
using namespace std;
int n,m;
int main()
{
    cin>>n>>m;
    vector<string>v1;
    vector<string>v2;
    vector<string>ans1;
    vector<string>ans2;
    int vis[100]={0};
    int i,j;
    for(i=1;i<=n;i++)
    {
        string temp;
        cin>>temp;
        v1.push_back(temp);
        reverse(temp.begin(),temp.end());
        v2.push_back(temp);
    }
    for(i=0;i<v1.size();i++)
    {
        //string s1=v1[i];
        for(j=0;j<v1.size();j++)
        {
            if(v2[j]==v1[i]&&!vis[i]&&!vis[j]&&i!=j)
            {
                ans1.push_back(v1[i]);
                ans2.insert(ans2.begin(),v1[j]);
                vis[i]=1;
                vis[j]=1;
            }
        }
    }
    string dc="";
    for(i=0;i<v1.size();i++)
    {
        if(!vis[i])
        {
            string s1=v1[i];
            string s2=v1[i];
            reverse(s2.begin(),s2.end());
            if(s1==s2)
            {
                dc=v1[i];
                break;
            }
        }
    }
    ans1.push_back(dc);
    if(ans1.size()==0&&ans2.size()==0)
    {
        cout<<0;
        return 0;
    }
    long long size=0;
    for(i=0;i<ans1.size();i++)size+=ans1[i].size();
    for(i=0;i<ans2.size();i++)size+=ans2[i].size();
    cout<<size<<endl;
    for(i=0;i<ans1.size();i++)cout<<ans1[i];
    for(i=0;i<ans2.size();i++)cout<<ans2[i];
    
    return 0;
}

 


 

 




posted @ 2020-02-16 10:43  脂环  阅读(325)  评论(0编辑  收藏  举报