●POJ 3974 Palindrome(Manacher)
题链:
http://poj.org/problem?id=3974
题解:
Manacher
求最长回文串长度。
终于会了传说中的马拉车,激动。
推荐一个很棒的博客:https://www.61mon.com/index.php/archives/181/
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 2005000
#define filein(x) freopen(#x".in","r",stdin);
#define fileout(x) freopen(#x".out","w",stdout);
using namespace std;
char T[MAXN]; int cnt;
int trans(char *S){
int lS=strlen(S),lT=2;
T[0]='&'; T[1]='#';
for(int i=0;i<lS;i++){
T[lT++]=S[i];
T[lT++]='#';
}
return lT;
}
int Manacher(int N,char *S){
static int p[MAXN],ans,pos,maxr;
pos=maxr=ans=0;
for(int i=1;i<N;i++){
if(i<maxr) p[i]=min(p[2*pos-i],maxr-i);
else p[i]=1;
while(S[i+p[i]]==S[i-p[i]]) {
if(i+p[i]>maxr){
maxr=i+p[i],pos=i;
if(S[i+p[i]-1]!='#')cnt++;
}
p[i]++;
}
ans=max(ans,p[i]-1);
}
return ans;
}
int main()
{
static char S[MAXN],cas=0;
while(1){
scanf("%s",S);
if(S[0]=='E'&&S[1]=='N'&&S[2]=='D'&&S[3]==0) break;
int N=trans(S); cnt=0;
int ans=Manacher(N,T);
printf("Case %d: %d\n",++cas,ans);
}
return 0;
}
●POJ 3974 Palindrome(Manacher)的更多相关文章
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3974 Palindrome 字符串 Manacher算法
http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- POJ 3974 Palindrome | 马拉车模板
给一个字符串,求最长回文字串有多长 #include<cstdio> #include<algorithm> #include<cstring> #define N ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- POJ 3974 Palindrome (算竞进阶习题)
hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
随机推荐
- Django 分类标签查找
from django.conf.urls import url from django.contrib import admin from blog.views import index,stude ...
- linux——网络基础
装完linux系统要对网络(ip地址,子网掩码,网关,DNS)进行配置,才能连接网络 一,开启网卡eth0 CentOS显示没有网卡(eth0) 2.设置静态IP vim /etc/sysconfig ...
- kubernetes进阶(05)kubernetes的命令
在Kubernetes中,Node.Pod.Replication Controller.Service等概念都可以看作一种资源对象,通过Kubernetes提供的Kubectl工具或者API调用进行 ...
- SpringCloud的服务注册中心(四)- 高可用服务注册中心的搭建
一.双 服务注册注册中心 1.服务注册中心的服务端 - EurekaServer 1.1.EurekaServer1 String.application.name=eureka-server ser ...
- Spring Security 入门(1-1)Spring Security是什么?
1.Spring Security是什么? Spring Security 是一个安全框架,前身是 Acegi Security , 能够为 Spring企业应用系统提供声明式的安全访问控制. Spr ...
- ssh整合之五struts和spring整合
1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了 另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么 ...
- 浅谈Web网站的架构演变过程
前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变. 该系统具备的功能: 用户模块:用户注册和管理 商品模块:商品展示和管理 交易模块:创建交易和管理 阶 ...
- JavaScript中的 原型 property 构造函数 和实例对象之间的关系
1 为什么要使用原型? /* * javascript当中 原型 prototype 对象 * * */ //首先引入 prototype的意义,为什么要使用这个对象 //先来写一个构造函数的面向对象 ...
- python开发:初识python
python简介 Python可以应用于众多领域,如:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算等众多领域.目前业内几乎所有大中型互联网企业都在使用Python,如:Youtube.D ...
- Python系列-python内置函数
abs(x) 返回数字的绝对值,参数可以是整数.也可以是浮点数.如果是复数,则返回它的大小 all(iterable) 对参数中的所有元素进行迭代,如果所有的元素都是True,则返回True,函数等价 ...