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很小,所以对每 ...
随机推荐
- neutron-删除负载均衡器
neutron-删除负载均衡器 在清除垃圾数据的时候,删除负载均衡器,总是有很多依赖.写了一个脚本,连同依赖资源一起删除 #!/bin/bash delete(){ local id id=$1 lo ...
- 计算机网络--差错检测(帧检验序列FCS计算方法)
我们知道数据链路层广泛使用循环冗余检验CRC的检验技术 现在我们知道要发送的数据M=101001(长度为k=6) 在我们每次发送数据的时候需要在M后面添加一个N位的冗余码,一共发送(k+N)位数据 ...
- java 11 完全支持Linux容器(包括Docker)
许多运行在Java虚拟机中的应用程序(包括Apache Spark和Kafka等数据服务以及传统的企业应用程序)都可以在Docker容器中运行.但是在Docker容器中运行Java应用程序一直存在一个 ...
- kubernetes云平台管理实战: 高级资源deployment-滚动升级(八)
一.通过文件创建deployment 1.创建deployment文件 [root@k8s-master ~]# cat nginx_deploy.yml apiVersion: extensions ...
- JAVA字符串
字符串 1. 字符串 1.1 字符串概述和特点 java.lang.String类代表字符串. API当中说:Java 程序中的所有字符串字面值(如 "abc" )都作为此类的 ...
- JavaScript对象 Object类型基础
前言 JavaScript 对象是整个语言学习的一个难点.本文主要带大家入门学习Object知识 对象定义 javascript的基本数据类型包括undefined.null.boolean.stri ...
- 四十九、进程间通信——System V IPC 之消息队列
49.1 System V IPC 介绍 49.1.1 System V IPC 概述 UNIX 系统存在信号.管道和命名管道等基本进程间通讯机制 System V 引入了三种高级进程间通信机制 消息 ...
- 利用div绘制细线居中
利用div配合css代码实现细线方式: 块级元素水平居中步骤: 设置宽度width 设置margin-left:auto; 设置margin-right:auto; 实现方法是,让其左右两边的外边距自 ...
- 51nod 1016 水仙花数
大水仙花数模板+1…… #include<stdio.h> #include<math.h> #include<queue> #include<vector& ...
- day02 解释器安装及初识变量
今日内容: 1.解释器的安装 2.添加到环境变量 3.pip初识 4.变量初识 5.PyCharm安装及激活 今日重点: 1.将python及pip添加到环境变量 在将python解释器安装到计算机后 ...