【hdu3294】Girls' research——manacher

本文详细介绍了一道关于寻找字符串中最长回文子串的问题,并通过Manacher算法高效解决。文章提供了完整的代码实现,解释了如何通过字符映射和Manacher算法找到最长回文串及其位置。

Problem Description

One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:

First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.


Input

Input contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'a' is and the length of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.
 
Output
Please execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!".
If there are several answers available, please choose the string which first appears.
 
Sample Input
b babd
a abcd
 
Sample Output
0 2 aza
No solution!
 
 
其实这道题本质上就是一道裸的manacher,我们知道替换字母并不会影响它是不是回文,该是回文的还是,不是的也肯定依然不是,因此我们可以先跑一遍manacher,然后再进行字母的转换(这样可以避免对不是最长回文的字母进行无谓的转换)。
关于起始点和终止点,分别是(a-p[a]+2)/2-1(a+p[a]-2)/2-1。其中a是最长回文串的对称轴,至于这是为什么希望你能自己推导一下。
最后是这道题最特殊的地方——字母替换:
首先要输入的是一个数组,这样读到EOF时才会退出循环。
然后int k为c[0]-'a',即向前进几个单位,用s[i]-'a'-k来表示是第几个字母,不过这样得到的可能是一个负数,所以应该是(s[i]-'a'-k+26)%26+'a'。
具体实现细节看代码。
#include<cstdio>
#include<cstring>
#include<iostream>
const int maxn=400010;
using namespace std;
char c[2],s[maxn];
int pos,len,p[maxn],a;
int main()
{
    while(~scanf("%s %s",c,s))
    {
        len=strlen(s);pos=0;a=0;
        for(int i=len;i>=0;i--)
        {
            s[i*2+2]=s[i];
            s[i*2+1]='#';
        }
        s[0]='&';p[0]=0;
        for(int i=2;i<len*2+1;i++)
        {
            if(pos+p[pos]>i)p[i]=min(p[pos*2-i],pos+p[pos]-i);
            else p[i]=1;
            while(s[i+p[i]]==s[i-p[i]])p[i]++;
            if(pos+p[pos]<i+p[i])pos=i;
            if(p[a]<p[i])a=i;
        }
        if(p[a]-1<2)printf("No solution!\n");
        else{
            printf("%d %d\n",(a-p[a]+2)/2-1,(a+p[a]-2)/2-1);
            int k=c[0]-'a';
            for(int i=a-p[a]+2;i<=a+p[a]-2;i+=2)printf("%c",(s[i]-'a'-k+26)%26+'a');
            printf("\n");
        }
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/JKAI/p/6950872.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值