CF1304B Longest Palindrome(可逆转符号,数学分析,回文串)

简介: CF1304B 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 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.


输入格式



The first line contains two integers nn and mm ( 1 \le n \le 1001≤n≤100 , 1 \le m \le 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.


输出格式



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.


题意翻译



  • 给你  n 个长度为  m 的字符串。
  • 请你判断删去其中的几个(或者不删去),能使得将剩下字符串随意排列所形成的回文串长度最大。
  • 请你输出最大的长度和那个回文串。
  • 1≤n≤100, 1≤m≤50。


输入输出样例



输入  

3 3

tab

one

bat


输出  

6

tabbat


输入

4 2

oo

ox

xo

xx


输出  

6

oxxxxo


输入

3 5

hello

codef

orces


输出  

0


输入

9 4

abab

baba

abcd

bcde

cdef

defg

wxyz

zyxw

ijji


输出  

20

ababwxyzijjizyxwbaba


题目刨析,首先我们观察答案发现是由2*n个互相可以颠倒相同的字符串组成,或者由2*n个互相颠倒的字符串组成然后再字符串中间加上一个本身可以颠倒的字符串组成答案,所以我们思路是定一个字符串然后遍历后面的字符串,如果相同用一个string来存字符串,最后找一个自身颠倒自己的字符串加在中间,退出循环。就是答案。思路就是这样,具体操作看代码。


#include<bits/stdc++.h>
using namespace std;
struct node{
  string word;//字符串
  int flag;//标记是否已经用过、、先默认为·0 
}a[105];
string ans;//用来输出
int main()
{
  int n,m;
  cin>>n>>m;
  getchar();
  for(int i=0;i<n;i++)
      cin>>a[i].word;
   for(int i=0;i<n;i++)
        {
      if(!a[i].flag)//=0
      { 
         for(int j=i+1;j<n;j++)//枚举i后面字符串是否对称
        {
          string temp=a[j].word;
          reverse(temp.begin(),temp.end());//反转字符串 
            if(strcmp(a[i].word.c_str(),temp.c_str())==0)//第一个是前面的反转后的字符,第二个是后面的字符看是不是相等 
            {
              ans=a[i].word+ans+a[j].word;//string 可以直接连在一起 
              a[i].flag=a[j].flag=1;//标记 
            }
        }
          } 
      }
    for(int i=0;i<n;i++)
    {
        if(!a[i].flag)//=0
        {
          string temp=a[i].word;
          reverse(temp.begin(),temp.end());
          if(strcmp(temp.c_str(),a[i].word.c_str())==0)//看自己是不是回文串 
          {
             ans.insert(ans.size()/2,a[i].word);//插入自身对称的字符串(回文串)
                break;
          }
            }
    }
    cout<<ans.size()<<"\n";
    cout<<ans<<endl;
 } 
相关文章
|
Linux Android开发 编解码
VLC播放RTSP视频延迟问题
之前写过一篇关于在Linux平台上编译Android平台上VLC播放器源代码的文章,vlc这款播放器非常优秀而且是开源的,它的核心是开源视频编解码库ffmpeg。而且这款播放器还支持RTSP协议,这个主要是用开源的live555来实现的,live555这个库以后还需要认真研习。
5667 0
|
7月前
|
监控 安全 Java
解决 Spring Boot 中 SecurityConfig 循环依赖问题的详解
本文详细解析了在 Spring Boot 中配置 `SecurityConfig` 时可能遇到的循环依赖问题。通过分析错误日志与代码,指出问题根源在于 `SecurityConfig` 类中不当的依赖注入方式。文章提供了多种解决方案:移除 `configureGlobal` 方法、定义 `DaoAuthenticationProvider` Bean、使用构造函数注入以及分离配置类等。此外,还讨论了 `@Lazy` 注解和允许循环引用的临时手段,并强调重构以避免循环依赖的重要性。通过合理设计 Bean 依赖关系,可确保应用稳定启动并提升代码可维护性。
620 0
|
8月前
|
人工智能 自然语言处理 API
如何在 10 分钟内将 DeepSeek API 集成到您的应用程序
在AI时代,DeepSeek API以其先进的模型帮助企业快速集成自然语言处理等功能,无需深厚机器学习背景。通过Apipost工具,开发者可轻松测试、调试API并生成代码,优化工作流。本文介绍从身份验证到错误处理的完整流程,并提供相关资源链接,助您高效实现应用智能化。
|
设计模式 存储 缓存
【ffmpeg 视频播放】深入探索:ffmpeg视频播放优化策略与设计模式的实践应用(二)
【ffmpeg 视频播放】深入探索:ffmpeg视频播放优化策略与设计模式的实践应用
325 0
|
机器学习/深度学习 数据采集 人工智能
揭开大模型幻觉之谜:深入剖析数据偏差与模型局限性如何联手制造假象,并提供代码实例助你洞悉真相
【10月更文挑战第2天】近年来,大规模预训练模型(大模型)在自然语言处理和计算机视觉等领域取得卓越成绩,但也存在“大模型幻觉”现象,即高准确率并不反映真实理解能力。这主要由数据偏差和模型局限性导致。通过平衡数据集和引入正则化技术可部分缓解该问题,但仍需学界和业界共同努力。
361 4
|
计算机视觉
MATLAB用Lasso回归拟合高维数据和交叉验证
MATLAB用Lasso回归拟合高维数据和交叉验证
|
安全 数据挖掘 BI
医疗安全(不良)事件管理系统源码 不良事件上报系统
医疗安全(不良)事件管理系统,主要包括对上报事件的保存、审批、修改及基本事件的统计分析等功能。 随着对患者安全关注度的逐渐提高,对于医院可能存在的各类不良事件进行上报和处理,对数据进行统计和分析,完成对事件的持续改进和整体风险评估,有效预防不良事件再次发生。 医疗安全(不良)事件管理,让上报者更加准确、快捷的将不良事件内容报告给相关管理人员,使管理者系统地收集资料,并通过深入分析和学习,寻找管理中的薄弱环节,完善系统结构,最终有效预防不良事件再次发生。
362 0
|
存储 Linux 编译器
Linux拓展:链接库
Linux拓展:链接库
191 0
|
网络协议 API Python
一文带你了解Python Socket 编程
一文带你了解Python Socket 编程
432 0
一文带你了解Python Socket 编程
|
存储 OLAP BI
Hologres只读从实例自助配置发布
本文将会介绍Hologres发布的只读从实例和相关使用操作。(文末有福利~)
1200 0
Hologres只读从实例自助配置发布

热门文章

最新文章