zoukankan      html  css  js  c++  java
  • 拼接最长回文串

    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 ndistinct 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 (1n1001≤n≤100, 1m501≤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
    

    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.

    拼接成最长的回文串

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int v[150];
    char s[105][55];
    vector<int> a,b;
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=0;i<n;i++)
            cin>>s[i];
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int flag=0;
                for(int k=0;k<m;k++)
                {
                    if(s[i][k]!=s[j][m-1-k]) 
                    {
                        flag=1;
                        break;
                    }
                }
                if(!flag)
                {
                    v[i]=v[j]=1;
                    a.pb(i),b.pb(j);
                }
            }
        }
        int q=-1;
        for(int i=0;i<n;i++)
        {
    
            if(!v[i])
            {
               int flag=0;
               for(int j=0;j<m;j++)
                {
                   if(s[i][j]!=s[i][m-1-j]) 
                   {
                          flag=1;
                       break;
                    }
                }
                if(!flag) 
                {
                    q=i;
                    break;
                }
            }
        }
        cout<<a.size()*2*m+(q!=-1)*m<<endl;
        for(int i=0;i<a.size();i++)
            cout<<s[a[i]];
        if(q!=-1) cout<<s[q];
        for(int i=b.size()-1;i>=0;i--)
            cout<<s[b[i]];s
        return 0;
    }
  • 相关阅读:
    变量使用的注意事项
    计算机存储单元
    常量
    mybatis报错There is no getter for property named '***' in 'class ***'
    sonar-scanner扫描各种出错解决
    SonarQube搭建的各种问题
    linux每日命令(18):whereis命令
    Django视图层之路由配置系统(urls)
    Django---MTV模型、基本命令、简单配置
    web框架
  • 原文地址:https://www.cnblogs.com/ylrwj/p/12318728.html
Copyright © 2011-2022 走看看