Codeforces Round #620 (Div. 2):B. Longest Palindrome

Discription
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
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

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。再在字符串中找是否存在回文串,如果存在,则加上一个回文串。输出的时候将只存了一半的字符串,再输出回文串,再将一半的字符串你想输出。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define rep(i, a, n) for(int i=a; i<=n; i++)
#define per(i, n, a) for(int i=n; i>=a; i--)
 
int T;
int n,m;
char a[110][60],b[110][60];
int f[110];
int ans;
multiset<string>pp;
multiset<string>::iterator it;
set<char>tt;
string s,s1;
 
int main()
{
    sdd(n,m);
    ans=0;
    s="";
    s1="";
    memset(f,0,sizeof(f));
    for(int i=1; i<=n; i++)
    {
        scanf("%s",a[i]);
        pp.insert(a[i]);
        int p=strlen(a[i])-1;
        for(int j=0; j<strlen(a[i]); j++)
        {
            b[i][p]=a[i][j];
            p--;
        }
    }
    /*for(int i=1;i<=n;i++)
        cout<<b[i]<<endl;*/
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++)
        {
            if(strcmp(a[i],b[j])==0&&f[i]==0&&f[j]==0)
            {
                ans+=m*2;
                s=s+a[i];
                f[i]=1;
                f[j]=1;
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(strcmp(a[i],b[i])==0&&f[i]==0)
        {
            ans+=m;
            s1=a[i];
            break;
        }
    }
    string s2=s;
    reverse(s2.begin(),s2.end());
    s+=s1+s2;
    printf("%d\n",ans);
    cout<<s<<endl;
    return 0;
}
发布了313 篇原创文章 · 获赞 105 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43460224/article/details/104348474
今日推荐