cd620B. Longest Palindrome

B. Longest Palindrome
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 n distinct strings of equal length m. 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 n and m (1≤n≤100, 1≤m≤50) — the number of strings and the length of each string.

Next n lines contain a string of length m 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

inputCopy
3 3
tab
one
bat
outputCopy
6
tabbat
inputCopy
4 2
oo
ox
xo
xx
outputCopy
6
oxxxxo
inputCopy
3 5
hello
codef
orces
outputCopy
0

inputCopy
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
outputCopy
20
ababwxyzijjizyxwbaba
Note
In the first example, “battab” is also a valid answer.

In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.

In the third example, the empty string is the only valid palindrome string.

题意

给出n个m长的字符串
找出能组成的最大回文串

思路

将每一个字符串存入map,如果反序字符串,存在map中就将其加入结构体中,特判本身是回文串的情况,之后统一输出,当初错在了getline(cin,s)就错,cin>>s反而就能过,还是不太理解输入原理。。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<map>
#include<string>
using namespace std;
map<string,int>Q;
struct node
{
	string strs;
}d[1000];
int k = 0;
int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie();
	int n, m;
	cin >> n>>m;
	string s,new_s;
	d[500].strs = "-";
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		//cout << s << endl;
		new_s = "";
		int t = 0;
		for (int j = m - 1,z=0; j >= 0,z<m; j--,z++)
		{
			new_s += s[j];
			if (s[z] != s[j])
				t = 1;
		}
		//cout << new_s << endl;
		if (t == 0)
		{
			d[500].strs = "";
			d[500].strs = s;
		}
		if (Q.count(new_s))
		{
			d[k++].strs = s;
		}
		else
		{
			Q[s] = 1;
		}
	}
	//cout << k << endl;
	int sum = 0;
	sum += k * m*2;
	if (d[500].strs != "-")
		sum += m;
	cout << sum << endl;
	for (int i = 0; i < k; i++)
	{
		cout << d[i].strs;
	}
	if (d[500].strs != "-")
		cout << d[500].strs;
	for (int i = k - 1; i >= 0; i--)
	{
		for (int j = m - 1; j >= 0; j--)
			cout << d[i].strs[j];
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值