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
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[]={};
int i,j;
for(i=;i<=n;i++)
{
string temp;
cin>>temp;
v1.push_back(temp);
reverse(temp.begin(),temp.end());
v2.push_back(temp);
}
for(i=;i<v1.size();i++)
{
//string s1=v1[i];
for(j=;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]=;
vis[j]=;
}
}
}
string dc="";
for(i=;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()==&&ans2.size()==)
{
cout<<;
return ;
}
long long size=;
for(i=;i<ans1.size();i++)size+=ans1[i].size();
for(i=;i<ans2.size();i++)size+=ans2[i].size();
cout<<size<<endl;
for(i=;i<ans1.size();i++)cout<<ans1[i];
for(i=;i<ans2.size();i++)cout<<ans2[i]; return ;
}


 

Codeforces Round #620 (Div. 2) B. Longest Palindrome的更多相关文章

  1. Codeforces Round #620 (Div. 2)

    Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...

  2. Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)

    A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...

  3. Codeforces Round #620 (Div. 2) 题解

    A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...

  4. Codeforces Round #620 (Div. 2) A. Two Rabbits

    Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...

  5. Codeforces Round #620 (Div. 2)E LCA

    题:https://codeforces.com/contest/1304/problem/E 题意:给定一颗树,边权为1,m次询问,每次询问给定x,y,a,b,k,问能否在原树上添加x到y的边,a到 ...

  6. Codeforces Round #620 (Div. 2)D dilworld定理

    题:https://codeforces.com/contest/1304/problem/D 题意:给定长度为n-1的只含’>'和‘<’的字符串,让你构造出俩个排列,俩个排列相邻的数字之 ...

  7. Codeforces Round #620 (Div. 2) D

    构造一个排列,要求相邻之间的数满足给定的大小关系,然后构造出两个序列,一个序列是所有可能的序列中LIS最长的,一个所有可能的序列中LIS最短的 最短的构造方法:我们考虑所有单调递增的部分,可以发现要让 ...

  8. Codeforces Round #620 (Div. 2)E(LCA求树上两点最短距离)

    LCA求树上两点最短距离,如果a,b之间距离小于等于k并且奇偶性与k相同显然YES:或者可以从a先走到x再走到y再走到b,并且a,x之间距离加b,y之间距离+1小于等于k并且奇偶性与k相同也输出YES ...

  9. Codeforces Round #620 (Div. 2)D(LIS,构造)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; int main(){ io ...

随机推荐

  1. linux软件下载

    可以到linux官网下载:http://vault.centos.org/6.10/os/Source/SPackages/

  2. access denied (2)

    除了https://www.cnblogs.com/bneglect/p/11558593.html  这里提到的以外,今天再记录一个新的原因造成这种情况的解决方法.path_info 这个算是路由模 ...

  3. java程序启动参数

    例如 启动进程如下 /home/work/noah/ccs/jc-controller/jdk1.7.0_55/bin/java -Xmx4096m -Xms4096m -Xmn1024m -XX:+ ...

  4. 滑雪(dfs+dp)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 113903   Accepted: 43478 Description ...

  5. python-excel读取-pyodbc

    https://github.com/mkleehammer/pyodbc/wiki/Cursor 利用pyodbc读取数据库,流程基本一样,就是配置connect对象时有所不同,下面是excel的: ...

  6. 题解 SP5271 XOINC - A Coin Game

    SP5271 XOINC - A Coin Game 双倍经验:P2964 [USACO09NOV]硬币的游戏A Coin Game O3做法(TLE):枚举i,j,k,即剩下i枚金币,上一轮选了j枚 ...

  7. Winform遍历窗口的所有控件(几种方式实现)

    本文链接:https://blog.csdn.net/u014453443/article/details/85088733 扣扣技术交流群:460189483 C#遍历窗体所有控件或某类型所有控件 ...

  8. EnumSet

    这个概念是在 Effective Java中了解到的, 可以通过EnumSet来代替位域这种方式表达.并不是很常见的概念, 因此记录下.如果在这之前恰好了解过 bitmap这种数据结构就更好了.不了解 ...

  9. 利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试

    从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试 通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文 ...

  10. nginx autoindex 配置目录浏览功能

    Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...