HDU3613: Best Reward

Source: Internet
Author: User

Problem DescriptionAfter an uphill battle, General Li won a great Vicat. now the head of state decide to reward him with honor and treasures for his great exploit. one of these treasures is a neck?made up of 26 different kinds of gemstones, and the length of the neck+is n. (That is to say: n gemstones are stringed together to constitute this necktasks, and each of these gemstones belongs to only one of the 26 kinds .) in accordance with the classical view, a neckpattern is valuable if and only if it is a palindrome-the neck1_looks the same in either direction. however, the neckaskwe 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 neckrom that is not palindrom has value zero. now the problem is: how to cut the given neck1_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: 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 necktasks. 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. outputOutput a single Integer: the maximum value General Li can get from the neck.pdf. sample Input21 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 1 1aba1 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 1 1 1 1 1 acacac Sample Output16 // expand KMP, to be honest, I still don't quite understand the meaning of the question. [cpp] zookeeper # include <iostream> # include <cstdio> # include <cstring> using namespace std; const int MAXN = 500005; char S [MAXN], T [MAXN]; int f [MAXN], extend1 [MAXN], extend2 [MAXN], val [30], sum [MAXN]; void turn (char * s, char * t, int len) {memset (t, 0, sizeof (t); for (int I = 0; I <len; I ++) t [I] = s [len-i-1];} void getnext (char * T, int * next) {int len = strlen (T), a = 0; next [0] = len; while (a <len-1 & T [a] = T [a + 1]) a ++; next [1] =; a = 1; for (int k = 2; k <len; k ++) {int p = a + next [a]-1, L = next [k-a]; if (k + L-1> = p) {int j = max (p-k + ); while (k + j <len & T [k + j] = T [j]) j ++; next [k] = j; a = k ;} else next [k] = L ;}} void EKMP (char * S, char * T, int * next, int * extend) {getnext (T, next ); int slen = strlen (S), tlen = strlen (T), a = 0; int minlen = min (slen, tlen ); while (a <minlen & S [a] = T [a]) a ++; extend [0] = a; a = 0; for (int k = 1; k <slen; ++ k) {int p = a + extend [a]-1, L = next [k-a]; if (K-1 + L> = p) {int j = max (p-k + 1, 0); while (k + j <slen & j <tlen & S [k + j] = T [j]) + + j; extend [k] = j; a = k;} else extend [k] = L ;}} int main () {int n, I, j; cin> n; while (n --) {for (I = 0; I <26; I ++) cin> val [I]; scanf ("% s ", s); memset (sum, 0, sizeof (sum); for (I = 0; S [I]; I ++) sum [I + 1] = val [S [I]-'a'] + sum [I]; int len = strlen (S); turn (S, T, strlen (S); EKMP (S, T, f, extend2); EKMP (T, S, f, extend1); int max =-1000000000; for (I = 0; I <len; I ++) {if (I & extend1 [I] + I = len) {int pos = extend1 [I]; int tmp = sum [pos]; if (extend2 [pos] + pos = len) {tmp + = sum [len]-sum [pos];} if (tmp> max) max = tmp;} else {int pos = I + 1, tmp = 0; if (extend2 [pos] + pos = len) {tmp + = sum [len]-sum [pos];} if (tmp> max) max = tmp ;}} printf ("% d \ n", max );} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.