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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<fstream>
using namespace std;
#define MAXN 1000003
typedef long long LL;
/*
最长回文串的长度
*/
char a[MAXN];
char s[MAXN*];
int r[MAXN*];
void Manacher(int len)
{
int p=;
s[p++] = '$';
s[p++] = '#';
for(int i=;i<len;i++)
{
s[p++] = a[i];
s[p++] = '#';
}
s[p] = ; int Mx = ,pos = ;
for(int i=;i<p;i++)
{
r[i] = (i<Mx)? min(Mx-i,r[*pos-i]):;
while(s[i+r[i]]==s[i-r[i]])
r[i]++;
if(i+r[i]>Mx)
{
Mx = i+r[i];
pos = i;
}
}
}
int main()
{
int cas = ;
while(scanf("%s",a),a[]!='E')
{
int l = strlen(a);
Manacher(l);
int ans = ;
for(int i=;i<*l+;i++)
{
ans = max(ans,r[i]-);
}
printf("Case %d: %d\n",cas++,ans);
}
}

最新文章

  1. MFC操作excel
  2. openlayers 3 简书
  3. 基于RulesEngine的业务规则实现
  4. Arrays.asList()使用注意点
  5. Android SDK 更新镜像服务器
  6. MySql数据类型(转)
  7. Struts2 Convention插件的使用(1)
  8. Qt将表格table保存为excel(odbc方式)
  9. android网址
  10. java代码收藏:获取HttpServletRequest中某一前缀的参数
  11. bzoj 2073 暴力
  12. 微信小程序-列表渲染多层嵌套循环
  13. linux安装配置zookeeper-3.4.10
  14. JSON数据写入和解析
  15. js 实现List
  16. JustOj 1929: 多输入输出练习1
  17. Spring4.0之四:Meta Annotation(元注解)
  18. 跟我一起学Python-day1(条件语句以及初识变量)
  19. redis实现秒杀demo
  20. python学习笔记——提取网页中的信息正则表达式re

热门文章

  1. 03、HelleBaiduMap
  2. PCB 使用第3方网站做为外链图片资源
  3. easyui -tree的详细讲解
  4. Educational Codeforces Round 24 题解
  5. Java保存错误日志信息
  6. 简单入门构建spark1.6.1源码环境
  7. fcc html5 css 练习3
  8. drupal-使用hook_preprocess_field在paragraph的accordion中添加自定义数据
  9. 在Yosemite中创建个人站点
  10. R包