hdu3613KMP求切割最长回文串

转自:http://blog.csdn.net/xingyeyongheng/article/details/9292449

Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 336    Accepted Submission(s): 131


Problem Description
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

首先输入n表示有n个测试,接着下一行有26个数分别表示a,b,c....z的价值,然后输入一行字符串,求将该字符串二分后的最大价值,二分后的字串如果是回文串则价值为字符价值之和,否则价值为0

另一种EKMP做法:http://blog.csdn.net/xingyeyongheng/article/details/9386235

另一种manacher做法:http://blog.csdn.net/xingyeyongheng/article/details/9386313

同种思路两种写法:

第一种(效率更高):

  1. /* 
  2. 思路:本题难度在于如何将原串二分后判断前缀是否是回文和后缀是否是回文串 
  3. 则可以将原串反转得s2,然后用原串s1去匹配s2,最终s1匹配到的位置就是s1的最大前缀回文串 
  4. 同理用s2去匹配s1得到s2的最大前缀回文串,即s1得最大后悔回文串 
  5. 然后根据next[k]的特性,next[k]跳转到的位置就是下一个前缀回文串(0~k和x~len相同,而x~len又是0~k反转得到的,所以0~k是回文串) 
  6. 直到k=0,将所有前缀回文串和后缀回文串标记,然后对原串线性扫描求出最大值即可  
  7. */  
  8. #include<iostream>  
  9. #include<cstdio>  
  10. #include<cstdlib>  
  11. #include<cstring>  
  12. #include<string>  
  13. #include<queue>  
  14. #include<algorithm>  
  15. #include<map>  
  16. #include<iomanip>  
  17. #define INF 99999999  
  18. using namespace std;  
  19.   
  20. const int MAX=500000+10;  
  21. char s1[MAX],s2[MAX];  
  22. int next[MAX],sum[MAX],val[27];  
  23. int per[MAX],pos[MAX];//用来标记前缀回文串和后缀回文串  
  24.   
  25. void get_next(char *a,int len){  
  26.     int i=-1,j=0;  
  27.     next[0]=-1;  
  28.     while(j<len){  
  29.         if(i == -1 || a[i] == a[j])next[++j]=++i;  
  30.         else i=next[i];  
  31.     }  
  32. }  
  33.   
  34. int KMP(char *a,char *b,int len){  
  35.     int i=0,j=0;  
  36.     while(i<len && j<len){  
  37.         if(i == -1 || a[i] == b[j])++i,++j;  
  38.         else i=next[i];  
  39.     }  
  40.     return i;  
  41. }  
  42.   
  43. int main(){  
  44.     int n,k;  
  45.     cin>>n;  
  46.     while(n--){  
  47.         for(int i=0;i<27;++i)scanf("%d",&val[i]);  
  48.         scanf("%s",s1);  
  49.         int len=strlen(s1);  
  50.         for(int i=0;i<len;++i)s2[i]=s1[len-1-i],sum[i+1]=sum[i]+val[s1[i]-'a'];  
  51.         get_next(s1,len);  
  52.         k=KMP(s1,s2,len);//求s1最大前缀回文串位置  
  53.         while(k)per[k]=n+1,k=next[k];//标记所有前缀回文串   
  54.         get_next(s2,len);  
  55.         k=KMP(s2,s1,len);//求s1最大后缀回文串位置   
  56.         while(k)pos[k]=n+1,k=next[k];//标记所有后缀回文串   
  57.         int ans=-INF,num=0;  
  58.         for(int i=1;i<len;++i){  
  59.             if(per[i] == n+1)num+=sum[i];  
  60.             if(pos[len-i] == n+1)num+=sum[len]-sum[i];  
  61.             if(num>ans)ans=num;  
  62.             num=0;  
  63.         }  
  64.         cout<<ans<<endl;  
  65.     }  
  66. }   
第二种:

思路:将原串 + 一个字符(比如'#') + 原串反转后的串  得到新串s,对s求next,则next[len]为原串的回文前缀,一直next[len]则可得到所有回文前缀,然后求原串后缀,最后求结果

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstdlib>  
  4. #include<cstring>  
  5. #include<string>  
  6. #include<queue>  
  7. #include<algorithm>  
  8. #include<map>  
  9. #include<iomanip>  
  10. #define INF 99999999  
  11. using namespace std;  
  12.   
  13. const int MAX=500000+10;  
  14. char s[MAX*2];  
  15. int next[MAX*2],sum[MAX],val[27];  
  16. int pre[MAX],pos[MAX];//标记原串中的回文前缀和回文后缀   
  17.   
  18. void get_next(char *a,int len){  
  19.     int i=-1,j=0;  
  20.     next[0]=-1;  
  21.     while(j<len){  
  22.         if(i == -1 || a[i] == a[j])next[++j]=++i;  
  23.         else i=next[i];  
  24.     }  
  25.     return;  
  26. }  
  27.   
  28. int main(){  
  29.     int n;  
  30.     cin>>n;  
  31.     while(n--){  
  32.         for(int i=0;i<26;++i)scanf("%d",&val[i]);  
  33.         scanf("%s",s);  
  34.         int len=strlen(s),k=len,temp=len;  
  35.         for(int i=1;i<=len;++i)sum[i]=sum[i-1]+val[s[i-1]-'a'];  
  36.         s[len]='#';//中间用一个字符隔开则next[len]就一定在#前面,即寻找到的回文前缀一定是原串的回文前缀   
  37.         while(k)s[++len]=s[--k];//将原串反转连接在原串后面   
  38.         s[++len]='\0';  
  39.         get_next(s,len);  
  40.         k=len;  
  41.         while(next[k])pre[next[k]]=n+1,k=next[k];//标记前next[len]个字符是回文串,即标记回文前缀   
  42.         for(int i=0;i<temp;++i){//前后两部分字串交换位置实现将最初原串反转连接在前面,为了求回文后缀   
  43.             s[i]=s[i]^s[temp+1+i];  
  44.             s[temp+1+i]=s[i]^s[temp+1+i];  
  45.             s[i]=s[i]^s[temp+1+i];  
  46.         }  
  47.         get_next(s,len);  
  48.         k=len;  
  49.         while(next[k])pos[next[k]]=n+1,k=next[k];//标记前next[len]是回文串,即标记回文后缀  
  50.         int ans=-INF,num=0;  
  51.         for(int i=1;i<temp;++i){//求最大值   
  52.             if(pre[i] == n+1)num+=sum[i];  
  53.             if(pos[temp-i] == n+1)num+=sum[temp]-sum[i];  
  54.             if(num>ans)ans=num;  
  55.             num=0;  
  56.         }  
  57.         cout<<ans<<endl;  
  58.     }  
  59.     return 0;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 HDU4992 所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了模 n 意义下的所有原根,我们需要先出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于最大公约数,phi 函数用于欧拉函数,pow 函数用于快速幂模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值