Palindrome
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 12616   Accepted: 4769

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

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 ;
}

最新文章

  1. MVC+EF6+Oracle,提示ORA-01918: user &#39;***&#39; does not exist
  2. asp.net读取execl模板并填充数据,关闭进程
  3. 选择App开发外包时,你该了解哪些法律常识?
  4. 控件包含代码块(即 &lt;% ... %&gt;),因此无法修改控件集合
  5. 简易版CSS3 Tab菜单 实用的Tab切换
  6. android——彻底关闭——应用程序
  7. Week11(11月18日)
  8. poj 1011 Sticks ,剪枝神题
  9. greenDAO简介
  10. 【Linux】Apache Httpd 服务管理
  11. APNS IOS 消息推送处理失效的Token
  12. 使用mpvue开发小程序教程(二)
  13. ES7
  14. IoC之Ninject
  15. Git 学习笔记--删除错误提交的commit
  16. DevExpress ASP.NET v18.2新功能详解(一)
  17. External Input Counter and External interrupt
  18. HDU 2476 String painter(区间DP)
  19. L207
  20. 《JavaScript》高级程序设计第7章 函数表达式

热门文章

  1. [TYVJ2340] 送礼物 - 双向搜索
  2. nginx::升级到最新nginx
  3. PHP实现阿里云OSS文件上传(支持批量)
  4. Fiddler抓包和工作原理
  5. JVM(5) 类加载机制
  6. hash算法的应用
  7. 爬虫链接mongodb 以及多线程多进程的操作
  8. foreach数组并直接改变数组内容
  9. linux C进程常用操作
  10. &lt;script&gt;属性async和defer的区别