poj 3974 Palindrome (manacher)
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 12616 | Accepted: 4769 |
Description
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
Output
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
C/C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 3e6 + ; char my_str[MAX], my_temp[MAX];
int my_len1, my_len2, my_ans[MAX]; void manacher()
{
int my_right = , my_mid = ;
for (int i = ; i < my_len2; ++ i)
{
if (my_right > i) my_ans[i] = min(my_right - i, my_ans[(my_mid << ) - i]);
else my_ans[i] = ;
while (my_temp[i - my_ans[i]] == my_temp[i + my_ans[i]]) my_ans[i] ++;
if (my_right < my_ans[i] + i)
{
my_right = my_ans[i] + i;
my_mid = i;
}
}
} int main()
{
int t = ;
while (scanf("%s", my_str), strcmp(my_str, "END"))
{
my_len1 = strlen(my_str);
my_temp[] = '$';
my_temp[] = '#';
for (int i = , j = ; i < my_len1; ++ i, j += )
{
my_temp[j] = my_str[i];
my_temp[j + ] = '#';
} my_len2 = (my_len1 << ) + ;
my_temp[my_len2] = '*'; manacher();
int temp = ;
for (int i = ; i < my_len2; ++ i)
if (temp < my_ans[i]) temp = my_ans[i];
printf("Case %d: %d\n", t ++, temp - );
} return ;
}
最新文章
- MVC+EF6+Oracle,提示ORA-01918: user &#39;***&#39; does not exist
- asp.net读取execl模板并填充数据,关闭进程
- 选择App开发外包时,你该了解哪些法律常识?
- 控件包含代码块(即 <;% ... %>;),因此无法修改控件集合
- 简易版CSS3 Tab菜单 实用的Tab切换
- android——彻底关闭——应用程序
- Week11(11月18日)
- poj 1011 Sticks ,剪枝神题
- greenDAO简介
- 【Linux】Apache Httpd 服务管理
- APNS IOS 消息推送处理失效的Token
- 使用mpvue开发小程序教程(二)
- ES7
- IoC之Ninject
- Git 学习笔记--删除错误提交的commit
- DevExpress ASP.NET v18.2新功能详解(一)
- External Input Counter and External interrupt
- HDU 2476 String painter(区间DP)
- L207
- 《JavaScript》高级程序设计第7章 函数表达式