题目链接:http://poj.org/problem?id=3974

Time Limit: 15000MS Memory Limit: 65536K

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

  1. abcbabcbabcba
  2. abacacbaaaab
  3. END

Sample Output

  1. Case 1: 13
  2. Case 2: 6

题意:

给出若干个字符串,求最长回文子串的长度。

题解:

首先预处理字符串的长度为 $i$ 的前缀子串的哈希值 $pre[i]$,

再把字符串反转,预处理新的字符串的长度为 $i$ 的前缀子串的哈希值 $suf[i]$,

这样,如果在原串中存在一个 $[l_1,r_1]$ 的回文串,那么对应到新串,这个区间就是 $[l_2,r_2] = [len - r_1 +1,len - l_1 + 1]$,这两个子串的哈希值应当是相等的,即:

$pre[r_1 ] - pre[l_1 - 1] \times P^x = suf[r_2 ] - suf[l_2 - 1] \times P^x$

其中,$x = r_1 - l_1 + 1 = r_2 - l_2 +1$。

所以,不难想到,我们如果二分回文子串的长度,就可以 $O\left( {\left| S \right|\log \left| S \right|} \right)$ 求出最长回文子串。

但是,这样做的话,在测样例时就会发现问题,奇数长度的回文子串和偶数长度的回文子串应当是分开计算的(因为回文串两侧同时各去掉一个字符,依然是回文串,且不改变奇偶性),

所以,需要两次二分,一次二分求得长度为偶数的最长回文子串(的长度),再一次二分求得长度为奇数的最长回文子串(的长度)。

最后输出两者中大的即可。

AC代码:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. using namespace std;
  5. typedef unsigned long long ull;
  6.  
  7. const int P=;
  8. const int maxn=+;
  9.  
  10. int len;
  11. char s[maxn];
  12. int q;
  13.  
  14. ull pre[maxn],suf[maxn],Ppow[maxn];
  15. void pretreat()
  16. {
  17. pre[]=;
  18. suf[]=;
  19. Ppow[]=;
  20. for(int i=;i<=len;i++)
  21. {
  22. pre[i]=pre[i-]*P+(s[i]-'a'+);
  23. suf[i]=suf[i-]*P+(s[len-i+]-'a'+);
  24. Ppow[i]=Ppow[i-]*P;
  25. }
  26. }
  27.  
  28. bool found(int x)
  29. {
  30. for(int l1=;l1+x-<=len;l1++)
  31. {
  32. int r1=l1+x-;
  33. int r2=len-l1+,l2=r2-x+;
  34. ull A=pre[r1]-pre[l1-]*(Ppow[x]);
  35. ull B=suf[r2]-suf[l2-]*(Ppow[x]);
  36. if(A==B) return ;
  37. }
  38. return ;
  39. }
  40.  
  41. int main()
  42. {
  43. int kase=;
  44. while(scanf("%s",s+) && s[]!='E')
  45. {
  46. len=strlen(s+);
  47. pretreat();
  48.  
  49. int l=,r=len/,mid;
  50. while(l<r)
  51. {
  52. mid=(l+r)/+;
  53. if(found(*mid)) l=mid;
  54. else r=mid-;
  55. }
  56. int ans1=l*;
  57.  
  58. l=,r=(len-)/;
  59. while(l<r)
  60. {
  61. mid=(l+r)/+;
  62. if(found(*mid+)) l=mid;
  63. else r=mid-;
  64. }
  65. int ans2=l*+;
  66.  
  67. printf("Case %d: %d\n",++kase,max(ans1,ans2));
  68. }
  69. }

POJ 3974 - Palindrome - [字符串hash+二分]的更多相关文章

  1. Palindrome POJ - 3974 (字符串hash+二分)

    Andy the smart computer science student was attending an algorithms class when the professor asked t ...

  2. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  3. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  4. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  5. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

  6. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. POJ 3865 - Database 字符串hash

    [题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...

  8. POJ 3974 Palindrome (算竞进阶习题)

    hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...

  9. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

随机推荐

  1. Linux内核编译与安装

    2013-04-16    Linux内核介绍  Linux内核是一个用C语言写成的,符合POSIX标准的类Unix操作系统.内核是操作系统中最基本的一部分,提供了众多应用程序访问计算机硬件的机制.L ...

  2. 2017 33 款iOS开源库

    IGListKit https://github.com/Instagram/IGListKit 由 Instagram 开发人员制作,IGListKit 是用于构建快速灵活列表的数据驱动型的 UIC ...

  3. ORACLE物化视图(物理视图)

    百度文库 http://wenku.baidu.com/view/f78f55c68bd63186bcebbc4b.html ORACLE物化视图 一.------------------------ ...

  4. 去除移动端alert/confirm的网址(url)

    移动端的alert.confirm都会显示来源的url,影响体验 下面的代码将alert和confirm重写了一遍,可去除url  参考了网上代码,完善了confirm不同状态跳转   示例代码: & ...

  5. Atitit php vs node.js attilax总结

    Atitit php vs node.js attilax总结 1.1. 上手度  还是php 1 1.2. Node.js最大的缺点  异步回调导致可读性差..特别嵌套的时候.. 1 1.1. 上手 ...

  6. Socket网络编程--简单Web服务器(4)

    上一小节已经实现了对图片的传输,接下来就是判断文件是否为js,css,png等格式.我们增加一个函数用于判断格式 int WebServer::get_filetype(char *type,char ...

  7. Python fcntl 与 signal 模块使用

    这两个模块是Python标准库里面就包含的模块 用法介绍在 https://docs.python.org/2/library/signal.html https://docs.python.org/ ...

  8. WebRTC 零基础开发者教程(中文版)下载

    WebRTC 简介 WebRTC,是一个支持网页浏览器进行实时语音通话或视频聊天的技术,是谷歌2010年以6820万美元收购Global IP Solutions公司而获得的一项技术. WebRTC提 ...

  9. unable to locate package gparted

    在unbuntu安装gparted的时候出现这个错误提示,意思为:找不到这个安装包 可能的原因: 1.当前系统更新包没有更新,执行命令:sudo apt-get update 2.命令错误,重新检查需 ...

  10. RR算法 调度

    RR算法是使用非常广泛的一种调度算法. 首先将所有就绪的队列按FCFS策略排成一个就绪队列,然后系统设置一定的时间片,每次给队首作业分配时间片.如果此作业运行结束,即使时间片没用完,立刻从队列中去除此 ...