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

给出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. print puts p

    共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将先处理转义再输出p 基本与puts相同,但不会处理 ...

  2. [NOIp2018]铺设道路 贪心

    LG传送门 考场上写的\(O(nlogn)\)做法,具体思想是把深度从低到高排个序,开一个标记数组,每次加入的时候标记当前位置并判断:如果当前加入的位置两边都被标记过,则下次的贡献-1,若两边都没有被 ...

  3. 2017"百度之星"程序设计大赛 - 初赛(A) 小C的倍数问题

    谢谢帮忙刷访问量! 题解当然下考再发啦 答案为P-1的约数个数 // It is made by XZZ #include<cstdio> #include<algorithm> ...

  4. JS基础,课堂作业,三个数字排序

    三个数字大小排序 <script> var a = parseInt(prompt("请输入第一个整数:")); var b = parseInt(prompt(&qu ...

  5. 8个数据清洗Python代码,复制可用,最长11行 | 资源

    最近,大数据工程师Kin Lim Lee在Medium上发表了一篇文章,介绍了8个用于数据清洗的Python代码. 数据清洗,是进行数据分析和使用数据训练模型的必经之路,也是最耗费数据科学家/程序员精 ...

  6. 关闭会声会影2018提示UEIP.dll找不到指定模块

    最近有一些会声会影2018用户反映在关闭后弹出UEIP.dll错误,不知道该怎么办才好,针对这个问题,小编下面为大家介绍下解决方法. 原因分析 出现这个错误跟会声会影安装路径有中文字符是密切相关的,导 ...

  7. Linux系统网络安装——基于pxe+dhcp+nfs+tftp+kickstart

    原文发表于:2010-09-05 转载至cu于:2012-07-21 一.原理简介 PXE(preboot execute environment)工作于Client/Server的网络模式,支持工作 ...

  8. 在香港网站使用工商银行的MasterCard,工商银行所犯的低级的错误,金融安全何在

  9. We are writing to let you know we have removed your selling privileges

     Hello, We are writing to let you know we have removed your selling privileges, canceled your listin ...

  10. Scrum立会报告+燃尽图(Beta阶段第二周第三次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411 项目地址:https://coding.net/u/wuyy694 ...