Palindrome(manacher算法)

最长回文子串算法详解:从暴力到Manacher 回文串是计算机科学中经典的字符串处理问题,指正读反读都相同的字符序列。其核心算法原理包括中心扩展和动态规划两种范式,时间复杂度可优化至O(n^2)甚至O(n)。在工程实践中,中心扩展算法因其实现简单、效率均衡成为面试首选,而Manacher算法则通过预处理和对称性利用达到线性时间复杂度。这类算法在DNA序列分析、数据压缩等领域有重要应用,也是LeetCode等编程竞赛中的高频考点。掌握回文子串检测技术,不仅能解决最长回文子串问题,还可延伸应用于回文子序列统计、字符串分割等变种问题。 阅读详情

Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 11080 Accepted: 4212

Description

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

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

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).

Output

For each test case in the input print the test case number and the length of the largest palindrome.

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6

Source


题意:寻找 给出字符串 中最长的回文字串,输出它的长度;
思路:manacher算法-----利用最短时间,找出最长回文字串的长度;

manacher算法详解参考博客:https://blog.youkuaiyun.com/dyx404514/article/details/42061017

                                              https://segmentfault.com/a/1190000008484167#articleHeader2

此题代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int mm=1000005;
char a[mm],tmp[mm*2];
int p[mm*2];
int init()
{
    int la=strlen(a);
    tmp[0]='$';                         //最左端分界符
    tmp[1]='#';                         //字母之间的分隔符
    int j=2;
    for(int i=0;i<la;i++)
    {
        tmp[j++]=a[i];             //转换字符串
        tmp[j++]='#';
    }
    tmp[j]='\0';
    return j;
}
int mach()
{
    int len=init();     
    int id,mx=0,maxx=-1;             //id是最大回文字串中心,mx是回文字串能延伸到最右端的位置。
    for(int i=1;i<len;i++)
    {  
        if(mx>i)
            p[i]=min(p[2*id-i],mx-i);          
        else
            p[i]=1;                            //i>=mx 
        while(tmp[i-p[i]]==tmp[i+p[i]])     
            p[i]++;
        if(mx<p[i]+i)
        {
             id=i;                          //更新id,mx的值;
             mx=p[i]+i;          
        }
        maxx=max(maxx,p[i]-1);    
    }
    return maxx;
}
int main()
{
    int ca=1;   
    while(~scanf("%s",a))
    {
        if(!strcmp(a,"END"))
            break;   
        else
            printf("Case %d: %d\n",ca++,mach());                
         mem(p,0);
    }
    return 0;
}

STM32F429+LAN8720实战:5分钟搞定LWIP裸机网络通讯(附完整代码) 本文详细介绍了如何在STM32F429微控制器上,结合LAN8720 PHY芯片,在裸机环境下快速部署和配置LWIP协议栈以实现网络通讯。内容涵盖硬件RMII接口连接、STM32CubeMX关键配置、代码移植与定制,以及常见问题排查方法,帮助工程师在资源受限的嵌入式系统中高效实现稳定的以太网功能。 阅读详情

相关推荐

边缘AI硬件风向:从专用芯片到端侧大模型

边缘AI硬件正呈现五大发展趋势:专用AI加速芯片(如华为昇腾、谷歌EdgeTPU)凭借高能效比成为主流;端侧大模型催生支持Transformer架构的新硬件需求;存算一体技术突破内存墙限制;FPGA和可重构计算提供灵活性;异构集成与Chiplet技术实现模块化设计。这些发展需要配套软件工具链支持,未来边缘设备将演变成具备本地智能的"智能体",竞争焦点转向算力、能效、易用性和成本的多维平衡。

qq_37027335的博客 348

Palindrome (马拉车算法)

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the lar

码农的一亩三分地 489

别再魔改LWIP了!用STM32CubeMX配置F407的TCP保活(KeepAlive)与自动重连

本文详细介绍了如何通过STM32CubeMX配置STM32F407的LWIP协议栈,实现TCP保活(KeepAlive)与自动重连功能,解决网络异常断开问题。文章提供了从基础环境配置到自动重连架构设计的完整指南,帮助开发者构建高可靠的网络连接机制,适用于工业物联网和嵌入式设备开发。

weixin_27038245的博客 236

算法习题篇之Palindrome

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explana...

灬影的博客 554

突发E层(Es层):短波通信中的“意外惊喜”与“隐形杀手”全解析

本文深入解析了突发E层(Es层)在短波通信中的双重作用,既可能带来超远距离通信的“意外惊喜”,也可能成为通信链路的“隐形杀手”。通过分析Es层的形成机制、时空分布规律及实战应用技巧,帮助通信爱好者与专业人员更好地利用或应对这一电离层现象,提升短波通信的可靠性与效率。

weixin_30443813的博客 825

暗黑地牢modfiles.txt文件生成工具

暗黑地牢modfiles.txt文件生成工具引言方法工具源码基本步骤 引言 玩过暗黑地牢mod,并手动修改了mod中文件的话,如果不同步修改modfiles.txt文件,就会引起各种问题,典型的比如马车刷不出mod英雄,商店和boss都不再出现mod饰品,怪物贴图丢失出现黑块等,要解决这个问题,就要修正mod文件夹下的modfiles.txt文件 方法 针对修改量大小,一般有几种方法: 如果修改较少,而且你还记得修改了哪几个文件,找到这几个文件,右键属性,查看大小,要用的是括号里面的字节数,然后打开mod

lttclaw_的专栏 5002

算法导论15.2 最长回文子序列 Longest palindrome subsequence

算法导论15.2 最长回文子序列 子序列可以是不连续的

Open, Share, Improve -大国小民之程序人生 3892

MATLAB 与 STK 互联导出导弹轨迹及速度数据(csv导出)

本文介绍了如何通过 MATLAB 脚本连接 STK 提取导弹三维轨迹数据,并以 CSV 格式导出,包含以下关键内容:自动连接 STK 场景;获取所有导弹对象;提取 J2000 坐标系下的位置和速度;加入任务优先级与状态;导出为标准 CSV 文件;

weixin_52370774的博客 779

poj3974 Palindrome (最长回文子串)

Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 2371   Accepted: 864 Description Andy the smart computer science student was attending an algorithm

把复杂的事情简单描述 1001

交换机初始化配置详解

MGMT或称MGT(Management Port,管理端口)是一个可以连接计算机的三层主机端口,如下图所示。只需使用一条双绞线跳线,就可以将计算机的以太网端口与交换机的MGMT端口连接在一起,实现对交换机的配置与管理。也就是说,可以使用MGMT端口代替Console端口实现对交换机或交换机堆叠的网络管理。MGMT端口既可用于交换机的初始化,也可以在交换机初始化完成后,以Telnet、Web浏览器或Cisco CNA方式进行管理。当将计算机连接到独立的交换机时,连接方式如下图所示。

悦分享 1996

华为OD机试:查找接口成功率最优时间段 (C/C++/Py/Java/JS/GO)

华为OD机试双机位C卷 查找接口成功率最优时间段,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。

qq_45776114的博客 4687

【代码超详解】POJ 3974 Palindrome(Manacher 算法 · 模板)

一、题目描述 二、算法分析说明与代码编写指导 Manacher 算法 注意:fgets 的第二个参数给出的长度需要至少比字符串的最长长度大 2,因为 fgets 会保存换行符 ‘\n’,并在读取到 ‘\n’ 后写 0 结束字符串。如果只比字符串的最长长度大 1,那么当字符串的长度达到最大时,紧跟其后的 ‘\n’ 将不会被读取。下一次 fgets 时会因为读取到了 ‘\n’ 而直接结束读取,导致 ...

COFACTOR 280

11、机器学习中的聚类与隐马尔可夫模型

本文介绍了机器学习中的两种重要方法:聚类算法和隐马尔可夫模型(HMM)。聚类作为无监督学习的代表,适用于发现无标签数据中的结构,常见算法包括K-均值和自组织映射(SOM),并在传感器数据分析等场景中展现应用价值。隐马尔可夫模型则是一种可解释的序列模型,用于从观测数据中推断隐藏状态,广泛应用于语音识别、自然语言处理和生物信息学等领域。文章详细讲解了HMM的核心概念如马尔可夫性质、转移与发射概率,并通过TensorFlow实现了前向算法,展示了其计算观测序列概率的动态规划思想。最后对比了两类方法的应用场景,展望

garlic的博客 179

【Manacher算法】Palindrome

POJ 3974 Palindrome,模板题,求最长回文子串长度,算法用了 Manacher 算法

欢迎您的光顾awa 309

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Palindrome Centers Manacher算法

题目大意一个长度为n的字符串S, 告诉你以每个位置结尾的最长回文串长度, 要你输出以每个字符为中心的最长长度思路开始的时候以为是水题, 以为用题目给的回文串长度, 找到中心, 更新长度就好了, 但这样是错的, 因为如果以j为结尾的回文串中心是i, 以i为中心的最长回文串不一定是只到j, 后面号可能有一个大于j的k, 以i为中心, k为边界也是回文串, 但是以k结尾还可能有一个更长的回文串, 长度为偶

litmxs的博客 298

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

URAL1297 Palindrome【manacher算法】

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» ha...

海岛Blog 559

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

[codeforces 1326D2] Prefix-Suffix Palindrome (Hard version) Manacher算法 马拉车算法

Codeforces Global Round 7 比赛人数10630 [codeforces 1326D2] Prefix-Suffix Palindrome (Hard version) Manacher算法 马拉车算法 总目录详见https://blog.youkuaiyun.com/mrcrack/article/details/103564004 也在线测评地址https://code...

mrcrack的博客 602

Palindrome-(Manacher算法)

题目链接:点击进入 题目 题意 给定一个字符串,求最长回文子串。 思路 Manacher算法模板 代码 #include<iostream> #include<string> #include<map> //#include<unordered_map> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include&l

Cosmic_Tree的博客 133

POJ 3974-Palindrome(Manacher算法)

题目地址:POJ 3974 题意:求最长的回文串。 思路:同样是用Mancher算法在O(n)的时间内解决(我其实是来练练板子的#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm

Rocky0429 1773

palindrome 回文 /// Manacher算法

判断最长不连续回文 #include <bits/stdc++.h> using namespace std; int main() { char ch[1500]; while(gets(ch)) { int len=strlen(ch),dp[1500],ans=0; for(int i=0;i...

babimei370256276的博客 95

从Shortest Palindrome谈到Manacher和KMP算法

投出去的三份实习简历都没收到消息...略郁闷...只好闷头刷题了QAQ.... 最近按题目类型在重刷Leetcode, 当中做到Shortest Palindrome,开始是用后缀数组做,结果TLE... 只好用马拉车法AC...顺便用KMP算法写了一份AC代码.... 后缀数组方法就不说了,可以参考博文后缀数组学习笔记... 马拉车法(Manacher)可以参考博文Manacher算法.

Speknighter的专栏 729

Extend to Palindrome - UVa 11475 Manacher算法

Problem E Extend to Palindromes Time Limit : 3 seconds       Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of l

BlueFissure's Blog 827

Prefix-Suffix Palindrome (Manacher算法)

题目链接 Manacher算法 #include<bits/stdc++.h> using namespace std; int t; string s; int q[2000006]; char ss[2000006]; int manacher(string a) { int len=a.size(); int now=0; for(int i=0;i<len...

郭源潮 323

POJ-3974 Palindrome(manacher算法)

题目链接:点击打开链接 经典的求最长回文子串题目,需要用到manacher算法。 manacher算法的具体介绍:http://blog.youkuaiyun.com/ggggiqnypgjg/article/details/6645824 #include #include #include #include #include using namespace std; const int maxn=1

CHeck_Check_check的博客 435

POJ 3974 Palindrome 回文数 Manacher算法

原文地址http://www.2cto.com/kf/201210/164253.html 该题就是求一个字符串的最长回文子串,就是一个满足本身是回文的最长的子串。该题貌似可以用后缀数组和扩展kmp做,但是好像后缀数组貌似会tle,改学了下一个专门的叫Manacher算法的东西。。。 该算法说起来也不是太复杂,比较容易看懂的那种,当然是接触过其它字符串算法的前提下了...

184

【Manacher算法】poj3974 Palindrome

Manacher算法教程:http://blog.youkuaiyun.com/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std;...

weixin_34326429的博客 122
上一篇: J - J 畅通工程续
下一篇: Convex
team39
博客等级 码龄9年 80粉丝 218原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值