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. access denied (2)

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

  2. swagger2 常用注解的使用

    一.@Api 效果: @Api注解放在类上面,这里的value是没用的,tags表示该controller的介绍. 二 .@ApiOperation 效果: @ApiOperation注解用于放在方法 ...

  3. EntityFramework 根据时间筛选数据

    需求:根据当前时间,获取条件合适的数据,其中截止时间只比较日期. 1. 运行会报错的版本: var lifeWorkEventBatch = clientRepositoryContainer.Lif ...

  4. opencv:边缘保留滤波

    EPF滤波概述 均值与滤波的缺点:并没有考虑中心像素点对整个输出像素的贡献,实际上锚定的那个点贡献应该是最大的 高斯滤波的缺点:当边缘值梯度很大的时候,应减少中心像素点的权重,而高斯滤波没有考虑 边缘 ...

  5. Nuxt的路由动画效果

    Nuxt.js提供两种方法为路由提供动画效果,一种是全局的,一种是针对单独页面的 全局动画默认使用page来进行设置,例如现在我们为每个页面都设置一个进入和退出时的渐隐渐现的效果.我们可以先在根目录的 ...

  6. Spring-Bean的后置处理器

    Spring——Bean 后置处理器 Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例 ...

  7. NAT ALG原理与应用

    1  NAT ALG简介 普通NAT实现了对UDP或TCP报文头中的的IP地址及端口转换功能,但对应用层数据载荷中的字段无能为力,在许多应用层协议中,比如多媒体协议(H.323.SIP等).FTP.S ...

  8. FileOutputStream,BufferedOutputStream,FileWriter 效率比较

    测试代码: /** * 写文件 * FileOutputStream, BufferedOutputStream, FileWriter * 三个流 效率比较 */ @Test public void ...

  9. 一个c程序的执行是从什么开始的?

    一个C语言的执行是从本程序的main函数开始,到main函数结束,但需要注意的是,也是有特殊情况的,若是代码中出现了exit函数,则直接结束程序,对于这一点还是需要了解的. 关于c语言需要了解的是,C ...

  10. red hat 报错:apt-get:找不到命令

    Linux有两个系列:一个是RedHat系列,一个是Debian系列. RedHat系列:Redhat.Centos.Fedora等 Debian系列:Debian.Ubuntu等 RedHat 系列 ...