日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 3613 Best Reward 扩展kmp算法(将一个字符串分成两个回文串)

發布時間:2023/12/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 3613 Best Reward 扩展kmp算法(将一个字符串分成两个回文串) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://vjudge.net/problem/HDU-3613
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: 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.

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

題目大意:
給個字符串S,要把S分成兩段T1,T2,每個字母都有一個對應的價值,如果T1,T2是回文串(從左往右或者從右往左讀,都一樣),那么他們就會有一個價值,這個價值是這個串的所有字母價值之和,如果不是回文串,那么這串價值就為0。問最多能獲得多少價值?

#include<stdio.h> #include<string.h> #include<math.h> #include<stack> #include<queue> #include<set> #include<iostream> #include<algorithm> #include<string> #include<vector> using namespace std; #define ll long long int v[27]; const int maxn=500005; char str1[maxn]; char str2[maxn]; int sum[maxn]; int nex[maxn]; int extend1[maxn]; int extend2[maxn]; void get_next(char str[]) ///套模板 {int i=0;int p;int len=strlen(str);while(str[i]==str[i+1]&&i+1<len){i++;}nex[1]=i;p=1;for(int i=2; i<len; i++){if(nex[i-p]+i<nex[p]+p){nex[i]=nex[i-p];}else{int j=nex[p]+p-i;if(j<0) j=0;while(i+j<len&&str[j]==str[i+j]){j++;}nex[i]=j;p=i;}} }void extendkmp(char s1[],char s2[],int extend[]) ///套模板 {int i=0;int p;int len1=strlen(s1);int len2=strlen(s2);get_next(s2);while(s1[i]==s2[i]&&i<len2&&i<len1){i++;}extend[0]=i;p=0;for(i=1; i<len1; i++){if(nex[i-p]+i<extend[p]+p){extend[i]=nex[i-p];}else{int j=extend[p]+p-i;if(j<0) j=0;while(i+j<len1&&j<len2&&s1[j+i]==s2[j]){j++;}extend[i]=j;p=i;}} } ///extend[i]表示為以字符串S1中以i為起點的后綴字符串和模式串S2的最長公共前綴長度. int main() {int t;scanf("%d",&t);while(t--){for(int i=0; i<26; i++){scanf("%d",&v[i]);}scanf("%s",str1);int len=strlen(str1);sum[0]=0;for(int i=0; i<len; i++){sum[i+1]=sum[i]+v[str1[i]-'a'];str2[i]=str1[len-1-i]; ///str2是str1的反轉}str2[len]='\0';memset(nex,0,sizeof(nex));get_next(str1);extendkmp(str2,str1,extend1);memset(nex,0,sizeof(nex));get_next(str2);extendkmp(str1,str2,extend2);int ans=-0x3f3f3f3f;for(int i=1;i<len;i++){int tmp=0;if(extend1[i]+i==len){tmp+=sum[len-i];}int pos=len-i;if(pos+extend2[pos]==len){tmp+=sum[len]-sum[pos];}if(tmp>ans) ans=tmp;}printf("%d\n",ans);} }

總結

以上是生活随笔為你收集整理的HDU 3613 Best Reward 扩展kmp算法(将一个字符串分成两个回文串)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。