Palindrome
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 13157   Accepted: 5028

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

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6

Source

题意:求一个字符串的最长回文子串

思路:回文串其实就是以一个节点为中间,两端的字符串是相同的。之前的比较字符串相同的Hash函数是以从左到右的顺序,那么这个就再存一个从右到左的字符串的Hash值。对于每一个字符,二分左半子串的长度,分回文串的长度是奇还是偶两种情况。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e6 + ;
char s[maxn];
unsigned long long H[maxn], p[maxn], H_rev[maxn]; unsigned long long getH(int i, int j)
{
return H[j] - H[i - ] * p[j - i + ];
} unsigned long long getHrev(int i, int j)
{
return H_rev[i] - H_rev[j + ] * p[j - i + ];
} int main()
{
int cas = ;
p[] = ;
for(int i = ; i < maxn; i++){
p[i] = p[i - ] * ;
}
while(scanf("%s", s + )){
if(strcmp(s + , "END") == ){
break;
}
int n = strlen(s + );
H[] = ;
H_rev[n + ] = ;
for(int i = ; i <= n; i++){
H[i] = H[i - ] * + (s[i] - 'a' + );
}
for(int i = n; i >= ; i--){
H_rev[i] = H_rev[i + ] * + (s[i] - 'a' + );
} int ans = -;
for(int i = ; i <= n; i++){
int ped = min(i - , n - i), pst = ;
while(pst < ped){
int pmid = (pst + ped + ) / ;
//cout<<pmid<<endl;
if(getH(i - pmid, i - ) == getHrev(i + , i + pmid)){
//
pst = pmid;
}
else{
ped = pmid - ;
}
}
//cout<<i<<" "<<pst<<endl;
ans = max(ans, * pst + );
int qed = min(i - , n + - i), qst = ;
while(qst < qed){
int qmid = (qst + qed + ) / ;
if(getH(i - qmid, i - ) == getHrev(i, i + qmid - )){ qst = qmid;
}
else{
qed = qmid - ;
}
}
ans = max(ans, * qst); } printf("Case %d: %d\n", cas++, ans);
} }

poj3974 Palindrome【回文】【Hash】【二分】的更多相关文章

  1. leetcode4 Valid Palindrome回文数

    Valid Palindrome回文数 whowhoha@outlook.com Question: Given a string, determine if it is a palindrome, ...

  2. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  3. 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)

    题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...

  4. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

  5. Palindrome 回文数

    回文数,从前到后,从后到前都一样 把数字转成字符串来处理 package com.rust.cal; public class Palindrome { public static boolean i ...

  6. valid palindrome(回文)

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. WHU 583 Palindrome ( 回文自动机 && 本质不同的回文串的个数 )

    题目链接 题意 : 给你一个串.要你将其划分成两个串.使得左边的串的本质不同回文子串的个数是右边串的两倍.对于每一个这样子的划分.其对答案的贡献就是左边串的长度.现在要你找出所有这样子的划分.并将贡献 ...

  8. 洛谷T89644 palindrome回文串

    洛谷 T89643 回文串(并查集) 洛谷:https://www.luogu.org/problem/T89643 题目描述 由于 Kiana 实在是太忙了,所以今天的题里面没有 Kiana. 有一 ...

  9. palindrome 回文 /// Manacher算法

    判断最长不连续回文 #include <bits/stdc++.h> using namespace std; int main() { ]; while(gets(ch)) { ],an ...

  10. 回文(palindrome)

    如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文).

随机推荐

  1. 第二百九十一节,RabbitMQ多设备消息队列-安装与简介

    RabbitMQ多设备消息队列-安装与简介 RabbitMQ简介 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是 ...

  2. Java中的内存处理机制和final、static、final static总结

    Java中的内存处理机制和final.static.final static总结   装载自:http://blog.csdn.net/wqthaha/article/details/20923579 ...

  3. 关于对afx_msg的解释-----来源百度百科

    1AFX前缀 Afx前缀是微软MFC一个小组的名称简写,并没有别的意义. MFC的很多代码,包括全局函数名.宏.头文件名都使用了"Afx". Afx*.h是一组MFC的核心头文件, ...

  4. php date strtotime的用法

    1.上个月第一天及最后一天. echo date('Y-m-01', strtotime('-1 month')); echo strtotime(date('Y-m-01 0:00:00', str ...

  5. <iOS>一个开发中值得注意的细节

    UIScrollView有一个属性叫做scrollToTop,是个BOOL值,默认为YES. 它的作用是定义当前的这个UIScrollView的delegate<UIScrollViewDele ...

  6. DoBox 下载

    DoBox下载 一款简单十分好用的办公助手,用于记录您接下来需要做的事情.待办事项小工具 - DoBox DoBox下载 下载地址:http://www.wxzzz.com/?id=141 最新版本: ...

  7. 如何使用CodeSmith批量生成代码(原创系列教程)

    在上一篇我们已经用PowerDesigner创建好了需要的测试数据库,下面就可以开始用它完成批量代码生成的工作啦. 下面我会一步步的解释如何用CodeSmith实现预期的结果的,事先声明一下,在此只做 ...

  8. day27<反射&JDK5新特性>

    反射(类的加载概述和加载时机) 反射(类加载器的概述和分类) 反射(反射概述) 反射(Class.forName()读取配置文件举例) 反射(通过反射获取带参构造方法并使用) 反射(通过反射获取成员变 ...

  9. Java精选笔记_Servlet技术

    Servlet技术 Servlet开发入门 Servlet接口 针对Servlet技术的开发,SUN公司提供了一系列接口和类,其中最重要的是javax.servlet.Servlet接口. Servl ...

  10. 数据库中存储js代码无法json解析

    .net-------------------Microsoft.JScript.GlobalObject.escape(); 编码 Mircorsoft.JScript.GlobalObject.u ...