题链:

http://poj.org/problem?id=3974

题解:

Manacher

求最长回文串长度。

终于会了传说中的马拉车,激动。
推荐一个很棒的博客:https://www.61mon.com/index.php/archives/181/

代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #define MAXN 2005000
  5. #define filein(x) freopen(#x".in","r",stdin);
  6. #define fileout(x) freopen(#x".out","w",stdout);
  7. using namespace std;
  8. char T[MAXN]; int cnt;
  9. int trans(char *S){
  10. int lS=strlen(S),lT=2;
  11. T[0]='&'; T[1]='#';
  12. for(int i=0;i<lS;i++){
  13. T[lT++]=S[i];
  14. T[lT++]='#';
  15. }
  16. return lT;
  17. }
  18. int Manacher(int N,char *S){
  19. static int p[MAXN],ans,pos,maxr;
  20. pos=maxr=ans=0;
  21. for(int i=1;i<N;i++){
  22. if(i<maxr) p[i]=min(p[2*pos-i],maxr-i);
  23. else p[i]=1;
  24. while(S[i+p[i]]==S[i-p[i]]) {
  25. if(i+p[i]>maxr){
  26. maxr=i+p[i],pos=i;
  27. if(S[i+p[i]-1]!='#')cnt++;
  28. }
  29. p[i]++;
  30. }
  31. ans=max(ans,p[i]-1);
  32. }
  33. return ans;
  34. }
  35. int main()
  36. {
  37. static char S[MAXN],cas=0;
  38. while(1){
  39. scanf("%s",S);
  40. if(S[0]=='E'&&S[1]=='N'&&S[2]=='D'&&S[3]==0) break;
  41. int N=trans(S); cnt=0;
  42. int ans=Manacher(N,T);
  43. printf("Case %d: %d\n",++cas,ans);
  44. }
  45. return 0;
  46. }

●POJ 3974 Palindrome(Manacher)的更多相关文章

  1. poj 3974 Palindrome (manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

  2. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  4. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  5. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  6. POJ 3974 Palindrome | 马拉车模板

    给一个字符串,求最长回文字串有多长 #include<cstdio> #include<algorithm> #include<cstring> #define N ...

  7. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  8. POJ 3974 Palindrome (算竞进阶习题)

    hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...

  9. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

随机推荐

  1. 2017-2018-1 1623 bug终结者 冲刺006

    bug终结者 冲刺006 by 20162328 蔡文琛 今日任务:音频素材添加 又是新的一天,小组项目有了很大的起色,已经可以在手机上试玩了. 添加背景音乐能使我们的游戏锦上添花. 音频资源需求 需 ...

  2. PYTHON 词云

    #!/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from wordcloud import W ...

  3. Android接受验证码自动填入功能(源码+已实现+可用+版本兼容)

    实际应用开发中,会经常用到短信验证的功能,这个时候如果再让用户就查看短信.然后再回到界面进行短信的填写,难免有多少有些不方便,作为开发者.本着用户至上的原则我们也应该来实现验证码的自动填写功能,还有一 ...

  4. nyoj 开方数

    开方数 时间限制:500 ms  |  内存限制:65535 KB 难度:3   描述 现在给你两个数 n 和 p ,让你求出 p 的开 n 次方.   输入 每组数据包含两个数n和p.当n和p都为0 ...

  5. Junit 4 测试中使用定时任务操作

    难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...

  6. 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类

    依赖注入在 ASP.NET Core 中起中很重要的作用,也是一种高大上的编程思想,它的总体原则就是:俺要啥,你就给俺送啥过来.服务类型的实例转由容器自动管理,无需我们在代码中显式处理. 因此,有了依 ...

  7. vue表单详解——小白速会

    一.基本用法 你可以用 v-model 指令在表单 <input> 及 <textarea> 元素上创建双向数据绑定. 但 v-model 本质上不过是语法糖.它负责监听用户的 ...

  8. 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 ...

  9. docker实践

    我的docker 学习笔记2   ps axf docker run -d cyf:sshd /usr/sbin -D   docker  ps docker-enter.sh 686 ps axf ...

  10. 新概念英语(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 ...