Palindrome
Time Limit: 15000MS   Memory Limit: 65536K
Total Submissions: 3280   Accepted: 1188

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
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[];
int p[],t;
int fun()
{
int i,r=,c=,max=;
p[]=;
for(i=;i<t;i++)
{
p[i]=r>i?(r-i<p[(c<<)-i]?r-i:p[(c<<)-i]):;
while(a[i+p[i]+]==a[i-p[i]-])p[i]++;
max=max>p[i]?max:p[i];
if(p[i]+i>r)
{
r=p[i]+i;
c=i;
}
}
return max;
}
int main()
{
char x;
int tt=;
while()
{
t=;
a[t++]='#';
while(x=getchar())
{
if(x=='\n')break;
a[t++]='&';
a[t++]=x;
}
a[t++]='&';
if(a[]=='E')break;
printf("Case %d: %d\n",tt++,fun());
}
}

Palindrome poj3974的更多相关文章

  1. [poj3974]Palindrome_Manacher

    Palindrome poj-3974 题目大意:求字符串的最长回文子串. 注释:$1\le strlen(s) \le 10^6$. 想法:介绍一种字符串算法——Manacher.求以每一个字符和字 ...

  2. 【学习笔记】字符串—马拉车(Manacher)

    [学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...

  3. POJ----(3974 )Palindrome [最长回文串]

    Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 1834 Description Andy ...

  4. Palindrome(poj3974)(manacher算法)

    http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...

  5. POJ3974 Palindrome

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. poj3974 Palindrome【回文】【Hash】【二分】

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 13157   Accepted: 5028 Desc ...

  7. POJ3974:Palindrome(Manacher模板)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 14021   Accepted: 5374 题目链接 ...

  8. POJ3974 Palindrome (manacher算法)

    题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...

  9. POJ--3974 Palindrome(回文串,hash)

    链接:点击这里 #include<iostream> #include<algorithm> #include<stdio.h> #include<cstri ...

随机推荐

  1. android TranslateAnimation 顶部segment分段移动动画

    这里实现的功能是从主页布局的fragment点击跳转到一个acitivity,然后顶部是一个切换的segment顶部是一个listview,点击segment分段让listview加载不同的内容.我这 ...

  2. 删除git项目

    主页,点击右上头像-->your profile-->Popular repositories ,看到你要删除的项目,点击进去 --> 上面 选择settings-->到页面最 ...

  3. Java GC 日志详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt105 java GC日志可以通过 +PrintGCDetails开启 以Pa ...

  4. iBatis的一个问题

    写了一段查询语句,条件中有一条alarmtype<>'1004'这样的条件,原来是这样写的 <![CATA[ and alarmtype<>'1004']]> 然后 ...

  5. Project 4:Longest Ordered Subsequence

    Problem description A numeric sequence of ai is ordered if a1 < a2 < - < aN. Let the subseq ...

  6. 团队作业8——第二次项目冲刺(Beta阶段)Day2--5.19

    1.展开站立式会议: 会议内容:①汇报一天大家任务的完成成果. ②新成员对学到的知识进行交流,并向老成员提问. ③根据大家的进度制定新一轮的任务计划. 2.每个人的工作分配 队员 今日任务 明日任务 ...

  7. 201521044091《java程序设计》第四次总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.11.2 使用常规方法总结其他上课内容 Object是所有对象类的父类,而toString方法只有可以转换为字符串的类型对象才可 ...

  8. 201521123069 《Java程序设计》 第3周学习总结

    1. 本章学习总结 如果看不清楚可点击类与对象 2. 书面作业 Q1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private sta ...

  9. 201521123093 java 第十二周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  10. 201521123042 《Java程序设计》 第10周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 上周总结了异常,这周主要总结多线程. 2. 书面作业 本次PTA作业题集异常.多线程 1.finally 题 ...