[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 ...
随机推荐
- 零基础Python应该怎样学习呢?(附视频教程)
Python应该怎样学习呢? 阶段一:适合自己的学习方式 对于零基础的初学者来说,最迷茫的是不知道怎样开始学习?那这里小编建议可以采用视频+书籍的方式进行学习.看视频学习可以让你迅速掌握编程的基础语法 ...
- 图解Java设计模式之设计模式七大原则
图解Java设计模式之设计模式七大原则 2.1 设计模式的目的 2.2 设计模式七大原则 2.3 单一职责原则 2.3.1 基本介绍 2.3.2 应用实例 2.4 接口隔离原则(Interface S ...
- Git的基本使用 -- 文件的添加、撤销、对比、删除
显示当前工作区.暂存区.仓库的状态 git status 当工作区的所有文件都提交到仓库,并和仓库保持一致时 有修改的文件时,会显示有改动的文件,并提示如何提交这些修改 添加到暂存区,还未提交到仓库时 ...
- Mac 下如何快速重启 Dock 栏?
两种方法. 如果Dock栏出现了问题或是没有反应,请打开Launchpad并按下Command+D键. 这样就可以关闭Dock栏并重启它,效果和经常用到的killall Dock命令相同.
- 流程图GGEditor 之 自定义节点相关属性
自定义节点 注册 -- registerNode 我们通过以下接口往 G6 全局注册节点: // 注册节点 G6.registerNode(name, { // 绘制 draw(item) { r ...
- 使用Python库paramiko登录远程设备
前言 手动下载paramiko库的安装包.在PyPi库中查找即可,但是不到是我的电脑问题还是网络问题,2.0.0以上版本我都安装不了,因此我自己是安装的paramiko 1.17.0版本,此版本经过测 ...
- 剑指offer-面试题21-调整数组顺序使奇数位于偶数前面-双指针
/* 题目: 调整数组顺序使奇数位于偶数前面. */ /* 思路: 双指针: 一个指针last用于遍历,当为奇数时+1, 当为偶数时,交换last和pre指向的值,向前移动pre指针. */ #inc ...
- pygame 运行心理学问卷
import pygame import sys from pygame.locals import * # wait for keys to putdown def waitForKeys(keys ...
- xctf-i-got-id-200(perl网页文件+ARGV上传造成任意文件读取)
打开url发现有三个链接,点进去都是.pl文件,且只有files可以上传文件. .pl文件都是用perl编写的网页文件 这里上传了又将文件的内容全部打印出来,那么猜想后台应该用了param()函数. ...
- codechef Counting D-sets
难度 \(hard\) 题意 官方中文题意 做法 为避免数重,强制每维至少有一个\(0\),这个可以简单容斥 直径恰好为\(d\),不好求,也容斥一下