POJ----(3974 )Palindrome [最长回文串]
Description 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 Sample Output Case 1: 13 Source |
关于manacher算法..........()
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define maxn 1000010
char ss[maxn];
char str[maxn << 1L];
int var[maxn << 1L]; int manacher() { int len = , mx_l = , pos = ;
str[] = '$';
str[] = '#';
for (int i = ; ss[i] != '\0'; i++) {
str[len++] = ss[i];
str[len++] = '#';
}
str[len] = '\0';
for (int i = ; i<len; i++) {
var[i] = mx_l>i ? std::min(var[ * pos - i], mx_l - i) : ;
while (str[i + var[i]] == str[i - var[i]])++var[i];
if (mx_l<i + var[i]) {
mx_l = i + var[i];
pos = i;
}
}
return len;
} int main() { int cnt = , len = , res = ;
while (scanf("%s", ss) != EOF) { if (ss[] == 'E') break;
len = manacher();
res = ;
for (int i = ; i<len; i++)
res =std::max(res, var[i]);
memset(str, , sizeof(char)*len);
printf("Case %d: %d\n", cnt++, res-);
}
return ;
}
POJ----(3974 )Palindrome [最长回文串]的更多相关文章
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- Longest Palindrome 最长回文串问题
1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
- 409 Longest Palindrome 最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...
- LeetCode409Longest Palindrome最长回文串
给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...
- POJ 1159 Palindrome(字符串变回文:LCS)
POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...
- Java实现 LeetCode 409 最长回文串
409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- ACM题目————最长回文串
Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组cas ...
随机推荐
- mysql qps tps计算
Information from web QPS (Query per second) (每秒查询量)TPS(Transaction per second) (每秒事务量,如果是InnoDB会显示,没 ...
- 导入charts开源库到工程里面
http://blog.csdn.net/zww1984774346/article/details/50608338 http://blog.csdn.net/zww1984774346/artic ...
- 移动端html页面布局
记录一下: ---------- 如果设计稿尺寸为 : 96px: 1. rem html{ font-size:62.5%; } 的情况下 : 4.8rem: 2. rem html{ font-s ...
- LaTeX学习教程
本来我对LaTeX不是看好的,毕竟都是命令格式的.觉得有word就足够啦word可视化操作方便快捷. 但是由于要写论文等,在导师要求下潜心学习一下,不知不觉间被LaTeX的强大功能所吸引.现在很多出版 ...
- java经典算法40题(21-40)
[程序21] 题目:求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘. public class Ex21 { static long sum = 0; s ...
- python windows安装
一.下载并安装 下载地址http://www.python.org/download/ 安装 二.配置环境变量 配置python环境变量以便后面安装插件.D:\Program Files\Python ...
- asp批量查询
在做asp查询时候 借鉴的一些代码 留个纪念....... <!-- #include file="conn.asp"--> <html> <head ...
- imail 删除历史邮件命令
删除旧的邮件(immsgexp.exe)Immsgexp.exe 可以让管理员删除指定天数的旧的邮件.基本语法 immsgexp -t startdirectory -d #of_days_to_sa ...
- 剑指offer——变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 问题分析 由于每次跳的阶数不确定,没有一个固定的规律,但是可以了解的是后一次跳 ...
- LeetCode----202. Happy Number(Java)
package isHappy202; /* * Write an algorithm to determine if a number is "happy". A happy n ...