题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613

题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是每个字母的价值之和,如果不是那么价值就是0;

例如:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

abbadf

那么可以分成abba 和df abba的价值是1+2+2+1=6,df不是回文串所以价值为0;总价值就是6;

我们可以用数组L【i】R【i】来记录串的前缀长度为i的是否是回文串和后缀长度为i的是否是回文串;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e6+; int v[], sum[N], p[N];
///sum[i]代表s串中前i个字符的价值总和;
///p[i]代表以i为中心的回文串的半径(包含i本身);
char s[N];
bool L[N], R[N];
///L[i]表示前i个字符是否是回文串,R[i]表示长度为i的后缀是否为回文串; void Manacher(char s[], int n)
{
int Id = , mx = ;
for(int i=; i<n; i++)
{
if(mx>i)
p[i] = min(mx-i, p[Id*-i]);
else
p[i] = ;
while(s[i+p[i]] == s[i-p[i]])
p[i]++;
if(mx < p[i]+i)
{
mx = p[i]+i;
Id = i;
} if(p[i] == i)
L[p[i]-] = true;
if(p[i]+i == n)
R[p[i]-] = true;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(L, , sizeof(L));
memset(R, , sizeof(R));
memset(p, , sizeof(p));
memset(sum, , sizeof(sum));
for(int i=; i<; i++)
scanf("%d", &v[i]);
scanf("%s", s);
int len = strlen(s);
for(int i=; i<=len; i++)
sum[i] += sum[i-] + v[s[i-]-'a'];
for(int i=len; i>=; i--)
{
s[i+i+] = s[i];
s[i+i+] = '#';
}
s[]='$';
Manacher(s, *len+);
int ans = ;
for(int i=; i<len; i++)
{
int t=;
if(L[i])
t+=sum[i];
if(R[len-i])
t+=sum[len]-sum[i];
ans = max(ans, t);
}
printf("%d\n", ans);
}
return ;
}

Best Reward---hdu3613(manacher 回文串)的更多相关文章

  1. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  2. UVA 12378 Ball Blasting Game 【Manacher回文串】

    Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...

  3. HDU3068 最长回文 MANACHER+回文串

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...

  4. Manacher回文串算法学习记录

    FROM:  http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...

  5. 【模板】Manacher 回文串

    推荐两个讲得很好的博客: http://blog.sina.com.cn/s/blog_70811e1a01014esn.html https://segmentfault.com/a/1190000 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  7. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

  8. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  9. (回文串 )Best Reward -- hdu -- 3613

    http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    ...

随机推荐

  1. jquery头文件的引入

    <script type="text/javascript" src="/library/js/jquery/jquery-1.9.1.min.js"&g ...

  2. 一些lua代码

    1.把k--v表转化为数组表,只支持2级 2.取中值 3.字符串按每行最多n像素分割,并返回每行最大宽度,可以用"\n"手动换行

  3. 软件配置管理中的SVN

    一.简单介绍 1.什么是软件配置管理 软件配置管理是指通过运行版本号控制.变更控制的规程.以及使用合适的配置管理软件.来保证全部配置项的完整性和可跟踪性. 配置管理是对工作成果的一种有效保护. 2.为 ...

  4. devicetree -- SPI

    SPI (Serial Peripheral Interface) busses SPI busses can be described with a node for the SPI master ...

  5. curses库--libncurses5-dev--游标移动及屏幕的显示

    curses是一个在Linux/Unix下广泛应用的图形函数库.,作用是可以绘制在DOS下的用户界面和漂亮的图形. curses的名字起源于"cursor optimization" ...

  6. modSecurity和Naxsi哪个更适合Nginx搭建WAF

    nginx增加modsecurity模块 modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成 ...

  7. 比特币交易本质--UTXO(Unspent Transaction Output)

    UTXO 代表 Unspent Transaction Output. Transaction 被简称为 TX,所以上面这个短语缩写为 UTXO. 现在的银行也好.信用卡也好.证券交易系统也好,互联网 ...

  8. c配置库ccl使用小结

    配置文件为key=value键值对形式 下载与安装 库文件下载:ccl-0.1.1.tar.gz 安装:  tar -zxvf ccl-0.1.1.tar.gz  cd ccl-0.1.1 ./con ...

  9. 数据库 proc编程五

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  10. tp 批量转码

    读取王正东成功,然后把乱码一条一条的改回来... 专门针对mssql数据库的!!!