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;
    }
  • 相关阅读:
    springboot项目在IDEA根据不同的开发人员读取不同的配置文件
    Idea中一个服务按多个端口同时启动
    修改feign解析器替换json
    Intellij IDEA中启动多个微服务--开启Run Dashboard管理
    解决springboot乱码和window cmd乱码
    调用远程linux服务器shell脚本
    cp复制命令详解
    ftp列出具体目录的所有目录,和目录按照文件类型列出
    Linux下Redis开机自启(Centos)
    vsftpd 配置上传失败553
  • 原文地址:https://www.cnblogs.com/ylrwj/p/12318728.html
Copyright © 2011-2022 走看看