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

给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大.

如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在最中间放一个回文的串,求一下这种情况带来的额外的收入即可.

卡自然溢出hash....需要树同构那种奇奇怪怪的hash...

  1. #include <cstdio>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <map>
  7. using namespace std;
  8. typedef unsigned long long ul;
  9. const int maxn=100005;
  10. int k,n;
  11. string str[maxn];
  12. ul Ha1[maxn],Ha2[maxn];
  13. int w[maxn];
  14. ul gethash1(string &A){
  15. ul res=233;
  16. for(int i=0;i<n;++i)res=(res*173323+A[i]+987)<<2^(res>>5);
  17. return res;
  18. }
  19. ul gethash2(string &A){
  20. ul res=233;
  21. for(int i=n-1;i>=0;--i)res=(res*173323+A[i]+987)<<2^(res>>5);
  22. return res;
  23. }
  24. ul Ha[maxn][2];
  25. vector<int> a[maxn];
  26. map<ul,int> dict;int tot=0;
  27. map<ul,int> dict2;
  28. long long work1(){
  29. long long ans=0;
  30. for(int i=1;i<=tot;++i){
  31. if(Ha[i][0]==Ha[i][1]){
  32. int SZ=a[i].size();
  33. for(int j=SZ-1;j>=1&&a[i][j]+a[i][j-1]>0;j-=2){
  34. ans+=a[i][j]+a[i][j-1];
  35. }
  36. }else if(Ha[i][0]<Ha[i][1]){
  37. int t=dict[Ha[i][1]];
  38. int SZ1=a[i].size(),SZ2=a[t].size();
  39. for(int j=0;j<SZ1&&j<SZ2&&a[i][SZ1-j-1]+a[t][SZ2-j-1]>0;++j){
  40. ans=ans+a[i][SZ1-j-1]+a[t][SZ2-j-1];
  41. }
  42. }
  43. }
  44. return ans;
  45. }
  46. long long work2(){
  47. long long ans=0;
  48. long long maxdelta=0;
  49. for(int i=1;i<=tot;++i){
  50. if(Ha[i][0]==Ha[i][1]){
  51. int SZ=a[i].size();
  52. if(SZ==1&&a[i][0]>maxdelta)maxdelta=a[i][0];
  53. if(SZ>=2&&a[i][SZ-1]>maxdelta&&a[i][SZ-1]+a[i][SZ-2]<=0)maxdelta=a[i][SZ-1];
  54. for(int j=SZ-1;j>=1&&a[i][j]+a[i][j-1]>0;j-=2){
  55. ans+=a[i][j]+a[i][j-1];
  56. if(a[i][j-1]<0&&-a[i][j-1]>maxdelta){
  57. maxdelta=-a[i][j-1];
  58. }
  59. if(j>=3&&a[i][j-2]>maxdelta&&a[i][j-2]+a[i][j-3]<=0)maxdelta=a[i][j-2];
  60. if(j==2&&a[i][0]>maxdelta)maxdelta=a[i][0];
  61. }
  62. }else if(Ha[i][0]<Ha[i][1]){
  63. int t=dict[Ha[i][1]];
  64. int SZ1=a[i].size(),SZ2=a[t].size();
  65. for(int j=0;j<SZ1&&j<SZ2&&a[i][SZ1-j-1]+a[t][SZ2-j-1]>0;++j){
  66. ans=ans+a[i][SZ1-j-1]+a[t][SZ2-j-1];
  67. }
  68. }
  69. }
  70. return ans+maxdelta;
  71. }
  72. int main(){
  73. cin>>k>>n;
  74. for(int i=1;i<=k;++i)cin>>str[i]>>w[i];
  75. int t;
  76. for(int i=1;i<=k;++i){
  77. Ha1[i]=gethash1(str[i]);
  78. Ha2[i]=gethash2(str[i]);
  79. if(Ha1[i]==Ha2[i]){
  80. if(dict2[Ha1[i]]){
  81. t=dict2[Ha1[i]];
  82. }else{
  83. dict2[Ha1[i]]=t=++tot;
  84. Ha[tot][0]=Ha[tot][1]=Ha1[i];
  85. }
  86. a[t].push_back(w[i]);
  87. }else{
  88. if(dict[Ha1[i]]){
  89. t=dict[Ha1[i]];
  90. }else{
  91. dict[Ha1[i]]=t=++tot;
  92. Ha[tot][0]=Ha1[i];Ha[tot][1]=Ha2[i];
  93. }
  94. a[t].push_back(w[i]);
  95. }
  96. }
  97. long long ans=0;
  98. for(int i=1;i<=tot;++i)sort(a[i].begin(),a[i].end());
  99. printf("%lld\n",max(work1(),work2()));
  100. return 0;
  101. }

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. 20155328 实验四 Android程序设计 实验报告

    20155328 实验四 Android程序设计 第24章 初识Android 提交点1:完成HelloWorld并显示自己的学号 安装Android Studio后,创建了属于自己的Project( ...

  2. WPF Window背景半透明 ,蒙版操作实现

    原文:WPF Window背景半透明 ,蒙版操作实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/detail ...

  3. ConfigurationManager 读写AppSettings键值对

    using System; using System.Configuration; namespace ConsoleApplication1 { class Program { static voi ...

  4. PostgreSQL通过mysql_fdw访问MySQL数据库

    Mysql与PostgreSQL的安装过程省略. 为简便起见,把MySQL和PostgreSQL都安装在一个机器上,然后在此机器上(准确地说是在PostgreSQL运行的机器上)安装mysql_fdw ...

  5. zabbix最新版3.4搭建(根据官方文档适当修改)

    操作系统:CentOS Linux release 7.4.1708 (Core) 1.安装apache 1.1 安装apache yum install httpd httpd-devel 1.2 ...

  6. day4 CSS属性操作

    1.CSS属性 基本属性 height, 高度 百分比 width, 宽度 像素,百分比 text-align:ceter, 水平方向居中 line-height, 垂直方向根据标签高度 color. ...

  7. 9.14 DP合集水表

    9.14 DP合集水表 关键子工程 在大型工程的施工前,我们把整个工程划分为若干个子工程,并把这些子工程编号为 1. 2. --. N:这样划分之后,子工程之间就会有一些依赖关系,即一些子工程必须在某 ...

  8. R Language Learn Notes

    One #install package install.packages("ggplot2") #load library library(ggplot2) #update.pa ...

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

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

  10. Mybatis-Plus的填坑之路 - Lynwood/wunian7yulian

    目录 Mybatis-Plus 我来填坑~ 目录 一.简单介绍 官方说明 : 成绩: 最新版本: 开发层面MyBatis-Plus特色 Mybatis-Plus中的Plus 二.MP的特性 三.MP框 ...