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. 转:unittest的几种运行方式

    #unittest-test.py import unittestfrom demo import RunMainimport HtmlTestRunner class TestMethod(unit ...

  2. chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现

    Ubuntu 16.04 下: 0x01 安装chrome 1 下载源加入系统源列表 sudo wget http://www.linuxidc.com/files/repo/google-chrom ...

  3. PAT-链表-A1032 Sharing

    题意:给出两条链表的首地址以及若干个节点的的地址.数据.下一个节点的地址,求两条链表的首个共用节点的地址.如果两条链表没有共用节点,则输出-1. 思路:使用静态链表,首先遍历一遍第一个链表并进行标记. ...

  4. thinkphp 接收文件并处理

    html前台文件,上传到控制器,thinkphp处理它 前台 <form action="{:url('product/brand_addcl')}" enctype=&qu ...

  5. C++继承、多态与虚表

    继承 继承的一般形式 子类继承父类,是全盘继承,将父类所有的东西都继承给子类,除了父类的生死,就是父类的构造和析构是不能继承的. 继承的访问权限从两方面看: 1.对象:对象只能直接访问类中公有方法和成 ...

  6. Centos7 下mysql 密码重置

    Centos7 下mysql 密码重置 先停止mysql服务 mysqld_safe --skip-grant-tables & mysql mysql> use mysql;mysql ...

  7. 对于一些stl自定义比较函数

    1.unorderd_map自定义键 自定义类型 struct my_key { int num; string name; }; 1.由于unordered_map是采用哈希实现的,对于系统的类型i ...

  8. Bugku-CTF之sql注入2 (全都tm过滤了绝望吗?)

    Day  38 sql注入2 200 http://123.206.87.240:8007/web2/ 全都tm过滤了绝望吗? 提示 !,!=,=,+,-,^,%

  9. 7-3 Path to Infinity(还没ac)

    留坑 #include<bits/stdc++.h> using namespace std; ; ; typedef long long ll; string s,t; ,tol2=,t ...

  10. React 实现input输入框的防抖和节流

    1.为什么使用防抖和节流对于频繁触发的事件 比如keydown keyup事件 当频繁点击时候 会多次触发事件 页面出现卡顿 影响性能 2.函数防抖(debounce):间隔时间内只执行一次   函数 ...