Codeforces #620 div2 B
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≤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.
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.
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
题意:给n个长度为m的不同字符串,找出回文串并拼接在一起,输出最长的回文串.
分析:输出的答案应为s1...s1+s2+s3...s3,其(s1,s3)为一对回文串,在他们的中间加上任意一个自身回文的字符串,这样题目就能直接出答案了,我们直接先遍历一边字符串数组,找到对回文串,并将其标记(防止他是自身回文在下面重复记录),然后再找自身回文(只需任意一个就行,多了反而不满足条件)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define ll long long
const int N=1e6+10;
using namespace std;
typedef pair<int,int>PII;
int n,m;
int q=0,p=0;
string s[N]; //字符串的容器
string ans1[N],ans2[N]; //记录回文串
string tmp; //记录反转后的字符串
map<string,int> w; //用来标记
int main(){
ios::sync_with_stdio(false);
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++){
tmp=s[j];
reverse(tmp.begin(),tmp.end());
if(s[i]==tmp){
w[s[i]]=2;
ans1[q++]=s[i];
break;
}
}
for(int i=0;i<n;i++){
tmp=s[i];
reverse(tmp.begin(),tmp.end());
if(s[i]==tmp && w[s[i]]!=2){
ans2[p++]=s[i];
break;
}
}
cout<<q*m*2+p*m<<endl;
for(int i=0;i<=q;i++){
cout<<ans1[i];
reverse(ans1[i].begin(),ans1[i].end());
}
cout<<ans2[0];
for(int i=q;i>=0;i--) cout<<ans1[i]; return 0;
}
Codeforces #620 div2 B的更多相关文章
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...
随机推荐
- Centos 7 下的KVM虚拟机
一 什么是KVM虚拟机: KVM虚拟机 Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中. ...
- python模块详解 | pyquery
简介 pyquery是一个强大的 HTML 解析库,利用它,我们可以直接解析 DOM 节点的结构,并通过 DOM 节点的一些属性快速进行内容提取. 官方文档:http://pyquery.readth ...
- Linux Clone函数
Linux Clone函数 之前某一次有过一次面试,问了内核中是怎么创建命名空间的? 下面就来扒一扒clone的精髓,以及如何通过它创建命名空间. 目录 Linux Clone函数 使用clone创建 ...
- 一. SpringCloud简介与微服务架构
1. 微服务架构 1.1 微服务架构理解 微服务架构(Microservice Architecture)是一种架构概念,旨在通过将功能分解到各个离散的服务中以实现对解决方案的解耦.你可以将其看作是在 ...
- 通过LOGMNR查找程式带入的实际值
生产库中出现了大量的锁表,需要得到当时程式执行的SQL以及其带入的值 1.查看SQL SELECT SQL_ID FROM V$SESSION WHERE SID=(SELECT FINAL_BLOC ...
- cts project的创建修改和删除
事务码:SPRO_ADMIN进入 项目管理界面,点击工具栏创建项目(F5),弹出对话框,输入项目名称,回车确定. 标题中输入项目的描述.点击保存.如图: 点击图片放大 注:要想此项目在CTS建立请求的 ...
- 06--Docker自定义镜像Tomcat9
1. 创建目录 /zhengcj/mydockerfile/tomcat9 2.将jdk和tomcat的安装包拷贝到tomcat9下 3.在tomcat9目录下创建Dockerfile文件,并写以下命 ...
- 微服务网关1-Spring Cloud Gateway简介
一.网关基本概念 1.API网关介绍 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各 ...
- html简单基础
标签语法 标签的语法: <标签名 属性1="属性值1" 属性2="属性值2"-->内容部分</标签名> <标签名 属性1=&quo ...
- 浅谈前端常用脚手架cli工具及案例
前端常用脚手架工具 前端有很多特定的脚手架工具大多都是为了特定的项目类型服务的,比如react项目中的reate-react-app,vue项目中的vue-cli,angular 项目中的angula ...