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. Android批量图片载入经典系列——afinal框架实现图片的异步缓存载入
  2. ef mysql 数据模型,EF Core使用CodeFirst在MySql中创建新数据库以及已有的Mysql数据库如何使用DB First生成域模型...
  3. wifi rssi 计算 距离_PPT|交通未来第3期 基于实时WIFI蓝牙检测数据的交通目标定位及出行模式甄别技术...
  4. 关于Android studio3.0的坑之butterknife 7.0.1(低版本)
  5. XenApp_XenDesktop_7.6实战篇之二十一:Linux Virtual Desktop 1.0(下篇)
  6. Java NIO网络编程之Selector
  7. Android基础知识点的整理3
  8. 如何制作一款HTML5 RPG游戏引擎——第五篇,人物人物特效
  9. os.path.join()函数
  10. Android中的权限请求
  11. ffmpeg h264 h265 视频格式操作
  12. php 快递打印设置,让ecshop批量打印快递单修改方法
  13. 垃圾邮件识别(一):用机器学习做中文邮件内容分类
  14. 经纬财富:沧州散户炒白银必读
  15. 80后小学计算机课上的游戏,80后最值得回味的经典课间游戏
  16. 代码在线执行工具(PHP,Java,C++ 等)
  17. 【附源码】计算机毕业设计java-志愿者管理系统设计与实现
  18. Hive建外表操作以及其它修改表操作 hive外表与内表区别
  19. 戴尔游匣G15怎么样?真实体验不翻车
  20. C++ - STL标准库

热门文章

  1. COI实验室技能:MATLAB控制PCO相机
  2. 关于国际化时报org.springframework.context.NoSuchMessageException错
  3. 腾达f3虚拟服务器怎么设置,腾达(Tenda)F3路由器用手机怎么设置?
  4. 在小县城适合做什么生意?
  5. 阿里云服务器ecs的功能和优势
  6. 《计算传播学导论》读书笔记:第七章 数据新闻
  7. 优惠券系统-第三章-活动中心
  8. 进击的PyTorch,和它背后的开源领袖
  9. mod在计算机导论中是什么意思,计算机导论ppt课件
  10. 开发工具快速下载地址