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

6932 단어
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 
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[100]={0};
    int i,j;
    for(i=1;i<=n;i++)
    {
        string temp;
        cin>>temp;
        v1.push_back(temp);
        reverse(temp.begin(),temp.end());
        v2.push_back(temp);
    }
    for(i=0;i)
    {
        //string s1=v1[i];
        for(j=0;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]=1;
                vis[j]=1;
            }
        }
    }
    string dc="";
    for(i=0;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()==0&&ans2.size()==0)
    {
        cout<<0;
        return 0;
    }
    long long size=0;
    for(i=0;ians1[i].size();
    for(i=0;ians2[i].size();
    cout<endl;
    for(i=0;ians1[i];
    for(i=0;ians2[i];
    
    return 0;
}
 
   

 

 
   
 

 




좋은 웹페이지 즐겨찾기