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.



InputThe 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.



OutputOutput 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

题意:

给你一条项链,由26个英文字母构成,每一个字符有一个价值,我们要把这个项链切成两部分,如果切成的直线是一个回文字符串那么他的价值就是所有字符价值之和。

但是要注意是要切完之后的那半部分整体是一个回文字符串才可以加上他的价值

这虽然是一个项链,但是你不能转动它,就相当于它就是一条直线不是一个环

题解:

这个就很容易想了,我们可以先用马拉车算法对这个字符串整体处理一下得到一个回文长度数组Len

在预处理一下这个项链的前缀价值

之后就一个for循环对这个字符串的每一位都判断,要知道Len[i]-1就是回文长度,又因为只有子字符串整体回文才加它的价值,所以就可以用if(Len[i + 1] - 1 == i)

来判断一下

当成立的话就要看它是奇数长度还是偶数长度,偶数长度就直接让他的前一半乘与2就可以了,奇数的话再多加中间那个就可以了

我们还要考虑切完之后后边那一部分是不是也是回文,方法一样

代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<queue>
5 #include<algorithm>
6 using namespace std;
7 const int maxn=5e5+10;
8 char str[maxn*2],s[maxn];
9 int len,Len[maxn*2],val[maxn],v[30];
10 void init()
11 {
12 memset(str,0,sizeof(str));
13 int k=0;
14 str[k++]='$';
15 for(int i=1; i<=len; ++i)
16 {
17 str[k++]='#';
18 str[k++]=s[i];
19 }
20 str[k++]='#';
21 len=k;
22 }
23 void manacher()
24 {
25 Len[0]=0;
26 int sum=0;
27 int id,mx=0;
28 for(int i=1; i<len; ++i)
29 {
30 if(i<mx) Len[i]=min(mx-i,Len[2*id-i]);
31 else Len[i]=1;
32 while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
33 if(Len[i]+i>mx)
34 {
35 mx=Len[i]+i;
36 id=i;
37 }
38 }
39 }
40 int main()
41 {
42 int t;
43 scanf("%d",&t);
44 while(t--)
45 {
46 for(int i=0; i<26; ++i)
47 scanf("%d",&v[i]);
48 scanf("%s",s+1);
49 len=strlen(s+1);
50 for(int i=1; i<=len; ++i)
51 {
52 val[i]=val[i-1]+v[s[i]-'a'];
53 }
54 int n=len;
55 init();
56 manacher();
57 int ans=0;
58 for(int i = 1; i < n; i++)
59 {
60 int tmp = 0;
61 if(Len[i + 1] - 1 == i)
62 {
63 if(i % 2)
64 {
65 tmp += val[i / 2] * 2 + v[s[(i + 1) / 2] - 'a'];
66 }
67 else
68 {
69 tmp += val[i / 2] * 2;
70 }
71 }
72 if(Len[i + n + 1] - 1 == n - i) //这个i+n+1就是后面那一部分的中点
73 {
74 if((n - i) % 2)
75 {
76 tmp += (val[i + (n - i) / 2] - val[i]) * 2 + v[s[i + (n + 1- i) / 2] - 'a'];
77 }
78 else
79 {
80 tmp += (val[i + (n - i) / 2] - val[i]) * 2;
81 }
82 }
83 ans = max(ans, tmp);
84 }
85 printf("%d\n",ans);
86 }
87 return 0;
88 }
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".

Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.

InputInput contains multiple cases.

Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'a' is and the length of the string will not exceed 200000.All input must be lowercase.

If the length of string is len, it is marked from 0 to len-1.OutputPlease execute the operation following the two steps.

If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!".

If there are several answers available, please choose the string which first appears.Sample Input

b babd
a abcd

Sample Output

0 2
aza
No solution!

题意:

就是找出来那一部分回文,并输出回文的区间

题解:

我感觉这一道题一直卡我的就是那个区间,我自己举个例子找错了,导致一直过不了

int r = start/2 + sum/2 - 1;      //这个start就是最大回文长度的中心(不是原串),sun是回文串长度
int l = r - sum + 1;

上代码:

 1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<set>
6 using namespace std;
7 const int maxn=400005;
8 const int INF=0x3f3f3f3f;
9 const int mod=998244353;
10 char str[maxn],s[maxn];
11 int len,Len[maxn],start,value[150],sum;
12 void init()
13 {
14 memset(str,0,sizeof(str));
15 int k=0;
16 str[k++]='$';
17 for(int i=0; i<len; ++i)
18 str[k++]='#',str[k++]=s[i];
19 str[k++]='#';
20 len=k;
21 }
22 int manacher()
23 {
24 int id,mx=0;
25 sum=0;
26 Len[0]=0;
27 for(int i=1; i<len; ++i)
28 {
29 if(i<mx) Len[i]=min(mx-i,Len[2*id-i]);
30 else Len[i]=1;
31 while(str[i-Len[i]]==str[i+Len[i]]) Len[i]++;
32 if(Len[i]+i>mx)
33 {
34 mx=Len[i]+i;
35 id=i;
36 }
37 if(sum<Len[i])
38 {
39 sum=Len[i];
40 start=i;
41 }
42 }
43 return --sum;
44 }
45 int main()
46 {
47 char q[5];
48 //printf("%d %d\n",'a','z');//97 122
49 while(~scanf("%s%s",q,s))
50 {
51 len=strlen(s);
52 int x = q[0] - 'a';
53
54 for(int i=0; s[i]; i++)
55
56 {
57
58 int t = s[i] - 'a';
59
60 s[i] = (t - x + 26)%26 + 'a';
61
62 }
63 init();
64 int w=manacher();
65 //printf("%d\n",w);
66 if(w>=2)
67 {
68 int r = start/2 + sum/2 - 1; //区间错误
69 int l = r - sum + 1;
70 printf("%d %d\n",l,r);
71 for(int i=l; i<=r; ++i)
72 {
73 printf("%c",s[i]);
74 }
75 printf("\n");
76 }
77 else printf("No solution!\n");
78 }
79 return 0;
80 }

Best Reward && Girls' research的更多相关文章

  1. HDU----(3294)Girls' research(manacher)

    Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)T ...

  2. Girls' research(manacher)

    Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  3. HDU 3294 Girls' research(manachar模板题)

    Girls' researchTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...

  4. (回文串 Manacher )Girls' research -- hdu -- 3294

    http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit:1000MS     Memory Limit:32 ...

  5. HDU3294 Girls' research —— Manacher算法 输出解

    题目链接:https://vjudge.net/problem/HDU-3294 Girls' research Time Limit: 3000/1000 MS (Java/Others)    M ...

  6. Hdu 3294 Girls' research (manacher 最长回文串)

    题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...

  7. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  8. hdu 3294 Girls' research(manacher)

    Problem Description One day, sailormoon girls are so delighted that they intend to research about pa ...

  9. hdu3294 Girls' research manacher

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...

随机推荐

  1. 【剑指 Offer】03.数组中重复的数字

    题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...

  2. 利用css和jquery制成弹幕

    1.首先上图看下效果 2.废话不多说,直接上代码 1>html代码 <div class="barrage"> <div class="scree ...

  3. 🎉 Element UI for Vue 3.0 来了!

    第一个使用 TypeScript + Vue 3.0 Composition API 重构的组件库 Element Plus 发布了 ~ 2016 年 3 月 13 日 Element 悄然诞生,经历 ...

  4. 【windows】快捷键

    Ctrl+字母键 1.Ctrl+A:全选 2.Ctrl+C:复制选择的项目 3.Ctrl+E:选择搜索框 4.Ctrl+F:选择搜索框 5.Ctrl+N:创建新的项目 6.Ctrl+W:关闭当前窗口 ...

  5. C语言逗号运算符(C语言学习笔记)

    什么是逗号运算符 逗号运算符 逗号运算符是指在C语言中,多个表达式可以用逗号分开,其中用逗号分开的表达式的值分别结算,但整个表达式的值是最后一个表达式的值. 用法 多个变量赋值 原因:"=& ...

  6. oracle编译表上失效USERDBY脚本

    对表进行DLL操作之后,依赖这个表的一些存储过程,触发器等会失效,可以用下边的脚本进行重编译 /* Formatted on 2020/7/8 上午 09:31:31 (QP5 v5.163.1008 ...

  7. 宝塔的url计划任务

    to通过url访问 就像访问你的网站一样 然后控制器/方法里面写你要做的操作 就可以了 ,简单的一批

  8. Mac中安装Git

    Mac 安装git 打开Mac终端输入git命令 如果出现以下代码说明已经安装 usage: git [--version] [--help] [-C <path>] [-c <na ...

  9. 大数据谢列3:Hdfs的HA实现

    在之前的文章:大数据系列:一文初识Hdfs , 大数据系列2:Hdfs的读写操作 中Hdfs的组成.读写有简单的介绍. 在里面介绍Secondary NameNode和Hdfs读写的流程. 并且在文章 ...

  10. k8s之PV、PVC、StorageClass详解

    导读 上一篇写了共享存储的概述以及一个简单的案例演示.这一篇就写一下PV和PVC. PV是对底层网络共享存储的抽象,将共享存储定义为一种"资源",比如Node也是容器应用可以消费的 ...