poj3974 Palindrome【回文】【Hash】【二分】
| Time Limit: 15000MS | Memory Limit: 65536K | |
| Total Submissions: 13157 | Accepted: 5028 | 
Description
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
Output
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】【二分】的更多相关文章
- leetcode4	Valid Palindrome回文数
		
Valid Palindrome回文数 whowhoha@outlook.com Question: Given a string, determine if it is a palindrome, ...
 - LeetCode: Palindrome 回文相关题目
		
LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...
 - 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)
		
题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...
 - hdu 1159 Palindrome(回文串)  动态规划
		
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
 - Palindrome 回文数
		
回文数,从前到后,从后到前都一样 把数字转成字符串来处理 package com.rust.cal; public class Palindrome { public static boolean i ...
 - valid palindrome(回文)
		
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
 - WHU 583 Palindrome ( 回文自动机 && 本质不同的回文串的个数 )
		
题目链接 题意 : 给你一个串.要你将其划分成两个串.使得左边的串的本质不同回文子串的个数是右边串的两倍.对于每一个这样子的划分.其对答案的贡献就是左边串的长度.现在要你找出所有这样子的划分.并将贡献 ...
 - 洛谷T89644 palindrome回文串
		
洛谷 T89643 回文串(并查集) 洛谷:https://www.luogu.org/problem/T89643 题目描述 由于 Kiana 实在是太忙了,所以今天的题里面没有 Kiana. 有一 ...
 - palindrome 回文 /// Manacher算法
		
判断最长不连续回文 #include <bits/stdc++.h> using namespace std; int main() { ]; while(gets(ch)) { ],an ...
 - 回文(palindrome)
		
如果一个字符串忽略标点符号.大小写和空格,正着读和反着读一模一样,那么这个字符串就是palindrome(回文).
 
随机推荐
- 把py文件打成exe
			
使用pyinstaller: pyinstaller -F -w -i manage.ico demo.py -F:打包为单文件-w:Windows程序,不显示命令行窗口-i:是程序图标,demo.p ...
 - 科技发烧友之单反佳能700d中高端
			
http://detail.zol.com.cn/series/15/15795_1.html 前三 佳能 尼康 索尼 佳能5d 1.6w 佳能70d 5k 佳能6d 9k 佳能d7100 5k 尼康 ...
 - 比較JS合并数组的各种方法及其优劣
			
原文链接: Combining JS Arrays原文日期: 2014-09-09翻译日期: 2014-09-18翻译人员: 铁锚 本文属于JavaScript的基础技能. 我们将学习结合/合并两个 ...
 - iOS: Assertion failure on picker view
			
Q:I'm getting an assertion failure while scrolling a picker view w/ zero data(zero rows). While scro ...
 - js正则表达式的应用
			
JavaScript表单验证email,判断一个输入量是否为邮箱email,通过正则表达式实现. //检查email邮箱 function isEmail(str){ var reg = /^([a- ...
 - phpcms二级栏目的调用
			
1.二级栏目的调用方法 {php $data = subcat($module, $catid);} {loop $data $n $r} {if $r[ismenu]} {$r[catname]} ...
 - spring 事物管理没起到作用
			
今天在做项目的时候发现配置的spring 事物管理没起到作用.可是配置又是依据官网配置的,不可能会错.最后发现使mysql的问题 普通情况下,mysql会默认提供多种存储引擎,你能够通过以下的查看: ...
 - Effective C++ Item 14 Think carefully about copying behavior in resource-managing classe
			
In C++, the only code that guaranteed to be executed after an exception is thrown are the destructor ...
 - Html解析
			
相关解析组件: HtmlAgilityPack CsQuery Winista.Text.HtmlParser
 - union和union all的并集(相加)区别
			
Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...