码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3613Best Reward

时间:2018-10-20 13:39:33      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:family   air   scanf   nbsp   otto   together   next   mission   repr   

题解:  考虑用ex_kmp维护出当前位置后缀和前缀是否是回文串即可  前缀和统计价值

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int MAXN=5e5+10;
const double eps=1e-8;
#define ll long long
using namespace std;
struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e;
void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar();
    return x*f;
}

int key[26],sum[MAXN],nxt[MAXN];
char str[MAXN],s1[MAXN],s2[MAXN];
void ex_next(int ex_Nxt[],char a[]){
	int len=strlen(a);
    ex_Nxt[0]=len;
    int id_max=1;
    int t=0;
    while(t<len-1&&a[t]==a[t+1]) t++;
    ex_Nxt[1]=t;
    for(int k=2;k<len;k++){
    	int p=ex_Nxt[id_max]+id_max-1;int l=ex_Nxt[k-id_max];
    	if(l+k-1>=p){
    		int j=(p-k+1)>0?p-k+1:0;
    		while(j+k<len&&a[j]==a[j+k]) j++;
    		ex_Nxt[k]=j;
    		id_max=k;
		}
		else ex_Nxt[k]=l;
	}
	return ;
}
void ex_exid(int ex_excd[],int ex_Next[],char a[],char b[]){
	int len=strlen(a);
	int max_id=0;
	int t=0;
	int len1=strlen(b);
	int len2=min(len1,len);
	while(t<len2&&a[t]==b[t]) t++;
	ex_excd[0]=t;
	for(int i=1;i<len;i++){
		int p=ex_excd[max_id]+max_id-1;int l=ex_Next[i-max_id];
		if(i+l-1>=p){
			int j=(p-i+1)>0?p-i+1:0;
			while(j+i<len&&j<len1&&a[j+i]==b[j]) j++;
			ex_excd[i]=j;
			max_id=i;
		}
		else ex_excd[i]=l;
	}
	return ;
}
int c1[MAXN],c2[MAXN];
int main(){
	int _=read();
	while(_--){
		inc(i,0,25)key[i]=read();scanf("%s",str);
		int len=strlen(str);
		if(len==1){puts("0");continue;}
		for(int i=0;i<len;i++)s1[i]=s2[i]=str[i],sum[i+1]=sum[i]+key[(int)(str[i]-‘a‘)];
		s1[len]=s2[len]=‘\0‘;
		reverse(s2,s2+len);
		ex_next(nxt,s2);
		ex_exid(c1,nxt,s1,s2);
		ex_next(nxt,s1);
		ex_exid(c2,nxt,s2,s1);
		for(int i=0;i<len;i++){
			if(c1[i]==len-i)c1[i]=1;else c1[i]=0;
			if(c2[i]==len-i)c2[i]=1;else c2[i]=0;
		}
		reverse(c2,c2+len);
		int maxx=0;
		for(int i=1;i<len;i++){
			int ans=0;
			if(c1[i])ans+=sum[len]-sum[i];
			if(c2[i-1])ans+=sum[i];
			maxx=max(maxx,ans);
		}
		printf("%d\n",maxx);
	}
}

 

        Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4245    Accepted Submission(s): 1737


Problem 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: v1, v2, ..., v26 (-100 ≤ vi ≤ 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 v1, the value of ‘b‘ is v2, ..., 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
 

 

Source

hdu3613Best Reward

标签:family   air   scanf   nbsp   otto   together   next   mission   repr   

原文地址:https://www.cnblogs.com/wang9897/p/9821271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!