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. 转:Python操作SQLServer示例

    注:此文也是转载,2018年1月发现此文阅读量过万,略感不安.当时只是为了自己存档学习,未粘此文的原始连接.如有侵权,通过即删除,敬请谅解! 从网上找的,估计原文是:Python操作SQLServer ...

  2. CommonJS初识

    CommonJS是什么 CommonJS是一个标准库,或者说是一组规范.因为官方并没有给出真正的代码实现,而是给出一些代码组织规范.常用模块的api.包(Package)的命名规范和具体格式. Com ...

  3. 比較JS合并数组的各种方法及其优劣

    原文链接:  Combining JS Arrays原文日期: 2014-09-09翻译日期: 2014-09-18翻译人员: 铁锚 本文属于JavaScript的基础技能. 我们将学习结合/合并两个 ...

  4. vnc 多用户登录

    1, 创建新用户: $ useradd tom $ passwd tom 2,  登录到tom账户,创建vnc实例: $ su tom$ vncserver 这时可以看看~/.vnc/目录下,有一些如 ...

  5. hadoop本地测试命令

    http://www.cnblogs.com/shishanyuan/p/4190403.html if have assign the /etc/profile: hadoop jar /usr/l ...

  6. MVC4 Controller 与 WebApi 的 Session 传值问

    在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的.例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值.但 ...

  7. html5引擎开发 -- 引擎消息中心和有限状态机 - 初步整理 一

    一 什么是有限状态机        FSM (finite-state machine),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.他对于逻辑以及 ...

  8. Java精选笔记_HTTP协议

    HTTP协议 HTTP概述 介绍 HTTP是Hyper Text Transfer Protocol的缩写,即超文本传输协议. HTTP是一种请求/响应式的协议,客户端在与服务器端建立连接后,就可以向 ...

  9. Android开发之--常用颜色值

    <?xml version="1.0" encoding="utf-8" ?> <resources> <color name=& ...

  10. 如何连接 MySQL

    操作如下: [root@localhost ~]$ mysql -uroot -p' # 本地连接 MySQL 服务 [root@localhost ~]$ mysql -uroot -p' -h19 ...