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) ...
随机推荐
- Dataguard单机—>单机
本演示案例所用环境: primary Standby OS Hostname CHINA-DB1 CHINA-DB2 OS Version SUSE Linux Enterprise Server 1 ...
- mcast_get_loop函数
#include <errno.h> #include <net/if.h> #include <sys/socket.h> #include <netine ...
- HeroM2连击技能设置和DB完整数据
连击技能设置: M2\选项\功能设置\技能魔法\通用技能\连击技能 魔法DB: 81;倚天辟地;0;55;5;10;10;5;6;6;99;15;5;15;10;15;15;60;; 300;万剑归宗 ...
- Python使用Tensorflow出现错误: UserWarning: The default mode, 'constant'
Python使用Tensorflow出现错误: UserWarning: The default mode, 'constant', will be changed to 'reflect' in s ...
- JavaScript的变量提升机制
变量提升 JavaScript的变量提升有两种,用var声明的变量以及用function声明的变量. 用var声明的变量 我们先来看下面这段代码,a的值是多少 代码1 console.log(a); ...
- 慕课网:剑指Java面试-Offer直通车视频课程
慕课网:剑指Java面试-Offer直通车视频课程,一共有10个章节. 目录结构如下: 目录:/2020036-慕课网:剑指Java面试-Offer直通车 [6G] ┣━━第10章 Java常用类库与 ...
- win10配置cuda和pytorch
简介 pytorch是非常流行的深度学习框架.下面是Windows平台配置pytorch的过程. 一共需要安装cuda.pycharm.anancoda.pytorch. 主要介绍cuda和pytor ...
- logback.xml设置mogodb日志打印控制台
<logger name="org.springframework.data.mongodb.core" level="DEBUG"/>
- yaml服务部署示例
apiVersion: apps/v1kind: Deploymentmetadata: name: igirl namespace: chaolai labels: app: igirl ...
- iOS APP语言国际化之应用内切换语言环境
最近接了一个项目,需求是要做一款应用的英文版本,客户并不清楚,以为要另做一个APP.沟通后告诉他们在之前应用基础上加个国际化功能就好,把之前的语言国际化重新梳理记录一下. 一般设置更改本地语言环境后, ...