CF2.D
2 seconds
256 megabytes
standard input
standard output
Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this string beautiful at all.
Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length n.
Recall that a palindrome is a string that doesn't change after one reverses it.
Since the empty string is a palindrome too, the answer can't be negative. Even if all ai's are negative, Santa can obtain the empty string.
The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k ≤ 100 000).
k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The string consists of n lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.
In the only line print the required maximum possible beauty.
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
12
3 1
a 1
a 2
a 3
6
2 5
abcde 10000
abcde 10000
0
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
题意:
共有k个字符串,每个长度为n,每个字符串有权值,问用这些字符串能组成的权值最大的回文串,输出权值。可以一个都不用最后权值为0,一个相同的字符串可能对应多个不同的权值。
代码:
//STL好强啊。共有两种情况,本身不是回文串的必须要有他的反串和他一起,本身是回文串的可以将一个放在中间,将一对
//放在两边。由于每个字符串可以由多个权值所以用的时候要尽量用权值大的。把他们放到优先队列中再map,用迭代器遍历,
//操作优先队列。当字符串是回文串并且多于一个时,有可能出现加入的第一个的权值>0,而第二个的权值<0,而此时又没有另
//一个正权值的回文串可以放在中间那么第二个权值<0的就不加入。
#include<bits\stdc++.h>
using namespace std;
char ch[];
struct cmp
{
bool operator()(int &a,int &b){
return a<b;
}
};
map<string,priority_queue<int,vector<int>,cmp> >mp;
int main()
{
int k,n,x;
scanf("%d%d",&k,&n);
for(int i=;i<k;i++){
scanf("%s %d",ch,&x);
string s=ch;
mp[s].push(x);
}
int ans=,maxn=,minn=;
for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
string s1=it->first;
string s2=s1;
reverse(s2.begin(),s2.end()); //翻转字符串
if(s1!=s2&&mp.find(s2)!=mp.end()){ //非回文串,并且存在相反串
while(!mp[s1].empty()&&!mp[s2].empty()){
int now=mp[s1].top()+mp[s2].top();
if(now>){
ans+=now;
mp[s1].pop();
mp[s2].pop();
}
else break;
}
}
else if(s1==s2){ //是回文串
while(mp[s1].size()>=){
int now1=mp[s1].top();
mp[s1].pop();
int now2=mp[s1].top();
mp[s1].pop();
if(now1+now2>){
ans+=(now1+now2);
minn=min(minn,now2); //记录
}
else{
mp[s1].push(now2);
mp[s1].push(now1);
break;
}
}
}
}
for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
string s1=it->first;
string s2=s1;
reverse(s2.begin(),s2.end());
if(s1==s2&&!mp[s1].empty()) //找一个放在中间的回文串
maxn=max(maxn,mp[s1].top());
}
printf("%d\n",max(ans-minn,ans+maxn));
return ;
}
CF2.D的更多相关文章
- 代码问题:【CF2】
[CF2/CFCF/HCF]: C Ma, JB Huang, X Yang, et al. Hierarchical convolutional features for visual tracki ...
- CF2.E
E. Comments time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- CF2.C
C. Vladik and fractions time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CF2.D 并查集+背包
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...
- CF2.BC
B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...
- CF2.C(二分贪心)
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- *CF2.D(哥德巴赫猜想)
D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- cf2.25
T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...
- BIRCH聚类算法原理
在K-Means聚类算法原理中,我们讲到了K-Means和Mini Batch K-Means的聚类原理.这里我们再来看看另外一种常见的聚类算法BIRCH.BIRCH算法比较适合于数据量大,类别数K也 ...
随机推荐
- WPF 通过Border来画边框
WPF有自己的表格控件DataGrid.ListBox等,如果只是简单的需求,可以通过Border控件来画边框. 比如我们需要给上面的控件加上边框. <Window x:Class=" ...
- splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目
删除位于 index 2 的元素,并添加一个新元素来替代被删除的元素: <script type="text/javascript"> var arr = new Ar ...
- 【转】arm 开发工具比较(ADS vs RealviewMDK vs RVDS)
ADS REALVIEW MDK RVDS 公司 ARM Keil(后被ARM收购) ARM 版本 最新1.2 ,被RVDS取代 最新4.0 是否免费 破解情况 有 有 工程管理 CodeWarr ...
- while 与do ..while区别
package com.chongrui.test;/*while语句的一般形式如下:while(表达式)语句表达式为非0值,执行while中内嵌语句.它是先判断表达式,在根据表达式值,执行语句.do ...
- 一、常见PHP网站安全漏洞
对于PHP的漏洞,目前常见的漏洞有五种.分别是Session文件漏洞.SQL注入漏洞.脚本命令执行漏洞.全局变量漏洞和文件漏洞.这里分别对这些漏洞进行简要的介绍. 1.session文件漏洞 Sess ...
- 电脑运行msi安装包提示the error code is 2503/2502如何解决
当在电脑中运行msi安装包时,出现the error code is 2503或者2502错误提示,其实是由于没有运行的权限导致的,但是又不能右击以管理员身份运行,那么应该如何操作呢?对于这样的问题, ...
- 我的 vim 基本配置
" required 使用 vundle 需要先设置这两项 set nocompatible filetype off " 设置 vundle 插件 使用帮助:https://gi ...
- android中如何用代码来关闭打开的相机
场景描述: 比如你再应用中打开了系统相机,然后需要在几分钟后自动关闭这个系统相机(不是手动关闭) 1.在activityA中利用startActivityForResult(intent,reques ...
- c语言结构体
[C语言]21-结构体 本文目录 一.什么是结构体 二.结构体的定义 三.结构体变量的定义 四.结构体的注意点 五.结构体的初始化 六.结构体的使用 七.结构体数组 八.结构体作为函数参数 九.指向结 ...
- Python学习日志(四)
列表 列表名 = [元素1,元素2,-] 列表和C中的数组有些相似,但列表的功能更加强大,数组的元素要求是同种类型,但列表可以是混合类型. 跟数组一样,通过元素的索引值,我们可以从列表获取单个数据. ...