Codeforces 748D Santa Claus and a Palindrome
雅礼集训期间我好像考完试就开始划水了啊
给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大.
如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在最中间放一个回文的串,求一下这种情况带来的额外的收入即可.
卡自然溢出hash....需要树同构那种奇奇怪怪的hash...
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef unsigned long long ul;
const int maxn=100005;
int k,n;
string str[maxn];
ul Ha1[maxn],Ha2[maxn];
int w[maxn];
ul gethash1(string &A){
ul res=233;
for(int i=0;i<n;++i)res=(res*173323+A[i]+987)<<2^(res>>5);
return res;
}
ul gethash2(string &A){
ul res=233;
for(int i=n-1;i>=0;--i)res=(res*173323+A[i]+987)<<2^(res>>5);
return res;
}
ul Ha[maxn][2];
vector<int> a[maxn];
map<ul,int> dict;int tot=0;
map<ul,int> dict2;
long long work1(){
long long ans=0;
for(int i=1;i<=tot;++i){
if(Ha[i][0]==Ha[i][1]){
int SZ=a[i].size();
for(int j=SZ-1;j>=1&&a[i][j]+a[i][j-1]>0;j-=2){
ans+=a[i][j]+a[i][j-1];
}
}else if(Ha[i][0]<Ha[i][1]){
int t=dict[Ha[i][1]];
int SZ1=a[i].size(),SZ2=a[t].size();
for(int j=0;j<SZ1&&j<SZ2&&a[i][SZ1-j-1]+a[t][SZ2-j-1]>0;++j){
ans=ans+a[i][SZ1-j-1]+a[t][SZ2-j-1];
}
}
}
return ans;
}
long long work2(){
long long ans=0;
long long maxdelta=0;
for(int i=1;i<=tot;++i){
if(Ha[i][0]==Ha[i][1]){
int SZ=a[i].size();
if(SZ==1&&a[i][0]>maxdelta)maxdelta=a[i][0];
if(SZ>=2&&a[i][SZ-1]>maxdelta&&a[i][SZ-1]+a[i][SZ-2]<=0)maxdelta=a[i][SZ-1];
for(int j=SZ-1;j>=1&&a[i][j]+a[i][j-1]>0;j-=2){
ans+=a[i][j]+a[i][j-1];
if(a[i][j-1]<0&&-a[i][j-1]>maxdelta){
maxdelta=-a[i][j-1];
}
if(j>=3&&a[i][j-2]>maxdelta&&a[i][j-2]+a[i][j-3]<=0)maxdelta=a[i][j-2];
if(j==2&&a[i][0]>maxdelta)maxdelta=a[i][0];
}
}else if(Ha[i][0]<Ha[i][1]){
int t=dict[Ha[i][1]];
int SZ1=a[i].size(),SZ2=a[t].size();
for(int j=0;j<SZ1&&j<SZ2&&a[i][SZ1-j-1]+a[t][SZ2-j-1]>0;++j){
ans=ans+a[i][SZ1-j-1]+a[t][SZ2-j-1];
}
}
}
return ans+maxdelta;
}
int main(){
cin>>k>>n;
for(int i=1;i<=k;++i)cin>>str[i]>>w[i];
int t;
for(int i=1;i<=k;++i){
Ha1[i]=gethash1(str[i]);
Ha2[i]=gethash2(str[i]);
if(Ha1[i]==Ha2[i]){
if(dict2[Ha1[i]]){
t=dict2[Ha1[i]];
}else{
dict2[Ha1[i]]=t=++tot;
Ha[tot][0]=Ha[tot][1]=Ha1[i];
}
a[t].push_back(w[i]);
}else{
if(dict[Ha1[i]]){
t=dict[Ha1[i]];
}else{
dict[Ha1[i]]=t=++tot;
Ha[tot][0]=Ha1[i];Ha[tot][1]=Ha2[i];
}
a[t].push_back(w[i]);
}
}
long long ans=0;
for(int i=1;i<=tot;++i)sort(a[i].begin(),a[i].end());
printf("%lld\n",max(work1(),work2()));
return 0;
}
Codeforces 748D Santa Claus and a Palindrome的更多相关文章
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL
D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Santa Claus and a Palindrome
Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...
- 【Codeforces752D】Santa Claus and a Palindrome [STL]
Santa Claus and a Palindrome Time Limit: 20 Sec Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...
- 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 standa ...
- Codeforces 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
随机推荐
- 一个命令安装lnmp
安装LNMP执行:wget -c http://soft.vpser.net/lnmp/lnmp1.3-full.tar.gz && tar zxf lnmp1.3-full.tar. ...
- 暗通道去雾算法的python实现
何凯明博士的去雾文章和算法实现已经漫天飞了,我今天也就不啰里啰唆,直接给出自己python实现的完整版本,全部才60多行代码,简单易懂,并有简要注释,去雾效果也很不错. 在这个python版本中,计算 ...
- 洛谷P2831 愤怒的小鸟
洛谷P2831 愤怒的小鸟 原题链接 题解 首先简单数学公式送上. \(ax_1^2+bx_1=y_1\) \(ax_2^2+bx_2=y_2\) \(ax_1^2x_2+bx_1x_2=y_1x_2 ...
- 车牌,车架号,VIN码毫秒识别技术,汽车后市场的春天到来了
vin码(车架号)识别运用 不仅在制造.销售.保养.保险.车辆评估.交易环节会需要录入汽车的VIN码,在交通事故处理中,作为汽车身份唯一识别码,VIN码是处理事故的执法人员必须要记录的信息之一.随着汽 ...
- Git之hotfix热修复分支
1.假设你正在开发一个新功能, 需要新建一个new分支并切换: git checkout -b new 等价于 git branch new git checkout new 然后在这个new分支上已 ...
- http 502 bad gate way
世界杯期间,公司的cdn在回源时突然出现大量502. 刚出现问题时,因为考虑到一般502都是上游服务器出现问题,然后因为已经服务了很久都没有出现过这种问题, 就没有仔细考虑,就让回源中心的同事进行排查 ...
- jupyter通过notedown使用markdown
0 Problem 最近看了下李沐老师的mxnet教程,在使用jupyter的时候打开教程发现全是markdown源文,没有展示markdown格式的文字. 1 Reason 源代码是用markdow ...
- Golang项目开发管理
工具 1. task(项目管理,类似于make) go get -u -v github.com/go-task/task/cmd/task 2. gopm(go依赖管理) go get -u git ...
- Linux sync命令的作用分析
Sync命令 在用reboot命令启动unix系统后,系统提示出错信息,部分应用程序不能正常工作.经仔细检查系统文件,并和初始的正确备份进行比较,发现某些文件确实被破坏了,翻来覆去找不到文件遭破坏 ...
- redis 学习记录
http://www.yiibai.com/redis/redis_quick_guide.html Redis 是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and s ...