回文串---Best Reward

HDU   3613

Description

After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit.

One of these treasures is a necklace made up of 26 different kinds of gemstones, and the length of the necklace is n. (That is to say: n gemstones are stringed together to constitute this necklace, and each of these gemstones belongs to only one of the 26 kinds.)

In accordance with the classical view, a necklace is valuable if and only if it is a palindrome - the necklace looks the same in either direction. However, the necklace we mentioned above may not a palindrome at the beginning. So the head of state decide to cut the necklace into two part, and then give both of them to General Li.

All gemstones of the same kind has the same value (may be positive or negative because of their quality - some kinds are beautiful while some others may looks just like normal stones). A necklace that is palindrom has value equal to the sum of its gemstones‘ value. while a necklace that is not palindrom has value zero.

Now the problem is: how to cut the given necklace so that the sum of the two necklaces‘s value is greatest. Output this value.

Input

The first line of input is a single integer T (1 ≤ T ≤ 10) - the number of test cases. The description of these test cases follows.

For each test case, the first line is 26 integers: v 1, v 2, ..., v 26 (-100 ≤ v i ≤ 100, 1 ≤ i ≤ 26), represent the value of gemstones of each kind.

The second line of each test case is a string made up of charactor ‘a‘ to ‘z‘. representing the necklace. Different charactor representing different kinds of gemstones, and the value of ‘a‘ is v 1, the value of ‘b‘ is v 2, ..., and so on. The length of the string is no more than 500000.

Output

Output a single Integer: the maximum value General Li can get from the necklace.

Sample Input

2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

aba

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

acacac

Sample Output

1

6

题意:给了一个串由小写子字母组成,‘a‘到’z‘对应26种颜色的钻石,每种颜色的钻石对应有一个价值,将一个串分成两段,求这两个串的最大价值,规定若这个串是回文串,这这个串的价值是每个钻石价值相加,若不是钻石,则这个串价值为0;

思路:定义两个数组d1[]和d2[],d1[i]表示从第一颗钻石开始到第i颗钻石的价值和,d2[i]表示从 最后一颗钻石开始向左到第i颗钻石的价值和,然后从第一颗钻石到最后一颗钻石遍历,若以第i颗钻石位置切割,求出分割后的两串的价值和,将所有钻石遍历一次,求出最大值;

复杂度:O(n);

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=500005;
int v[30],p[2*N],d1[2*N],d2[2*N];
char s[2*N],str[2*N];
int n;

void kp()
{
    int mx=0;
    int id;
    ///for(i=n;str[i]!=0;i++)
    ///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
    for(int i=1;i<n;i++)
    {
        if(mx>i)
            p[i]=min(p[2*id-i],p[id]+id-i);
        else
            p[i]=1;
        for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
        if(p[i]+i>mx)
        {
            mx=p[i]+i;
            id=i;
        }
    }
}

void init()
{
    str[0]=‘$‘;
    str[1]=‘#‘;
    for(int i=0;i<n;i++)
    {
        str[i*2+2]=s[i];
        str[i*2+3]=‘#‘;
    }
    n=n*2+2;
    s[n]=0;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=1;i<=26;i++)
            scanf("%d",&v[i]);
        scanf("%s",s);
        n=strlen(s);
        init();
        kp();
        d1[0]=0;
        for(int i=1;i<n;i++)
        {
            if(str[i]==‘#‘)  d1[i]=d1[i-1];
            else      d1[i]=d1[i-1]+v[str[i]-‘a‘+1];
        }
        d2[n]=0;
        for(int i=n-1;i>0;i--)
        {
            if(str[i]==‘#‘)  d2[i]=d2[i+1];
            else      d2[i]=d2[i+1]+v[str[i]-‘a‘+1];
        }
        int sum,tmp=-99999999;
        for(int i=2;i<n-2;i=i+2)
        {
            sum=0;
            if(p[(i+2)/2]*2-1>=i+1) sum+=d1[i];
            if(p[(n+i)/2]*2-1>=n-i-1) sum+=d2[i+1];
            if(sum>tmp) tmp=sum;
        }
        printf("%d\n",tmp);
    }
    return 0;
}
时间: 2024-10-08 06:34:49

回文串---Best Reward的相关文章

HDU 3613 Best Reward(manacher求前、后缀回文串)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割字符串s能获得的最大价值. 解题思路: 用manacher算法计算出p[i],每次计算p[i]是顺便计算一下这段回文串是否能到达边界,若能则计算出前缀或者后缀的结束位置,标记起来.//还有之前数组开1e6+5教C++是错的,改成2e6+5就对了,不觉明历.... 代码 1 #include<ios

最少回文串--牛客网(秋招备战专场三模)-C++方向

题目描述:一个字符串从左向右和从右向左读都完全一样则是回文串,给定一个字符串,问该字符串中的字符所能组成的最少的回文串的个数为多少 解题思路:如果一个字符出现的次数为偶数,则必能组成回文串,如果一个字符出现奇数次,只能自己组成回文串,题目中问最少的回文串数目,即求出现次数为奇数次的字符个数即可,定义a存储每个字符出现的次数,统计出现奇数次的字符的个数,即为输出 1 #include <iostream> 2 #include <string> 3 using namespace s

回文串问题

1.回文串的判断 #include <iostream> #include <string.h> using namespace std; //回文串的判断 bool isPalindrome(const char* src) { if(src == NULL) return true; int end = strlen(src)-1,begin = 0; while(begin < end)//从两边向中间判断,当然也可以从中间向两边判断 { if(src[begin] !

bzoj 2565: 最长双回文串

Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同).输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串. Input 一行由小写英文字母组成的字符串S. Output 一行一个整数,表示最长双回文子串的长度. Sample Input baacaabbacabb Sample Output 12 HINT

BZOJ2565:最长双回文串

2565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2195  Solved: 1119[Submit][Status][Discuss] Description 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为"abc",逆序为"cba",不相同). 输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都

Power oj/2610[判断回文串]

题目链接[https://www.oj.swust.edu.cn/problem/show/2610] 题意:给你一个字符串,让你判断这个字符串是不是回文串,字符串的长度是1<len<1e7,内存是4096KB. 题解:首先这1e7个字符是存不下的,1e71024=9765KB>4096kB.那么怎么办?字符串哈希,先对字符串的前半部分进行哈希,然后在对字符串后半部分进行哈希,如果两部分的哈希值相同,那么这个字符串就是回文串. BKDRH哈希,哈希公式为has=has*seed+s[i]

409.求最长回文串的长度 LongestPalindrome

题目要求求出长度即可,并不需要求出最长回文串. 思路:用字典统计每一个字符的出现次数,出现次数大于1的字符必定出现在回文串中,另外还再加上一个中心点. public static int LongestPalindrome(string s) { int length = 0; Dictionary<char, int> dictionary = new Dictionary<char, int>(); int value = 0; foreach (char c in s) {

[haoi2009]求回文串

所谓回文串,就是对于给定的字符串,正着读和反着读都一样,比如ABCBA就是一个回文串,ABCAB则不是.我们的目标是对于任意输入的字符串,不断将第i个字符和第i+1个字符交换,使得该串最终变为回文串.求最少交换次数. 题解: 有一种做法是贪心: 就是每次找到最左端的字符,然后找到这序列中最右边的一样的字符,然后把这个字符移过去,然后把最左端右移,继续以上操作: 最后的答案就是每次的移动步数加起来: 要吐槽的是,window下I64d不要忘了...... #include<iostream> #

字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串

Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行,为一个只包含小写字母(a -z)的非空字符串s. Output 输出一个整数,为逝查回文子串的最大出现值. Sample Input [样例输入l] abacaba [样例输入2] www Sample Output [样例输出l] 7 [样例输出2] 4 HINT 一个串是回文的,当且仅当它从左