雅礼集训期间我好像考完试就开始划水了啊

给出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的更多相关文章

  1. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

  2. 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 ...

  3. Santa Claus and a Palindrome

    Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...

  4. 【Codeforces752D】Santa Claus and a Palindrome [STL]

    Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...

  5. 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 ...

  6. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...

  7. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  9. [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)

    题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...

随机推荐

  1. 20155325 信息安全技术 实验二 Windows口令破解

    内容一览 实验结果 遇到的问题 思考题 详细步骤与解析(为了使存做笔记资料而做) 实验结果 字典破解 暴力破解 -不同密码强度的破解时间比较 用户名 密码 破解方式 破解时间 TEST (年月日) 字 ...

  2. js的视频和音频采集

    js的视频和音频采集 今天要写的,不是大家平时会用到的东西.因为兼容性实在不行,只是为了说明下前端原来还能干这些事. 大家能想象前端是能将摄像头和麦克风的视频流和音频流提取出来,再为所欲为的么.或者说 ...

  3. idea 单元测试 mybatis spring-test 异常: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    因为在idea中必须在test下才能进行单元测试,所以进行单元测试时,ssm的项目会因为找不到resourece中的配置文件而报错 这里 org.apache.ibatis.binding.Bindi ...

  4. The filename 未命名.ipa in the package contains an invalid character(s). The valid characters are: A-Z, a-z, 0-9, dash, period, underscore, but the name cannot start with a dash, period, or underscore

    The filename 未命名.ipa in the package contains an invalid character(s).  The valid characters are: A-Z ...

  5. hexo部署

    title: hexo 部署(一) date: 2018-09-16 18:01:26 tags: hexo部署配置 categories: 博客搭建 hexo博客搭建 折腾了好久的时间,终于使用he ...

  6. redis 批量删除操作

    redis 批量删除操作 需要在redis里面清空一批数据,redis没有支持通配符删除, 只有del key1 key2 ... 但是可以通配符获取 KEYS PATTERN 然后利用linux管道 ...

  7. Pycharm实现服务器端代码的远程调试

     Pycharm是很多人在学习机器学习时的常用IDE.但是,当代码需要庞大计算资源的时候,我们往往需要借助远程服务器的GPU资源.很多人都是将代码拷贝到服务器,然后运行,但是当修改调试的时候,很不方便 ...

  8. ecshop以及一些需要注意的

    Deprecated: Assigning the return value of new by reference is deprecated in 定位到出错的那一行: $this->_ol ...

  9. 欢迎来怼--第三十次Scrum会议

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/18 17:20~17:53,总计33min. 地 ...

  10. 《JavaScript》JS中的跨域问题

    参考博客:https://www.cnblogs.com/yongshaoye/p/7423881.html