zoukankan      html  css  js  c++  java
  • CF2.D

    D. Santa Claus and a Palindrome
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this string beautiful at all.

    Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length n.

    Recall that a palindrome is a string that doesn't change after one reverses it.

    Since the empty string is a palindrome too, the answer can't be negative. Even if all ai's are negative, Santa can obtain the empty string.

    Input

    The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k  ≤ 100 000).

    k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The string consists of n lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.

    Output

    In the only line print the required maximum possible beauty.

    Examples
    input
    7 3
    abb 2
    aaa -3
    bba -1
    zyz -4
    abb 5
    aaa 7
    xyx 4
    output
    12
    input
    3 1
    a 1
    a 2
    a 3
    output
    6
    input
    2 5
    abcde 10000
    abcde 10000
    output
    0
    Note

    In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).

     题意:

    共有k个字符串,每个长度为n,每个字符串有权值,问用这些字符串能组成的权值最大的回文串,输出权值。可以一个都不用最后权值为0,一个相同的字符串可能对应多个不同的权值。

    代码:

     1 //STL好强啊。共有两种情况,本身不是回文串的必须要有他的反串和他一起,本身是回文串的可以将一个放在中间,将一对
     2 //放在两边。由于每个字符串可以由多个权值所以用的时候要尽量用权值大的。把他们放到优先队列中再map,用迭代器遍历,
     3 //操作优先队列。当字符串是回文串并且多于一个时,有可能出现加入的第一个的权值>0,而第二个的权值<0,而此时又没有另
     4 //一个正权值的回文串可以放在中间那么第二个权值<0的就不加入。
     5 #include<bitsstdc++.h>
     6 using namespace std;
     7 char ch[100005];
     8 struct cmp
     9 {
    10     bool operator()(int &a,int &b){
    11         return a<b;
    12     }
    13 };
    14 map<string,priority_queue<int,vector<int>,cmp> >mp;
    15 int main()
    16 {
    17     int k,n,x;
    18     scanf("%d%d",&k,&n);
    19     for(int i=0;i<k;i++){
    20         scanf("%s %d",ch,&x);
    21         string s=ch;
    22         mp[s].push(x);
    23     }
    24     int ans=0,maxn=0,minn=0;
    25     for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
    26         string s1=it->first;
    27         string s2=s1;
    28         reverse(s2.begin(),s2.end());  //翻转字符串
    29         if(s1!=s2&&mp.find(s2)!=mp.end()){   //非回文串,并且存在相反串
    30             while(!mp[s1].empty()&&!mp[s2].empty()){
    31                 int now=mp[s1].top()+mp[s2].top();
    32                 if(now>0){
    33                     ans+=now;
    34                     mp[s1].pop();
    35                     mp[s2].pop();
    36                 }
    37                 else break;
    38             }
    39         }
    40         else if(s1==s2){  //是回文串
    41             while(mp[s1].size()>=2){
    42                 int now1=mp[s1].top();
    43                 mp[s1].pop();
    44                 int now2=mp[s1].top();
    45                 mp[s1].pop();
    46                 if(now1+now2>0){
    47                     ans+=(now1+now2);
    48                     minn=min(minn,now2); //记录
    49                 }
    50                 else{
    51                     mp[s1].push(now2);
    52                     mp[s1].push(now1);
    53                     break;
    54                 }
    55             }
    56         }
    57     }
    58     for(map<string,priority_queue<int,vector<int>,cmp> >::iterator it=mp.begin();it!=mp.end();it++){
    59         string s1=it->first;
    60         string s2=s1;
    61         reverse(s2.begin(),s2.end());
    62         if(s1==s2&&!mp[s1].empty())     //找一个放在中间的回文串
    63             maxn=max(maxn,mp[s1].top());
    64     }
    65     printf("%d
    ",max(ans-minn,ans+maxn));
    66     return 0;
    67 }
  • 相关阅读:
    八、Docker+RabbitMQ
    Spring.Net学习笔记一(IOC第一个实例)
    快速搞懂 SQL Server 的锁定和阻塞
    JQuery.Ajax()的data参数类型
    常用开源介绍
    AutoMapper简明教程(学习笔记)
    面向.Net程序员的前端优化
    --数组元素插入有两种方式
    JQuery源码分析-02正则表达式-RegExp-常用正则表达式
    关于ASP.NET MVC的Filter小记
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6232133.html
Copyright © 2011-2022 走看看