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. nginx 定义:响应头和请求头

    1) 响应头 add_header 例如: add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; a ...

  2. Makefile中的wildcard/notdir/patsubst

    在Makefile规则中,通配符会被自动展开. 但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的用法是:$(wildcard PATTE ...

  3. 1.学习一下Angularjs的promisee

    1.首先来了解一下promisee: 在谈论Promise之前我们要了解一下一些额外的知识:我们知道JavaScript语言的执行环境是“单线程”,所谓单线程,就是一次只能够执行一个任务,如果有多个任 ...

  4. Googletest Primer

    Googletest Primer 原文地址:https://github.com/google/googletest/blob/master/googletest/docs/primer.md In ...

  5. PHP 代码内执行Linux命令

    还是那个问题,就是那个php填写pdf表单,因为副武器的原因,改用命令执行了,哎,一个问题好多知识点啊,先来说说PHP执行linux命令,其实挺简单的,但是呢,后面说说我遇到的问题 1.PHP执行命令 ...

  6. C#连接SQL Server数据库(二)

    执行SQL语句:Command对象 1.Command对象概述   Command对象是一个数据命令对象,主要功能是向数据库发送查询.更新.删除.修改操作的SQL语句.Command对象主要有以下几种 ...

  7. mac登录窗口出现白框问题解决

    昨天早上起床打开电脑,发现登录窗口的界面出现了大半边的白框,如下图,可是昨晚上关机前还是好好的,而且新电脑不至于啥也没干屏幕就出问题. 输入密码进入桌面,OK,不是屏幕的问题,那为什么会出现白框呢? ...

  8. 消息队列(六)--- RocketMQ-消息消费

    文章部分图片来自参考资料,侵删 概述 我们从前面的发送流程知道某个主题的消息到了broker 的 messageque 里,假如让我们来设计一个消息队列的消费者过程,那么多个消费者应该如何消费数量较少 ...

  9. go基础_defer

    defer defer是go语言中的关键字 特点:FILO 作用:保障一些申请的资源最终得以释放 func main() { defer fmt.Println("line-1") ...

  10. Go_random

    package main import ( "math/rand" "fmt" "time" ) func main() { /* 生成随机 ...