• hdu3613 Best Reward


    先manacher。然后前缀和价值,枚举切点,O(1)判断切后是否回文

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    int T, val[1000005], p[1000005], len, w[255];
    char a[1000005];
    void manacher(){
    	int mx=0, id=0;
    	for(int i=1; i<len; i++){
    		if(i<mx)	p[i] = min(p[2*id-i], mx-i);
    		else	p[i] = 1;
    		while(a[i-p[i]]==a[i+p[i]])	p[i]++;
    		if(mx<i+p[i])	mx = i + p[i], id = i;
    	}
    }
    int main(){
    	cin>>T;
    	w['#'] = w['$'] = 0;
    	while(T--){
    		for(int i=0; i<26; i++)
    			scanf("%d", &w[i+'a']);
    		scanf("%s", a);
    		len = strlen(a);
    		for(int i=len; i>=0; i--){
    			a[2*i+2] = a[i];
    			a[2*i+1] = '#';
    		}
    		len = 2 * len + 1;
    		a[0] = '$';
    		manacher();
    		for(int i=1; i<len; i++)
    			val[i] = val[i-1] + w[(int)a[i]];
    		int maxans=0;
    		for(int i=3; i<len; i+=2){
    			int tmp=0;
    			if((i+1)/2+p[(i+1)/2]>i)	tmp += val[i];
    			if((i+len)/2+p[(i+len)/2]>len)	tmp += val[len-1] - val[i-1];
    			maxans = max(maxans, tmp);
    		}
    		printf("%d
    ", maxans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    python 集合 set
    Meet Python
    Python 模块
    KNN
    Python Numpy包安装
    R分词
    Maximum Entropy Model(最大熵模型)初理解
    Conditional Random Fields (CRF) 初理解
    Naive Bayes (NB Model) 初识
    Hidden Markov Models(HMM) 初理解
  • 原文地址:https://www.cnblogs.com/poorpool/p/7912042.html
Copyright © 2020-2023  润新知