[CF1034B] Longest Palindrome - 贪心

如果自己是回文串可以做中心
如果一个串和另一个串的转置相等则可以凑一对
优先配对
#include <bits/stdc++.h>
using namespace std;
int n,m,u[105];
string str[105];
vector <int> a,b;
signed main() {
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>str[i];
int ans=0;
for(int i=1;i<=n;i++) {
if(u[i]) continue;
for(int j=i+1;j<=n;j++) {
int flag=1;
for(int k=0;k<m;k++) if(str[i][k]!=str[j][m-k-1]) flag=0;
if(flag) {
u[i]=1;
u[j]=1;
a.push_back(i);
b.push_back(j);
ans+=2;
break;
}
}
}
int flag=0;
for(int i=1;i<=n;i++) {
int tmp=1;
for(int k=0;k<m;k++) if(str[i][k]!=str[i][m-k-1]) tmp=0;
if(tmp) flag=i;
}
if(flag) ++ans;
cout<<ans*m<<endl;
for(int i=0;i<a.size();i++) cout<<str[a[i]];
cout<<str[flag];
for(int i=b.size()-1;i>=0;--i) cout<<str[b[i]];
}
[CF1034B] Longest Palindrome - 贪心的更多相关文章
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- LeetCode 409 Longest Palindrome
Problem: Given a string which consists of lowercase or uppercase letters, find the length of the lon ...
- LeetCode Longest Palindrome
原题链接在这里:https://leetcode.com/problems/longest-palindrome/ 题目: Given a string which consists of lower ...
- 每天一道LeetCode--409 .Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 24. leetcode 409. Longest Palindrome
409. Longest Palindrome Given a string which consists of lowercase or uppercase letters, find the le ...
- [Swift]LeetCode409. 最长回文串 | Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
随机推荐
- js this是什么?[多次书写]
前言 以前的时候,我写了一个关于js this的博客,写的非常复杂,分析了各种情况. 现在我想简化. 如果你有后台基础,专门去理解过this,那么请忘记. 这东西是有口诀的: 在方法中,this 表示 ...
- Android开发当中ListView的使用
首先我们看ListView实现之后的的效果,如下图所示: 现在我们来看看如何来实现这个可以进行上下活动的ListView: 首先是主界面Activity_Main.xml的代码: <?xml v ...
- MySQL基础(7) | 触发器
MySQL基础(7) | 触发器 基本语法 创建 CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EA ...
- 想在don‘t starve中活的更久?那饥荒海难攻略你怎么能不知道!
饥荒海难mac版是一款非常好玩的烧脑游戏.玩家将扮演一个勇敢的绅士科学家威尔逊,被一个恶魔困住并送到一个神秘的荒野世界,玩家必须利用异世界中的自然资源让自己存活下去,并且抵御各种异世界生物的威胁.想在 ...
- 折腾vue--使用vscode创建vue项目(二)
1.安装webpack npm install -g webpack 2.安装sass npm install --save-dev sass-loader npm install --save-de ...
- WebStorm 2019.3.1 永久破解
PS:动手能力强的来,手残的去淘宝买吧,大概15块钱1年.建议看完后在动手,有一个全局观,浪费不了多少时间 一. 下载破解补丁文件 链接:https://pan.baidu.com/s/16-rPPH ...
- npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
Mac 权限不够 前面加sudo 然后输入密码
- Dolphin Scheduler初始化Postgresql数据库失败
在执行sh script/create-dolphinscheduler.sh初始化数据库时报错: 07:05:03.070 [main] ERROR com.alibaba.druid.pool.D ...
- Java如何优雅地使用close()?
注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/java-clo ...
- SQL JOIN 的解析
1.SQL语句结构 select distinct < select_list > from < left_table > < join_type > joi ...