Codeforces Round #620 (Div. 2) B. 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.
Input
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.
Output
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.
Examples
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
大意是给定一些字符串,让你用他们尽可能的拼成最长的回文串。考stl的题,用string类会比较方便。可以先用一个vector存原string,另一个vector存原string reverse后的string(直接用reverse(s.begin(),s.end()),再建两个vector,一个存最终答案左半部分的string,一个存右边的。首先两重for循环找出能成对的string,对称的放进两个答案容器,并在该位置打上标记,然后再On扫一遍原字符串,遇到自身就是回文的可以直接加入
左半部分的尾或者右半部分的头(没有区别)最后统计完总长后依次输出两个答案容器的元素即可。
#include <bits/stdc++.h> using namespace std; int n,m; int main() { cin>>n>>m; vector<string>v1; vector<string>v2; vector<string>ans1; vector<string>ans2; int vis[100]={0}; int i,j; for(i=1;i<=n;i++) { string temp; cin>>temp; v1.push_back(temp); reverse(temp.begin(),temp.end()); v2.push_back(temp); } for(i=0;i<v1.size();i++) { //string s1=v1[i]; for(j=0;j<v1.size();j++) { if(v2[j]==v1[i]&&!vis[i]&&!vis[j]&&i!=j) { ans1.push_back(v1[i]); ans2.insert(ans2.begin(),v1[j]); vis[i]=1; vis[j]=1; } } } string dc=""; for(i=0;i<v1.size();i++) { if(!vis[i]) { string s1=v1[i]; string s2=v1[i]; reverse(s2.begin(),s2.end()); if(s1==s2) { dc=v1[i]; break; } } } ans1.push_back(dc); if(ans1.size()==0&&ans2.size()==0) { cout<<0; return 0; } long long size=0; for(i=0;i<ans1.size();i++)size+=ans1[i].size(); for(i=0;i<ans2.size();i++)size+=ans2[i].size(); cout<<size<<endl; for(i=0;i<ans1.size();i++)cout<<ans1[i]; for(i=0;i<ans2.size();i++)cout<<ans2[i]; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!