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

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6 题意:给一个字符串,找出其中最长回文子串长度
思路:二分枚举其长度,然后枚举每个位置每个位置,利用字符串hash值比较中间位置前后是否一致,需要分奇偶进行二分,因为奇数或者偶数的时候判断前后时候一致的区间不同
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e6+;
unsigned long long f1[maxn],f2[maxn],p[maxn];
int even[maxn>>],odd[maxn>>]; char word[maxn];
int n; unsigned long long Find_l(int l,int r)
{
return f2[n-l+]-f2[n-r]*p[r-l+];//逆序hash值
}
unsigned long long Find_r(int l,int r)
{
return f1[r]-f1[l-]*p[r-l+];//正序hash值
} int check(int x)
{
int len = x;
x /= ;
if(len & )
{
for(int i=x; i<=n-x-; i++)
if(Find_l(i-x+,i)==Find_r(i+,i+x+))
return len;
}
else
{
for(int i=x; i<=n-x; i++)
if(Find_l(i-x+,i)==Find_r(i+,x+i))
return len;
}
return ;
} int serch(int num[],int l,int r)
{
int maxx = ;
while(l <= r)
{
int mid = (l+r) >> ;
int val = check(num[mid]);
if(val)
{
l = mid + ;
maxx = max(val,maxx);
}
else
r = mid - ;
}
return maxx;
}
int main()
{
int tot1 = ,tot2 = ;
p[] = ;
for(int i=; i<=; i++)
{
p[i] = p[i-]*;
if(i&)
odd[++tot1] = i;
else
even[++tot2] = i;
}
int cas = ;
while(~scanf("%s",word+) && word[] != 'E')
{
f1[] = f2[] = ;
n = strlen(word+);
for(int i=; i<=n; i++)
{
f1[i] = f1[i-]* + word[i]-'a'+;
f2[i] = f2[i-]* + word[n-i+]-'a'+;
}
int ans1 = serch(odd,,n/+);
int ans2 = serch(even,,n/+);
printf("Case %d: %d\n",++cas,max(ans1,ans2));
}
}

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

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

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

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

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

  3. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  4. poj 1200字符串hash

    题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...

  5. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  6. poj 2503 字符串hash

    题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...

  7. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  8. POJ 3974 Palindrome

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

  9. POJ 3865 - Database 字符串hash

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

随机推荐

  1. pestle.phar

    nstalll: 1,cd /usr/local/bin && curl -LO http://pestle.pulsestorm.net/pestle.phar : 2,chmod  ...

  2. eMMC真能优化成UFS?谈谈手机闪存的文件系统

    和UFS闪存相比,eMMC的性能更弱,同一型号的手机混用这两种规格的闪存,让一些消费者感到了不满.对此,厂商称通过优化,eMMC的产品也可以获得优秀的体验.这个优化到底是怎么回事?根据以往的一些宣传, ...

  3. 爬虫保存cookies时重要的两个参数(ignore_discard和ignore_expires)的作用

    两个参数的作用: 官方的解释: ignore_discard: save even cookies set to be discarded. ignore_expires: save even coo ...

  4. 金融量化分析【day110】:NumPy多维数组

    一.Numpy简介 NumPy 是高性能科学计算和数据分析的基础包,它是pandas等其他各种工具的基础 1.主要功能 1.ndarray,一个多维数组结构,高效且节省空间 2.无序循环对整组数据进行 ...

  5. SNMP源码分析之(一)配置文件部分

    snmpd.conf想必不陌生.在进程启动过程中会去读取配置文件中各个配置.其中几个参数需要先知道是干什么的: token:配置文件的每行的开头,例如 group MyROGroup v1 readS ...

  6. 第十节: 利用SQLServer实现Quartz的持久化和双机热备的集群模式 :

    背景: 默认情况下,Quartz.Net作业是持久化在内存中的,即 quartz.jobStore.type = "Quartz.Simpl.RAMJobStore, Quartz" ...

  7. [物理学与PDEs]第2章第4节 激波 4.2 熵条件

    1.  R.H. 条件仅仅给出了越过激波时的能量守恒定律, 即热力学第一定律; 但客观的流体运动过程还需满足热力学第二定律, 即越过激波是个熵增过程: $$\bex S_1>S_0\quad(0 ...

  8. 6-12 varchar和char 枚举类型enum 集合set

    1       字符类型char和varchar #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的参 ...

  9. Django详细流程

    一.设计表结构 我们以学生管理系统为例,讲解一下Django的基本操作.首先要设计一下表的结构,这里就不多解释 班级表结构: 表名:grades 字段:班级名称 gname 成立时间 gdate 女生 ...

  10. Android app中的so库和CPU架构

    一.android目前有几种cpu架构? 早期的Android系统几乎只支持ARMv5的CPU架构,目前支持七种CPU架构:ARMv5,ARMv7 (从2010年起),x86 (从2011年起),MI ...