转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

求最长回文子串。

http://acm.timus.ru/problem.aspx?space=1&num=1297

Manacher模板题,复杂度O(n),做这题纯属是为了验一下自己写的模板是否正确。

当然这题也可以用后缀数组来搞

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
char str[MAXN],s[MAXN];
int p[MAXN];
int n;
void Manacher(){
n=strlen(s);
str[]='$';
str[]='#';
for(int i=;i<n;i++)str[*i+]=s[i],str[*i+]='#';
n=n*+;
str[n]=;
int mx=,id;
for(int i=;i<n;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);
else p[i]=;
for(;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx)mx=p[i]+i,id=i;
}
} int main()
{
ios::sync_with_stdio(false);
while(scanf("%s",s)!=EOF){
Manacher();
int ans=;
int id;
int maxx=;
for(int i=;i<n;i++){
if(p[i]>maxx)id=i,maxx=p[i];
}
//cout<<maxx<<endl;
p[id]--;
for(int i=id-p[id];i<=id+p[id];i++){
if(str[i]=='#')continue;
printf("%c",str[i]);
}
puts("");
}
return ;
}

ural 1297 Palindrome(Manacher模板题)的更多相关文章

  1. HDU 3068 最长回文( Manacher模板题 )

    链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...

  2. Manacher Ural 1297 Palindrome

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  3. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  4. URAL 1297 Palindrome(Manacher)

    The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...

  5. URAL 1297 Palindrome 后缀数组

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

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

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

  7. 最长回文 HDU - 3068 manacher 模板题

    题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...

  8. HDU 3068 最长回文(manacher模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...

  9. URAL - 1297 Palindrome —— 后缀数组 最长回文子串

    题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...

随机推荐

  1. MAC中通过gem命令安装compass

    在MAC中通过gem命令安装compass时会出异常,原因是compass版本更新了,一些运行时所用到的依赖软件的版本没能得到更新,故而出现错误.例如,用以下命令安装compass: $ gem in ...

  2. apache用户认证、默认主机、301跳转

    我更正论坛一个同学帖子(今天坑我一下午):原文http://www.apelearn.com/bbs/foru ... 3%BB%A7%C8%CF%D6%A4 apache用户认证.默认主机.301跳 ...

  3. ET 与RETI 基于51单片机中断跳出指令“RETI”浅议

    最近在基于51单片机编程的过程中出现了个很奇怪的问题“程序执行中在寄存器EA=1,ET0=1,TR0=1条件下,单TF0=1时并没有执行中断”.在有过单片机中断编程经历者都知道当EA=1,ET0=1的 ...

  4. 《Programming WPF》翻译 第7章 5.可视化层编程

    原文:<Programming WPF>翻译 第7章 5.可视化层编程 形状元素能提供一种便利的方式与图形一起工作,在一些情形中,添加表示绘图的元素到UI树中,可能是比它的价值更加麻烦.你 ...

  5. (转载)Setup Factory 会话变量

    本文转自http://www.cnblogs.com/lzjsky/archive/2010/11/18/1880440.html 方便今后查询 Session variables are speci ...

  6. windows下编译firefox

    可以自己定制下.估计很简单..... 官方文档扫一遍: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_I ...

  7. shell下有操作json对象的库

    http://kernelpanic.im/blog/2012/03/08/shell-manipulate-json/ Json.org推荐了两个:Jshon和JSON.sh 其中JSON.sh是完 ...

  8. QQ聊天界面的布局和设计(IOS篇)-第一季

    我写的源文件整个工程会再第二季中发上来~,存在百度网盘, 感兴趣的童鞋, 可以关注我的博客更新,到时自己去下载~.喵~~~ QQChat Layout - 第一季 一.准备工作 1.将假数据messa ...

  9. 修改Fedora 20 启动项

    在Fedora 20里面,Fedora 使用了systemd作为系统与服务的管理工具,这个守护进程是系统开机后第一个开启的进程,pid 为1.systemd扮演着初始化系统的角色,主要用于开启与维护系 ...

  10. [TI DLP Buglist]data type error in illum_EnableIllumination function

    I am debuging my code today, I find when my code is running, it's stop at illum_EnableIllumination() ...