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. e670. 缓冲图像转换为图像

    // This method returns an Image object from a buffered image public static Image toImage(BufferedIma ...

  2. 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?

    1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现重复元素,当然也只允许有一个null对象. 2.如何来区分重复与否呢?(how) “ 用 iterator() ...

  3. c++ _int64 转成string

    _i64toa(a,buffer,10); scanf("%I64d",&a);printf("%I64d",a); 就可以正确输入输出了.当使用uns ...

  4. jQuery弹出遮罩层效果完整示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Unity 移动端的复制这么写

    游戏上线很久了,有些玩家慢慢就流失了,为了让刚流失的玩家再度回归所以做了召回功能!如果一个200级的玩家10天没上线且成功召回的,就会给予召回玩家丰厚的奖励! Q:那如何召回这个流失的玩家呢? A:召 ...

  6. javascript在字符串中提取网址并替换成超链接

    var str = " http://wasmip.baidu.com.cn/mip/km/archives/km_archives_main/kmArchivesMain.do?metho ...

  7. NodeJs生成SVG图形验证码

    背景:短信接口有调用限制,如果受到恶意攻击,很容易就爆掉,所以需要一系列验证机制,后端采用签名加密的方式,而前端要做人机识别,有两个要求: 1)不能使用文本式的验证码,很简单就能拿到 2)所有验证逻辑 ...

  8. 用cocos2d 2.1制作一个过河小游戏(4): 游戏主逻辑BaseLayer设计

    前段时间一直在忙.没有时间更新博客.今天还是抽点时间把最后一小部分游戏的实现放上来吧. BaseLayer.h: #import <GameKit/GameKit.h> #import & ...

  9. /etc/hostname

    我们可以使用 hostname 命令来修改主机名,但只是临时生效,如果想永久生效可以编辑 /etc/hostname 文件,注意不是每个 Linux 发行版都有该文件 root@Ubuntu_Lee: ...

  10. linux下用gcc如何生成预处理、汇编等文件

    [gcc -E test.c -o test.i------>预处理文件生成.i 文件.] 1.c语言程序生成过程 C语言程序的生成过程可以简单的分为:编辑.预处理.编译.汇编.链接五个阶断. ...