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

给出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. 课上实践练习——MySort

    模拟实现Linux下Sort -t : -k 2的功能.参考 Sort的实现.提交码云链接和代码运行截图. Linux下Sort -t : -k 2的功能 sort的工作原理: sort将文件的每一行 ...

  2. JS继承方法

    1.原型链: 每个构造函数都有一个原型对象,且有一个指针指向该原型对象(prototype),原型对象都包含一个指向构造函数的指针(constructor),而实例都包含一个指向原型对象的内部指针(p ...

  3. C、C++字符操作归总

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  4. CentOS 下 Java 的下载、安装、配置

    CentOS 下 Java 的下载.安装.配置 系统: CentOS 7 x86_64 Java 版本: 1.8.0_171 本文将 Java 目录放在 /usr/local/java 文件夹下,读者 ...

  5. mysql数据导到本地

    需求: 把mysql查询结果导出到txt(其他格式亦可),放在本地,供下一步使用 首先网上查了下,select * from driver into outfile 'a.txt'; 前面是你的sql ...

  6. 前端基础HTML

    web的服务本质 浏览器发送请求>>>HTTP协议>>>服务端接受请求>>>服务端返回响应>>>服务端把HTML文件内容发给浏览 ...

  7. [CF1137]Museums Tour

    link \(\text{Description:}\) 一个国家有 \(n\) 个城市,\(m\) 条有向道路组成.在这个国家一个星期有 \(d\) 天,每个城市有一个博物馆. 有个旅行团在城市 \ ...

  8. leetcode28_C++实现strStr()函数

    实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...

  9. 2019CSUST集训队选拔赛题解(三)

    PY学长的放毒题 Description 下面开始PY的香港之行,PY有n个要去的小吃店,这n个小吃店被m条路径联通起来. PY有1个传送石和n−1个传送石碎片. PY可以用传送石标记一个小吃店作为根 ...

  10. NO.3:自学python之路------集合、文件操作、函数

    引言 本来计划每周完成一篇Python的自学博客,由于上一篇到这一篇遇到了过年.开学等杂事,导致托更到现在.现在又是一个新的学期,春天也越来越近了(冷到感冒).好了,闲话就说这么多.开始本周的自学Py ...