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;
 } 
相关文章
|
存储 Java
【Java基础】- RMI原理和使用详解
【Java基础】- RMI原理和使用详解
984 0
|
机器人 开发工具 Web App开发
干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
阿里云RPA,作为阿里云自研8年的技术,在资本的热捧下,逐渐从幕后来到台前,成为企业服务市场的黑马。本文将从产品上全面剖析,阿里云RPA这款产品的现阶段情况,同时简单谈谈阿里云RPA的商业化进展。
9466 0
干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
|
10月前
|
人工智能 数据可视化 网络安全
新手小白零基础阿里云轻量服务器部署OpenClaw(Clawdbot)喂饭级教程
在AI自动化工具全民普及的2026年,OpenClaw(原Clawdbot、Moltbot)凭借“自然语言指令+任务主动执行”的核心优势,成为小白、职场人、轻量团队搭建专属AI助手的首选工具。它打破了传统AI“只能聊天、无法落地”的局限,无需专业编程知识,仅需输入日常口语化指令,就能完成文件管理、日程提醒、代码生成、网页抓取、跨工具协同等各类重复性工作,被网友亲切称为“私人AI数字员工”。
1310 8
|
小程序 JavaScript 开发工具
Uniapp 对接抖音短剧播放器 video-player 坑点解决
Uniapp 对接抖音短剧播放器 video-player 坑点解决
1113 1
|
11月前
|
缓存 网络协议 安全
若无 DNS 与代理 IP,我们的上网体验会崩塌吗?
DNS是互联网“快递员”,负责将域名精准解析为IP地址;代理IP则是“神秘信使”,隐匿真实身份、中转请求,保障隐私与访问自由。二者协同如接力赛:DNS先定位代理,代理再查目标IP,共同构建高效、安全、灵活的网络访问通路。
|
缓存 NoSQL Redis
[Nestjs] 使用redis的基础配置,以及基本使用的介绍
要在 NestJS 中使用 Redis,需要安装依赖npm install --save redis fastify-redis
1036 0
|
数据采集 JavaScript 搜索推荐
我们一起聊聊如何对Vue项目进行搜索引擎优化
【9月更文挑战第4天】Vue 项目的搜索引擎优化(SEO)较为复杂,因其内容默认由 JavaScript 渲染,部分搜索引擎难以索引。为提升 SEO 效果,可采用服务器端渲染(SSR)或预渲染,使用 Nuxt.js 或 Vue Server Renderer 实现 SSR,或利用 Prerender SPA Plugin 预渲染静态 HTML。此外,动态管理 Meta 标签、优化静态内容与 Sitemap、懒加载、图片优化、提升页面速度、设置正确的路由模式、使用结构化数据及构建良好外链均有益于 SEO。
965 11
|
算法 机器人 中间件
C++的主要优势是什么?
C++的主要优势是什么?
1036 1
|
Dart
Flutter之ExpansionTile实现以代码方式码折叠、展开
Flutter之ExpansionTile实现以代码方式码折叠、展开
629 4
|
Python
PyCharm View as Array 查看数组
PyCharm View as Array 查看数组
898 1