Palindrome POJ - 3974 (字符串hash+二分)
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 题意:给一个字符串,找出其中最长回文子串长度
思路:二分枚举其长度,然后枚举每个位置每个位置,利用字符串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+二分)的更多相关文章
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- poj 2503 字符串hash
题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3865 - Database 字符串hash
[题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...
随机推荐
- 【LOJ2586】【APIO2018】选圆圈 CDQ分治 扫描线 平衡树
题目描述 在平面上,有 \(n\) 个圆,记为 \(c_1,c_2,\ldots,c_n\) .我们尝试对这些圆运行这个算法: 找到这些圆中半径最大的.如果有多个半径最大的圆,选择编号最小的.记为 \ ...
- HTTP 404 Not Found Error with .woff or .woff2 Font Files
如果是 ERR_ABORTED 404 (Not Found) WOFF2,则有可能是文件不存在
- JDK动态代理(Proxy)的两种实现方式
JDK自带的Proxy动态代理两种实现方式 前提条件:JDK Proxy必须实现对象接口 so,创建一个接口文件,一个实现接口对象,一个动态代理文件 接口文件:TargetInterface.java ...
- JS 防抖函数和节流函数
文章转载自:木上有水 什么是防抖?什么是节流? 工作中我们经常会用一些方法监听某些事件的完成,比如scroll.resize.keyup等. 常规事件触发的时候,比如scroll,会在短时间内触发多次 ...
- busybox(二)编译
title: busybox(二)编译 tag: arm date: 2018-11-13 23:14:58 --- busybox(二)编译 解压,源码包在busybox-1.7.0.tar.bz2 ...
- Shiro与CAS整合实现单点登录
1.简介 CAS:Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法. Shiro:Apache Shiro是一个Java安全框架,可以帮助我们完成认证.授权.会话管 ...
- LFYZ-OJ ID: 1011 hanoi双塔问题
思路 虽然每种大小盘子数量为2,但对总步数的影响只是一个简单的倍数关系而已,递推关系很容易可以总结出来:an=an-1+2+an-1=2(an-1+1),n=1时,a1=2.故递推的过程就是从a1=2 ...
- EffectiveC++ 第5章 实现
我根据自己的理解,对原文的精华部分进行了提炼,并在一些难以理解的地方加上了自己的"可能比较准确"的「翻译」. Chapter 5 实现 Implementations 适当提出属于 ...
- tf.py_func
在 faster rcnn的tensorflow 实现中看到这个函数 rois,rpn_scores=tf.py_func(proposal_layer,[rpn_cls_prob,rpn_bbox ...
- ArcGis汇总篇
ArcGis-javascript-API下载 bigemap.太乐地图 可下载地图文件 用arcgis for js 可以河流流域水质图 ArcGis导出shp文件(dbf.prj.sbn.sbx. ...