Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

 

time limit per test

2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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.

Output

In the only line print the required maximum possible beauty.

Examples
input
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
output
12
input
3 1
a 1
a 2
a 3
output
6
input
2 5
abcde 10000
abcde 10000
output
0
Note

In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).

 

map+堆+贪心

由于所有的字符串长度都相等,所以不必考虑不同字符串间的组合。

如果有两个字符串可以拼成回文字符串,且它们的价值和大于0,那么可以将这两个字符串一左一右添加进已有的串里。

↑字符串可能重复出现,为了贪心选取价值和最大的两个串加进已有串,可以先将它们的价值push进大根堆里(priority_queue)

为了建立字符串到堆下标的映射,再开一个map。codeforces评测机跑得飞快,不必担心时间。

之后在所有没有使用的回文串中,找到价值最大的,作为总串的中心。

(但是这还没有结束)

然而WA掉了。

发现一个bug:

例如

aba 10

aba -1

如果总共就这两个字符串,那么光添加一个aba显然比匹配一对aba收益大。

为此又加了一个“反悔”操作(第47行),AC

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 #include<map>
 8 using namespace std;
 9 const int mxn=100010;
10 map<string,int>mp;
11 priority_queue<int>p[mxn];
12 int cnt=0;
13 bool hw[mxn];
14 string s[mxn],c;
15 int score[mxn];
16 int n,k;
17 int ans=0;
18 bool pd(string ch){
19     int l=n/2;
20     for(int i=0;i<l;i++){
21         if(ch[i]!=ch[n-i-1])return 0;
22     }
23     return 1;
24 }
25 int main(){
26     int i,j,w;
27     cin>>k>>n;
28     for(i=1;i<=k;i++){
29         cin>>s[i]>>w;
30         if(!mp[s[i]])mp[s[i]]=++cnt;
31         p[mp[s[i]]].push(w);
32         if(!hw[mp[s[i]]]){
33             if(pd(s[i]))hw[mp[s[i]]]=1;
34         }
35     }
36     int mx=0;
37     for(i=1;i<=k;i++){
38         if(p[mp[s[i]]].empty())continue;
39         c=s[i];
40         reverse(c.begin(),c.end());
41         int t=mp[c];
42         int x=mp[s[i]];
43         w=p[x].top();
44         p[x].pop();
45         if(t && !p[t].empty() && p[t].top()+w>0){
46             if(p[t].top()*w<0 && hw[x]){
47                 mx=max(mx,max(p[t].top(),w)-p[t].top()-w);
48             }
49             ans+=p[t].top()+w;
50             p[t].pop();
51         }
52         else p[x].push(w);
53     }
54 //    printf("%d %d\n",ans,mx);
55     for(i=1;i<=cnt;i++){
56         if(hw[i] && !p[i].empty()){
57             mx=max(mx,p[i].top());
58         }
59     }
60     printf("%d\n",ans+mx);
61     return 0;
62 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6224308.html

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的大学生第二课堂系统,旨在为大学生提供一个便捷、高效的学习和实践平台。项目包含了完整的数据库设计、后端Java代码实现以及前端Vue.js页面展示,适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 在功能方面,系统主要实现了以下几个模块:用户管理、课程管理、活动管理、成绩管理和通知公告。用户管理模块支持学生和教师的注册、登录及权限管理;课程管理模块允许教师上传课程资料、设置课程时间,并由学生进行选课;活动管理模块提供了活动发布、报名和签到功能,鼓励学生参与课外实践活动;成绩管理模块则用于记录和查询学生的课程成绩和活动参与情况;通知公告模块则实时发布学校或班级的最新通知和公告。 技术实现上,后端采用SSM框架进行开发,Spring负责业务逻辑层,SpringMVC处理Web请求,MyBatis进行数据库操作,确保了系统的稳定性和扩展性。前端则使用Vue.js框架,结合Axios进行数据请求,实现了前后端分离,提升了用户体验和开发效率。 该项目不仅提供了完整的源代码和相关文档,还包括了详细的数据库设计文档和项目部署指南,为学习和实践提供了便利。对于基础较好的学习者,可以根据自己的需求在此基础上进行功能扩展和优化,进一步提升自己的技术水平和项目实战能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值