Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, “Can you propose an efficient algorithm to find the length of the largest palindrome in a string?”

A string is said to be a palindrome if it reads the same both forwards and backwards, for example “madam” is a palindrome while “acm” is not.

The students recognized that this is a classical problem but couldn’t come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said “Okay, I’ve a better algorithm” and before he starts to explain his idea he stopped for a moment and then said “Well, I’ve an even better algorithm!”.

If you think you know Andy’s final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string “END” (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6

#include <iostream>
#include <cstring>
#include <cstdio>using namespace std;
const int Mod = 1e9 + 7;
const int Maxn = 1000010;
char s[Maxn << 1],tmp[Maxn << 1];
int Len[Maxn << 1];
int Trans(char *st) {int i,len = strlen(st);tmp[0] = '@';for(i=1; i<=len*2; i+=2) {tmp[i] = '#';tmp[i + 1] = st[i >> 1];}tmp[len << 1 | 1] = '#';tmp[(len << 1 | 1) + 1] = '$';tmp[len * 2 + 3] = 0;return len << 1 | 1;
}int Manacher(char * st,int len) {int mx = 0,Ans = 0,id = 0;for(int i=1; i<=len; i++) {if(mx > i)Len[i] = min(mx - i,Len[2 * id - i]);else Len[i] = 1;while(st[i - Len[i]] == st[i + Len[i]]) Len[i]++;if(Len[i] + i > mx) {mx = Len[i] + i;id = i;}Ans = max(Ans,Len[i]);}return Ans - 1;
}int main(int agrc,char* agrv[]) {int kase = 0,Ans;while(scanf("%s",s ) == 1) {if(strcmp(s,"END") == 0) break;int len = Trans(s);Ans = Manacher(tmp,len);printf("Case %d: %d\n",++kase,Ans);}return 0;
}

https://blog.csdn.net/xiaoxiede_wo/article/details/79817998

A - Palindrome相关推荐

  1. 【LeetCode】Palindrome Partitioning 解题报告

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

  2. 234. Palindrome Linked List - Easy

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

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

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

  4. Lintcode108 Palindrome Partitioning || solution 题解

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

  5. 数位DP 回文序列 POJ-3280 Cheapest Palindrome

    Cheapest Palindrome [ POJ - 3280 ] 题目大意: 给定字符串s,长度为m,由n个小写字母组成.在s的任意位置增删字母,把它变成回文串,增删特定字母的花费不同,求最小花费 ...

  6. Determine whether an integer is a palindrome. Do this without extra space.

    看到这个题目的时候,首先不认识 Determine这个单词.英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思. 问题是 回文是个什么东西,官方解释: A ...

  7. [leetcode] Palindrome Number

    2019独角兽企业重金招聘Python工程师标准>>> Determine whether an integer is a palindrome. Do this without e ...

  8. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

  9. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  10. 409 Longest Palindrome

    Longest Palindrome 这题我完全想错了. 求最长回文.~~统计个个字母出现次数.奇数的保留最大的那个.偶数全保留.~~这个思路完全错了. 因为奇数中成对的数字也是可以拿来用的. 正确的 ...

最新文章

  1. 怎么在手机上下载python模块_python下载模块然后怎么安装
  2. Mac下的比较器工具DeltaWalker的试用期延长法
  3. POI 导出excel带小数点的数字格式显示不对解决方法
  4. 创建应用服务器连接客户端
  5. 使用@Order调整配置类加载顺序
  6. 一、uniapp项目(封装异步请求、moment.js时间处理、封装手势滑动组件、下载图片到本地)
  7. TPLinker 联合抽取 实体链接方式+源码分析
  8. SQL 2005 新功能
  9. 年轻人“颜值消费”崛起 分期乐联手屈臣氏发力95后美妆护理市场
  10. 直观理解:为什么A为 n 阶满秩方阵时,Ax=0 只有零解?
  11. VS Code 必知必会的 20 个快捷键!
  12. 视觉SLAM十四讲_4-相机模型和非线性优化
  13. python序列类型唯一的映射类型_什么是python中唯一的映射类型
  14. Linux中vi命令详解
  15. PDF怎么裁剪页面,PDF裁剪页面的方法
  16. 使用win10自带的计算器,计算对数log
  17. 广电linux机顶盒装软件,电视盒子如何安装第三方软件(四种参考方法)
  18. Python爬虫实战——反爬策略之代理IP【无忧代理】
  19. 谈谈 Ops(一):我的运维经历
  20. 每日一题-917. 仅仅反转字母_Python

热门文章

  1. Python openjudge 练习025:正常血压
  2. 解决国内 github.com 打不开的最最最准确方法
  3. 美的、九阳、苏泊尔“变粉”,女性小家电成下一片“蓝海”?
  4. Android/Linux 子系统Graphics图形栈入门普法介绍
  5. 国内的镜像源访问地址
  6. 韩国计算机硕士累吗,在韩国读研究生有多心累...
  7. 收集的常用的CTF学习资源网站
  8. 脑电EEG常用的特征
  9. 关于pyecharts各个全局配置项设置示例——个人内容整理与分享
  10. 多机器协作工具 之 远程桌面(WinSvr2012-Ubu1604): tigervncserver-1.6.80-4 + mstsc