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; ...
随机推荐
- jenkins + Ansible Plugin + ansi-color 让结果显示颜色
1 安装jenkins: 此处省略百余字...... 2 安装jenkins的插件: Ansible Plugin AnsiColor Plugin 3 设置job 内容 让ansible ...
- Logrotate工具使用
Logrotate logrotate是一个被设计来简化系统管理日志文件的工具,在系统运行时,如果产生大量的日志文件,可以使用该工具进行管理,如/var/log/*文件夹是存储系统和应用日志的目录 ...
- python学习笔记 | 顺序表的常规操作
''' @author: 人人都爱小雀斑 @time: 2020/3/11 8:46 @desc: 顺序表的相关操作 ''' class SequenceList: def __init__(self ...
- EXPORT和IMPORT使用示例
1 report ztestprog. 2 data:begin of itab1 occurs 0, 3 ff(10), 4 end of itab1. 5 data:itab2 like itab ...
- Jenkins 部署打包文件 并通过SSH上传到 linux服务器
编译 发布 打包成zip文件 dotnet clean : dotnet的命令清除解决方案 dotnet build : dotnet的命令重新生成 dotnet publish .\Hy.MyDem ...
- CSS不用背景图片实现优惠券样式反圆角,凹圆角,反向半圆角,并且背景渐变
日常开发过程中,特别是商城相关应用开发过程中,时常会遇到花里胡哨的设计图,比如优惠券样式,上图: 实现思路如下: 1.先写一个外容器,实现背景色渐变: Html: 1 <div clas ...
- 用CSS制做一个三角形!
用CSS制做一个三角形! <style> .outer { width: 0; height: 0; border-left: 10px solid transparent; border ...
- Android 8.0/9.0 wifi 自动连接评分机制
前言 Android N wifi auto connect流程分析 Android N selectQualifiedNetwork分析 Wifi自动连接时的评分机制 今天了解了一下Wifi自动连接 ...
- mail Header Injection Exploit
Preventing Email Header Injection - PHundamental PHP Best Practices - http://nyphp.org/phundamentals ...
- 1.kafka基础架构
kafka基础架构 ## 什么是kafka? Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域. 1.什么是消息队列? 2.使用消息队列的好处 1)解耦 允许你独立的 ...