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 ...
随机推荐
- SQL Server 导入 MDF LDF文件
EXEC sp_attach_db @dbname = 'OA', @filename1 = 'C:\OA.mdf', @filename2 = 'C:\OA_log.ldf'
- Echarts3 使用教程
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 【转修正】sql server行版本控制的隔离级别
在SQL Server标准的已提交读(READ COMMITTED)隔离级别下,一个读操作会和一个写操作相互阻塞.未提交读(READ UNCOMMITTED)虽然不会有这种阻塞,但是读操作可能会读到脏 ...
- c# 相对路径的一些资料
1.获取和设置当前目录的完全限定路径. string str = System.Environment.CurrentDirectory; Result: C:\xxx\xxx 2.获取启动了应用程序 ...
- UNIX操作系统和Linux操作系统介绍
UNIX操作系统(尤尼斯),是一个强大的多用户.多任务操作系统,支持多种处理器架构,按照操作系统的分类,属于分时操作系统,最早由KenThompson.DennisRitchie和DouglasMcI ...
- 设置irb和ri
设置irb和ri 输入ruby -v查看是否能够显示ruby版本 现在可以 配置irb了. irb是交互式Ruby 的命令行工具,即输入一句就立即执行并给出结果. 默认的irb不够强大,现在给它配置一 ...
- Maps for Developers
苹果官方文档: Give your apps a sense of place with maps and location information. Present maps with custom ...
- Objective-C语言Foundation框架
Mac OS X开发会使用Cocoa框架,它是一种支持应用程序提供丰富用户体验的框架,它实际上由:Foundation和Application Kit(AppKit)框架组成.iOS开发,会使用Coc ...
- 完整成功配置wamp server小记
首先安装最新版本的wamp server,这是必须的! 配置默认”www目录”的路径(可选):下面以改为D:\site为例.打开wamp\scripts\config.inc.php第47行,$www ...
- Python—Socket
Socket模块 socket通常也称作"套接字",用于描述IP地址和端口,是特定网络协议如TCP/IP.UDP/IP套件对网络应用程序提供者提供的当前可移植标准的对象, 用来连接 ...