字符串-POJ3974-Palindrome
Palindrome
Time Limit: 15000MS Memory Limit: 65536K
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
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
解题心得:
- 就是叫你去求一个字符串中的最长回文串,暴力肯定是会超时的,可以去看看manacher算法,这个算法不是很难。manacher详解。这就是一个manacher算法的模板题,看懂了就会做了。
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e6+100;
char s[maxn<<1],ch[maxn<<1];
int rl[maxn<<1];
int get_ch(int len)
{
int ch_len = 0;
for(int i=0; i<len; i++)
{
ch[ch_len++] = '#';
ch[ch_len++] = s[i];
}
ch[ch_len++] = '#';//注意要把最后一个#给加上去
return ch_len;
}
int manacher(int ch_len)
{
int max_right = 0,pos = 0,Max = 0;;
for(int i=0;i<ch_len;i++)
{
rl[i] = 1;
if(i < max_right)
rl[i] = min(rl[(pos<<1)-i],max_right-i);
while(i+rl[i]<ch_len && i-rl[i]>=0 && ch[i+rl[i]] == ch[i-rl[i]])
rl[i]++;
//max_right、pos更新
if(i + rl[i]-1 > max_right)
{
max_right = i+rl[i]-1;
pos = i;
}
//记录最大的那一个回文串
if(rl[i] > Max)
Max = rl[i];
}
return Max-1;//记得要减去1
}
int main()
{
int t = 1;
while(scanf("%s",s) != EOF)
{
if(s[0] == 'E' && s[1] == 'N' && s[2] == 'D' && strlen(s) == 3)
break;
int len = strlen(s);
int len_ch = get_ch(len);//将元字符串改写
int ans = manacher(len_ch);
printf("Case %d: %d\n",t++,ans);
}
return 0;
}
字符串-POJ3974-Palindrome的更多相关文章
- [Swift]LeetCode680. 验证回文字符串 Ⅱ | Valid Palindrome II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- poj3974 Palindrome【回文】【Hash】【二分】
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 13157 Accepted: 5028 Desc ...
- LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
- POJ----(3974 )Palindrome [最长回文串]
Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 5121 Accepted: 1834 Description Andy ...
- POJ3974 Palindrome (manacher算法)
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 【后缀数组】【线段树】poj3974 Palindrome
考虑奇数长度的回文,对于字符串上的每个位置i,如果知道从i开始的后缀和到i为止的前缀反转后的字符串的lcp长度的话,也就知道了以第i个字符为对称中心的最长回文的长度了.因此,我们用在S中不会出现的字符 ...
- [POJ3974]Palindrome(后缀数组 || manacher)
传送门 求一个串的最长回文子串的长度 1.后缀数组 把这个串反转后接到原串的后面,中间连一个没有出现过的字符. 然后求这个新字符串的某两个后缀的公共前缀的最大值即可. ——代码 #include &l ...
- [poj3974] Palindrome 解题报告 (hash\manacher)
题目链接:http://poj.org/problem?id=3974 题目: 多组询问,每组给出一个字符串,求该字符串最长回文串的长度 数据范围支持$O(nlog n)$ 解法一: 二分+hash ...
- poj3974 Palindrome(Manacher最长回文)
之前用字符串hash+二分过了,今天刚看了manacher拿来试一试. 这manacher也快太多了%%% #include <iostream> #include <cstring ...
随机推荐
- docker jetty启动时报错 failed setting default capabilities.
docker 容器中jetty启动fail ,查看/var/log/jetty中的日志发现具体错误信息如下: failed setting default capabilities.set_caps( ...
- CodeForces 731C C - Socks 并查集
Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m ...
- MapReduce项目之气温统计
在本博文,我们要学习一个挖掘气象数据的程序.气象数据是通过分布在美国全国各地区的很多气象传感器每隔一小时进行收集,这些数据是半结构化数据且是按照记录方式存储的,因此非常适合使用 MapReduce 程 ...
- js获取ISO8601规范时间
var d = new Date(); d.setHours(d.getHours(), d.getMinutes() - d.getTimezoneOffset()); console.log(d. ...
- 关于小米手机USB传输稍大点的文件老中断的问题解决方法!
关于小米手机USB传输稍大点的文件老中断的问题解决方法! 这是一个很痛苦的事情,当你传输大文件的时候,传输到一半就会莫名其妙的中断,拔插数据线很多次以后,好不容易没准可以成功传输一次. 后来使用了36 ...
- UI2_异步下载
// AppDelegate.m // UI2_异步下载 // // Created by zhangxueming on 15/7/17. // Copyright (c) 2015年 zhangx ...
- iOS Runtime常用方法整理
关于runtime的学习网上有很多博客,在学习之前也查过很多资料,觉得南峰子老师博客中对 runtime 的讲解挺详细的,博客地址:http://southpeak.github.io/categor ...
- 初学Vue.js(2.x版本)
首先肯定是打开官网查看文档了,没想到我太高估了自己,看的我头晕也不知道到底说了个啥.没办法,只能另寻他法,好在有菜鸟教程.然而我还是想多了,不稀饭一点点看下去,只想快点明白它到底说了个啥.嗯,找来找去 ...
- JDBC事务--软件开发三层架构--ThreadLocal
JDBC事务--软件开发三层架构--ThreadLocal 一.JDBC事务 1.概述: 事务是指逻辑上的一组操作!这一组操作,通常认为是一个整体,不可拆分! 特点:同生共死;事务内的这一组操作要么全 ...
- Jmeter监控内存及CPU等
在进行性能测试时需要查看内存和CPU等信息来判断系统瓶颈,关于CPU和内存的监控,goole开发了一款专门的jmeter插件,弥补了Jmeter这方面的不足,下面来介绍这款插件-JmeterPlugi ...