Palindrome. Again Palindrome
1354. Palindrome. Again Palindrome
Memory limit: 64 MB
Input
Output
Samples
input | output |
---|---|
No |
NoN |
OnLine |
OnLineniLnO |
AbabaAab |
AbabaAababA |
/**使用求回文串的算法:Manacher算法**/
/** 要添加最短的串,使得新形成的串是回文串,首先,要考虑原串的回文属性,寻找包含末尾的最大回文后缀串,但是,要注意,当原串全部回文的时候,需要找最大回文后缀,但不包括开头~最后,要注意,当原串长度为1时,直接复制一遍输出即可.**/
#include <iostream>
#include<cstdio>
#include<cstring>using namespace std;
#define maxn 50000char s[maxn],p[maxn];
int slen[maxn],len;void read()
{scanf("%s",s);len=strlen(s);for(int i=1; i<=len; i++)//Manacher的预处理{p[(i<<1)-1]='#';p[i<<1]=s[i-1];}p[0]='?';
}void Manacher()
{int j,k,maxid=0,id=0;for(int i=1; p[i]!='\0'; i++){if(maxid>i)slen[i]=min(maxid-i,slen[2*id-i]);//实质为动态规划elseslen[i]=1;while(p[i+slen[i]]==p[i-slen[i]])slen[i]++;if(slen[i]+i>maxid){maxid=slen[i]+i;id=i;}}
}int deal()
{int maxi=0;for(int i=strlen(p)-1; i>(strlen(p)+1)/2; i--)//寻找最长的回文后缀,注意中间的判断条件,如果是大于等于那就会找出包含开头的最大回文后缀,会导致错误{if(p[i+slen[i]]=='\0')maxi=i;}return maxi;
}int main()
{read();if(len==1)//原串长度为1时,需要特判{printf("%s%s",s,s);return 0;}Manacher();int j=deal();printf("%s",s);for(int i=j-slen[j]; i>=1; i--)//计算出要添加的那一段是从哪里开始的if(p[i]!='#')printf("%c",p[i]);printf("\n");return 0;
}
Palindrome. Again Palindrome相关推荐
- leetcode 1328. Break a Palindrome | 1328. 破坏回文串(贪心)
题目 https://leetcode.com/problems/break-a-palindrome/ 题解 分析所有可能情况,然后贪心. class Solution {public String ...
- 【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)
题干: Mike has a string s consisting of only lowercase English letters. He wants to change exactly one ...
- LeetCode算法入门- Palindrome Number-day2
LeetCode算法入门- Palindrome Number-day2 Palindrome Number Determine whether an integer is a palindrome. ...
- [LeetCode] 234. Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- All Palindrome
链接:https://ac.nowcoder.com/acm/contest/624/A 来源:牛客网 Ramen likes palindrome. A palindrome is a word, ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 234. Palindrome Linked List - Easy
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...
- Lintcode108 Palindrome Partitioning || solution 题解
[题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...
最新文章
- rust 官服指令_RUST 命令大全(包括服务器指令)
- Hough Transform 霍夫变换检测直线
- rdp连接工具_如何在Windows10中清除RDP连接历史记录?
- 深度强化学习在智能城市领域应用介绍
- hadoop2.6.0实践:002 检查伪分布式环境搭建
- java的CountDownLatch使用
- declspec(dllexport)与_declspec(dllimport) 异同
- 递归实现10进制转8进制,字符串数字互转,判断数组正逆向
- Android屏幕旋转时Activity不重新调用onCreate的方法
- Chromium版Edge体验——几个理由告诉你为什么卸载Chrome!
- windows环境下IDEA安装和配置和第一个IDEA项目运行加常用快捷键
- SonicWall 紧急提醒:EOL 设备正遭勒索攻击!
- Matplotlib可视化数据分析图表下(常用图表的绘制、折线图、柱形图、直方图、饼形图、散点图、面积图、热力图、箱形图、3D图表、绘制多个图表、双y轴可视化图表、颜色渐变图)
- 工程结算的23个问题及技巧
- mysql数据库接收不了中文乱码_mysql数据库 中文乱码
- 笔记本电脑光驱位置装固态硬盘流程
- jQuery 效果 - fadeIn() 方法
- Xavier法与何恺明法初始化权重的意义及原理
- Vagrant 介绍
- 使用BGP机房有以下优势:
热门文章
- 青云QingCloud Insight 2017: 云计算支撑未来商业图景
- 选择样式的日期时间js代码
- 用计算机进行计算ppt,《用计算器进行运算》课件.ppt
- 【sentinel】Sentinel的介绍与使用
- 超详细域名备案+阿里云服务器配置+小程序开发(简略)+前后端分离(简略)
- Linux系统下延时任务及定时任务
- Runtime.getRuntime().exec()操作脚本
- 自学前端第十五天:伪类伪元素和属性选择器和精灵图
- Win10家庭版如何正确关闭自动更新
- 【一些话】陈怡然教授:年底了,和学生说两句心里的鸡汤