CF:#620场div2题目题解:B - Longest Palindrome

本文介绍了一种算法,用于从一组等长字符串中构建出最长的回文串。通过检查单个字符串是否为回文及寻找成对的回文字符串,算法能够有效地重组并输出最长的可能回文串。

题目:

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的串串,让你拼出一个最长回文串,并且输出长度和该回文串。

思路

首先就是这些序列长度都是m,这也就是说要么是回文串两两回文,中间有可能有个自己本身就是回文串像是abba这样的序列。

我们利用string类型的超方便函数就可以做了。

用map定义一个字符串数组之后就可以写几个函数,来检查哪些自己就是一个回文串:

bool check(int k)
{
	string str=all[k];
	for(int time=0;time<m/2;time++)
	{
		if(str[time]!=str[m-time-1])return false;
	}
	return true;
}

找到一个就够了,外面这样处理

for(int time=1;time<=n;time++)
	{
		if(check(time)){mid=time;ans+=m;break;}
	}

用mid记录一下到底是哪个自己就是回文串;
下一个函数就是检查哪些是两两回文的,这就用到了我们的超方便函数:
reverse(str.begin(),str.end())来倒转一个string类型变量。
str1.compare(str2)如果str1和str2是一样的内容,比如aba和aba来compare一下,会返回0,否则就是一个非零的数,
我们就可以用if(!str1.compare(str2))表示俩变量是一回事。

bool FIND(int a,int b)
{
	reverse(all[b].begin(),all[b].end());
	if(!all[a].compare(all[b])){reverse(all[b].begin(),all[b].end());return true;}
		else	{reverse(all[b].begin(),all[b].end());return false;}
}
for(int time=1;time<=n;time++)
	{
		for(int time1=time+1;time1<=n;time1++)
		{
			if(FIND(time,time1)){left.push(time);right.push(time1);ans+=2*m;}
		}
	}

这样就能很好解决问题,然后你们会发现上面代码片的left和right是什么,看一下前面

	queue<int>left;
	stack<int>right;

我用这俩容器就能决定输出顺序,毕竟你要按一定的顺序输出答案。
接下来就是输出了,
上完整代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define

int n,m;
map<int ,string>all;
bool check(int k)
{
	string str=all[k];
	for(int time=0;time<m/2;time++)
	{
		if(str[time]!=str[m-time-1])return false;
	}
	return true;
}
bool FIND(int a,int b)
{
	reverse(all[b].begin(),all[b].end());
	if(!all[a].compare(all[b])){reverse(all[b].begin(),all[b].end());return true;}
		else	{reverse(all[b].begin(),all[b].end());return false;}
}
int main()
{
	scanf("%d %d",&n,&m);
	for(int time=1;time<=n;time++)
	{
		cin>>all[time];
	}
	int ans=0;
	int mid=0;
	for(int time=1;time<=n;time++)
	{
		if(check(time)){mid=time;ans+=m;break;}
	}
	queue<int>left;
	stack<int>right;
	for(int time=1;time<=n;time++)
	{
		for(int time1=time+1;time1<=n;time1++)
		{
			if(FIND(time,time1)){left.push(time);right.push(time1);ans+=2*m;}
		}
	}
	printf("%d\n",ans);
	while(!left.empty())
	{
		cout<<all[left.front()];
		left.pop();
	}
	if(mid)cout<<all[mid];
	while(!right.empty())
	{
		cout<<all[right.top()];
		right.pop();
	}
	printf("\n");
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值