1354. Palindrome. Again Palindrome

Time limit: 1.0 second
Memory limit: 64 MB
word is the nonempty sequence of symbols  a 1 a 2… an. A  palindrome is the word  a 1 a 2… an that is read from the left to the right and from the right to the left the same way ( a 1 a 2… an =  ana n−1… a 1). If S 1 =  a 1 a 2… an and  S 2 =  b 1 b 2… bm, then  S 1 S 2 =  a 1 a 2… anb 1 b 2… bm. The input contains some word  S 1. You are to find a nonempty word  S 2 of the minimal length that  S 1 S 2 is a palindrome.

Input

The first input line contains  S 1 (it may consist only of the Latin letters). It’s guaranteed that the length of  S 1 doesn’t exceed 10000 symbols.

Output

S 1 S 2.

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相关推荐

  1. leetcode 1328. Break a Palindrome | 1328. 破坏回文串(贪心)

    题目 https://leetcode.com/problems/break-a-palindrome/ 题解 分析所有可能情况,然后贪心. class Solution {public String ...

  2. 【CodeForces - 798A】Mike and palindrome (回文串,水题,字符串问题)

    题干: Mike has a string s consisting of only lowercase English letters. He wants to change exactly one ...

  3. LeetCode算法入门- Palindrome Number-day2

    LeetCode算法入门- Palindrome Number-day2 Palindrome Number Determine whether an integer is a palindrome. ...

  4. [LeetCode] 234. Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. All Palindrome

    链接:https://ac.nowcoder.com/acm/contest/624/A 来源:牛客网 Ramen likes palindrome. A palindrome is a word, ...

  6. 【LeetCode】Palindrome Partitioning 解题报告

    [题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  7. 234. Palindrome Linked List - Easy

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  8. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  9. Lintcode108 Palindrome Partitioning || solution 题解

    [题目描述] Given a strings, cutsinto some substrings such that every substring is a palindrome. Return t ...

最新文章

  1. rust 官服指令_RUST 命令大全(包括服务器指令)
  2. Hough Transform 霍夫变换检测直线
  3. rdp连接工具_如何在Windows10中清除RDP连接历史记录?
  4. 深度强化学习在智能城市领域应用介绍
  5. hadoop2.6.0实践:002 检查伪分布式环境搭建
  6. java的CountDownLatch使用
  7. declspec(dllexport)与_declspec(dllimport) 异同
  8. 递归实现10进制转8进制,字符串数字互转,判断数组正逆向
  9. Android屏幕旋转时Activity不重新调用onCreate的方法
  10. Chromium版Edge体验——几个理由告诉你为什么卸载Chrome!
  11. windows环境下IDEA安装和配置和第一个IDEA项目运行加常用快捷键
  12. SonicWall 紧急提醒:EOL 设备正遭勒索攻击!
  13. Matplotlib可视化数据分析图表下(常用图表的绘制、折线图、柱形图、直方图、饼形图、散点图、面积图、热力图、箱形图、3D图表、绘制多个图表、双y轴可视化图表、颜色渐变图)
  14. 工程结算的23个问题及技巧
  15. mysql数据库接收不了中文乱码_mysql数据库 中文乱码
  16. 笔记本电脑光驱位置装固态硬盘流程
  17. jQuery 效果 - fadeIn() 方法
  18. Xavier法与何恺明法初始化权重的意义及原理
  19. Vagrant 介绍
  20. 使用BGP机房有以下优势:

热门文章

  1. 青云QingCloud Insight 2017: 云计算支撑未来商业图景
  2. 选择样式的日期时间js代码
  3. 用计算机进行计算ppt,《用计算器进行运算》课件.ppt
  4. 【sentinel】Sentinel的介绍与使用
  5. 超详细域名备案+阿里云服务器配置+小程序开发(简略)+前后端分离(简略)
  6. Linux系统下延时任务及定时任务
  7. Runtime.getRuntime().exec()操作脚本
  8. 自学前端第十五天:伪类伪元素和属性选择器和精灵图
  9. Win10家庭版如何正确关闭自动更新
  10. 【一些话】陈怡然教授:年底了,和学生说两句心里的鸡汤