Codeforces 1304B. Longest Palindrome
根据数据范围,暴力可以解决,对每一个串,找与其互为回文的串,或者判断自身是否为回文串,然后两两将互为回文的串排列在头尾,中间放且只能最多放一个自身为回文串的串,因为题目说每个串都是不同的
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; string str[];
int cache[], self[], s[], chosen[]; void run_case() {
int n, m;
cin >> n >> m;
for(int i = ; i <= n; ++i)
cin >> str[i];
for(int i = ; i <= n; ++i) {
bool flag = true;
for(int j = ; j < m/ && flag; ++j) {
if(str[i][j] != str[i][m-j-]) flag = false;
}
if(flag) {
self[i] = ;
continue;
}
if(cache[i]) continue;
string now = str[i];
reverse(now.begin(), now.end());
for(int j = ; j <= n; ++j) {
if(j != i && now == str[j]) {
cache[i] = j; break;
}
}
}
vector<int> ans, selfs;
int top = ;
for(int i = ; i <= n; ++i)
if(cache[i] && !chosen[i]) {
ans.push_back(i);
s[++top] = cache[i];
chosen[cache[i]] = i;
} else if(self[i]) selfs.push_back(i);
if(selfs.size()) ans.push_back(selfs[]);
while(top) {
ans.push_back(s[top--]);
}
cout << ans.size() * m << "\n";
for(auto i : ans)
cout << str[i];
} int main() {
ios::sync_with_stdio(false), cin.tie();
//cout.setf(ios_base::showpoint);cout.precision(10);
//int t; cin >> t;
//while(t--)
run_case();
cout.flush();
return ;
}
Codeforces 1304B. Longest Palindrome的更多相关文章
- Codeforces Round #620 (Div. 2) B. Longest Palindrome
Returning back to problem solving, Gildong is now studying about palindromes. He learned that a pali ...
- [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) ...
随机推荐
- 算法进阶:0x01 位运算
一.快速幂的模板代码 a^b%p: #include<iostream> using namespace std; int main() { int a,b,p; cin>>a ...
- java.lang.NoSuchMethodException: com.sun.tools.javac.util.List.<init>()
主要原因是import jar包的时候import java.util.List;这个,导致错误
- 1、安装GPIO Zero(Installing GPIO Zero)
学习目录:树莓派学习之路-GPIO Zero 官网地址:http://gpiozero.readthedocs.io/en/stable/installing.html 环境:UbuntuMeta-1 ...
- Java常量/变量
1. 常量 /* 常量:在程序运行期间,固定不变的量. 常量的分类: 1. 字符串常量:凡是用双引号引起来的部分,叫做字符串常量.例如:"abc"."Hello" ...
- 2020qbxt D1T3 停车
嗯... 题目: [问题描述] 市中心有一个环形的停车场,编号1到n,现在有m个车要停,停在每个位置会有不同的费用.为了方便,不允许两辆车停在相邻的位置,请问停好所有车的最小花费是多少? [输入格式] ...
- redis-py相关
一 redis客户端命令 cmd进入redis客户端管理程序路径xx:\windows redis\redis-2.4.0-win32-win64\64bit 执行:redis-cli.exe -h ...
- MYSQL命令练习及跳过数据库密码进行密码重新设置
2.看当前所有数据库:show databases; 3.进入mysql数据库:use mysql; 4.查看mysql数据库中所有的表:show tables; 5.查看user表中的数据: ...
- 反编译 java
1.winrar https://www.rarlab.com/ 2.github jd-gui http://java-decompiler.github.io/ SignNatureTest.j ...
- next_permutation的使用-Hdu1027
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- Web--Response
using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using ...