●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 题意: 给你一个字符串, 问你做少须要 ...
随机推荐
- 2017-2018-1 1623 bug终结者 冲刺006
bug终结者 冲刺006 by 20162328 蔡文琛 今日任务:音频素材添加 又是新的一天,小组项目有了很大的起色,已经可以在手机上试玩了. 添加背景音乐能使我们的游戏锦上添花. 音频资源需求 需 ...
- PYTHON 词云
#!/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from wordcloud import W ...
- Android接受验证码自动填入功能(源码+已实现+可用+版本兼容)
实际应用开发中,会经常用到短信验证的功能,这个时候如果再让用户就查看短信.然后再回到界面进行短信的填写,难免有多少有些不方便,作为开发者.本着用户至上的原则我们也应该来实现验证码的自动填写功能,还有一 ...
- nyoj 开方数
开方数 时间限制:500 ms | 内存限制:65535 KB 难度:3 描述 现在给你两个数 n 和 p ,让你求出 p 的开 n 次方. 输入 每组数据包含两个数n和p.当n和p都为0 ...
- Junit 4 测试中使用定时任务操作
难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...
- 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
依赖注入在 ASP.NET Core 中起中很重要的作用,也是一种高大上的编程思想,它的总体原则就是:俺要啥,你就给俺送啥过来.服务类型的实例转由容器自动管理,无需我们在代码中显式处理. 因此,有了依 ...
- vue表单详解——小白速会
一.基本用法 你可以用 v-model 指令在表单 <input> 及 <textarea> 元素上创建双向数据绑定. 但 v-model 本质上不过是语法糖.它负责监听用户的 ...
- VMware vCenter Server 6.5.0 U1
VMware vCenter Server 6.5.0 U1gName: VMware-VCSA-all-6.5.0-8024368.iso Release Date: 2018-03-20 Buil ...
- docker实践
我的docker 学习笔记2 ps axf docker run -d cyf:sshd /usr/sbin -D docker ps docker-enter.sh 686 ps axf ...
- 新概念英语(1-51)A pleasant climate
新概念英语(1-51)A pleasant climate Does it ever snow in Greece? A:Where do you come from? B:I come from G ...