Best Reward [HDU - 3613 ] (manacher算法)

Best Reward [HDU - 3613 ] (manacher算法)

HDU - 3613

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

题意:

给定一个字符串s,以及字符'a'到'z'的价值,

你可以把字符串分成任意的连续的2段字符串,每一部分的价值是:

如果这部分字符串是回文串,则价值是其包含的字符价值的sum和,否则价值为0。

问你划分后的价值最大是多少?

思路:

用前缀和数组:sum[i] 代表 字符串1~i的价值和(字符串采用1-index)

用manacher算法处理字符串s ,得到他的最长回文半径数组\(\mathit p\)

我们知道 \(p[i]-1\)代表 以\(\mathit i\) 为中心的最长回文长度。

我们枚举字符串划分时的前缀长度\(i,i \in [1,len-1]\)

那么字符串s的长度为\(\mathit i\) 的前缀\(s[1\dots i]\)为回文串时:\(p[i + 1] - 1 = i\)

那么字符串s的长度为\(\mathit i\) 的后缀\(s[len - i+1\dots len]\)为回文串时:\(p[i + 1 + len] - 1 = len - i\)

维护最大的价值即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#include <sstream>
#include <bitset>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
const int maxn = 500010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int val[maxn];
char s[maxn];
int len;
char s_new[maxn * 2 + 10];
int p[maxn * 2 + 10];
int Len;
int sum[maxn];
void init()
{
    Len = len * 2 + 2;
    int index = 0;
    s_new[index++] = '$';
    repd(i, 1, len)
    {
        s_new[index++] = '#';
        s_new[index++] = s[i];
    }
    s_new[index++] = '#';
    s_new[index++] = '\0';
    // chu(s_new);
}
void manacher()
{
    int R = 0;
    int mid = 0;
    repd(i, 1, Len)
    {
        p[i] = i < R ? min(R - i, p[2 * mid - i]) : 1;
        while (s_new[i - p[i]] == s_new[i + p[i]])
        {
            p[i]++;
        }
        if (i + p[i] > R)
        {
            R = i + p[i];
            mid = i;
        }
    }
}

int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    int t;
    t = readint();
    while (t--)
    {
        repd(i, 'a', 'z')
        {
            val[i] = readint();
        }
        scanf("%s", s + 1);
        len = strlen(s + 1);
        repd(i, 1, len)
        {
            sum[i] = sum[i - 1] + val[s[i]];
        }
        init();
        manacher();
        int ans = -inf;
        repd(i, 1, len - 1)
        {
            int temp = 0;
            if (p[i + 1] - 1 == i)
            {
                temp += sum[i];
            }
            if (p[i + 1 + len] - 1 == len - i)
            {
                temp += sum[len] - sum[i];
            }
            ans = max(ans, temp);
        }
        printf("%d\n", ans );
    }

    return 0;
}



posted @ 2020-03-21 17:54  茄子Min  阅读(174)  评论(0编辑  收藏  举报