hdu-3294(最长回文子串)
题意:给你一个字符和一个字符串让你求出最长回文子串并且输出来,答案需要根据给出的字符转换一下,就是将给出的字符认定为a,然后依次向后推;
解题思路:manacher模板+一些处理
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<Cstdio>
using namespace std;
char s[200500],a[400500];
char t[30],m;
int p[400500];
int n;
int pos;
int change()
{
int i,j,t;
a[0]='$';
a[1]='#';
j=2;
for(i=0;i<n;i++)
{
a[j++]=s[i];
a[j++]='#';
}
a[j]='\0';
return j;
}
int manacher()
{
int len=change();
int maxlen=-1;
int id;
int mx=0;
for(int i=1;i<len;i++)
{
if(i<mx)
p[i]=min(p[id*2-i],mx-i);
else
p[i]=1;
while(a[i-p[i]]==a[i+p[i]])
p[i]++;
if(mx<p[i]+i)
{
mx=p[i]+i;
id=i;
}
if(maxlen<p[i]-1)
{
maxlen=p[i]-1;
pos=i;
}
}
return maxlen;
}
int main()
{
int x,y;
//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
t[0]='z';t[1]='a';
for(int i=2;i<=25;i++)
t[i]=t[i-1]+1;
while(scanf("%c %s",&m,s)!=EOF)
{
getchar();
n=strlen(s);
int ans=manacher();
x=(pos-ans+1)/2-1;y=(ans+pos-1)/2-1;
if(ans<=1)
{
printf("No solution!\n");
}
else
{
printf("%d %d\n",x,y);
for(int i=x;i<=y;i++)
{
int xx=s[i]-m;
xx++;
xx=xx+26;xx=xx%26;
printf("%c",t[xx]);
}
printf("\n");
}
}
}
hdu-3294(最长回文子串)的更多相关文章
- hdu 3068 最长回文子串 TLE
后缀数组+RMQ是O(nlogn)的,会TLE..... 标准解法好像是马拉车,O(n).... #include "algorithm" #include "cstdi ...
- hdu 3068 最长回文子串 马拉车模板
前几天用后缀数组写过一次这题,毫无疑问很感人的TLE了-_-|| 今天偶然发现了马拉车模板,O(N)时间就搞定 reference:http://acm.uestc.edu.cn/bbs/read.p ...
- HDU 3068 [最长回文子串]
#include<iostream> #include<string> #include<string.h> #include<algorithm> # ...
- 最长回文子串(百度笔试题和hdu 3068)
版权所有.所有权利保留. 欢迎转载,转载时请注明出处: http://blog.csdn.net/xiaofei_it/article/details/17123559 求一个字符串的最长回文子串.注 ...
- hdu 3068 最长回文(manachar求最长回文子串)
题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...
- HDU 3068 最长回文 【最长回文子串】
和上一题一样,不过这题只是要求最长回文子串的长度 在此采用了非常好用的Manacher算法 据说还是O(n) 的效率QAQ 详细用法参考了上篇博客的参考资料,这两天有空学习一下~ Source cod ...
- HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)
Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- Manacher模板( 线性求最长回文子串 )
模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...
- 【转】最长回文子串的O(n)的Manacher算法
Manacher算法 首先:大家都知道什么叫回文串吧,这个算法要解决的就是一个字符串中最长的回文子串有多长.这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文 ...
- 最长回文子串(Manacher算法)
回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...
随机推荐
- hibernate操纵数据库常用方法 及 hibernate对象的三种状态
在dao层使用hibernate语言来与数据库进行访问,hibernate作为面向对象思想开发的dao层框架其理解也需要以面向对象的思想来看待 使用.hibernate不仅支持使用者使用他提供的对象来 ...
- Graph Without Long Directed Paths CodeForces - 1144F (dfs染色)
You are given a connected undirected graph consisting of nn vertices and mm edges. There are no self ...
- Python_面向对象_单例模式
class A(object): pass a1 = A() a2 = A() print(a1 == a2)print(id(a1))print(id(a2)) 结果: False 23257231 ...
- HTML 5 Web 音频
HTML 5 音频http://www.w3school.com.cn/html5/html_5_audio.asp 在 Web 上播放音频http://www.w3school.com.cn/med ...
- Alibaba Cloud Toolkit for Eclipse & ECS、EDAS 或容器服务 Kubernetes
UserGuide_V2.1.0http://toolkit.aliyun.com/eclipse/?spm=5176.2020520130.105.3.3c3b697bOHma9f&msct ...
- C# foreach内部原理
我们知道使用foreach的一个要求是对象必须继承自IEnumerable接口 这样才可以进行迭代 那内部是怎么实现的呢 这个时候会将对应的foreach语句转换为一个while循环 并且通过Move ...
- Pyspider上手
pyspider安装: pip3 install Pyspider 启动服务操作 1.打开cmd:输入 pyspider --help 回车,可以查看帮助信息,pyspider all ...
- React-Native之轮播组件looped-carousel的介绍与使用
React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...
- artTemplate之初印象
介绍 art-template 是JavaScript模板引擎,是一个简约.超快的模板引擎. 它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支 ...
- Spring-Boot Banner
下载Spring-Boot源码,目录结构spring-boot-2.1.0.M2\spring-boot-2.1.0.M2\spring-boot-project\spring-boot\src\ma ...