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; ...
随机推荐
- zabbix 监控的数据
/usr/local/zabbix/bin/zabbix_sender --zabbix-server 192.168.1.10 --port 10051 --input-file /var/log/ ...
- 2021年正确的Android逆向开发学习之路
2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...
- MAVEN编译NIFI源码
场景: 由于项目需求,需要借用NIFI进行二次开发,因此需要将NIFI源码进行修改,然后编译,办公环境无外网. 步骤: (1) 找一台可以上网(外网)的机器,安装java环境和maven环境,安装 ...
- C#使用OracleParameter操作数据库
public static int GetScalar(string sql,params OracleParameter [] OracleParms) { using (OracleConnect ...
- 训练分类器 - 基于 PyTorch
训练分类器 目前为止,我们已经掌握了如何去定义神经网络.计算损失和更新网络中的权重. 关于数据 通常来讲,当你开始处理图像.文字.音频和视频数据,你可以使用 Python 的标准库加载数据进入 Num ...
- Git 创建新分支检查分支
创建分支和切换分支,也可以称为检出分支 创建新分支 git branch branchName 切换到新分支 git checkout branchName 上面两个命令也可以合成为一个命令: git ...
- Spring基于注解开发的注解使用之AOP(部分源代码分析)
AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...
- 配置HDFS的HA
配置前准备: -- 配置hadoop -- 配置ZooKeeper,传送门:https://www.cnblogs.com/zhqin/p/11906106.html 安装配置好hadoop和ZooK ...
- 服务端渲染 数据驱动 不是渲染后的网页,而是一个由html和Javascript组成的app ssr 隐藏接口服务器
小结: 1. 服务端渲染主要的工作是把组件渲染为服务器端的 HTML 字符串,将它们直接发送到浏览器,最后将静态标记"混合"为客户端上完全交互的应用程序. 服务器给到客户端的已经是 ...
- mysql和oracle的字符拼接方法
不同的数据库,相应的字符串拼接方式不同,通过对比加深一下记忆. 一.MySQL字符串拼接 1.CONCAT函数 语法格式:CONCAT(char c1, char c2, ..., char cn) ...