POJ   3974

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<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
int n,p[N];
char s[N],str[N]; void kp()
{
int i;
int mx=;
int id;
///for(i=n;str[i]!=0;i++)
///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
for(i=;i<n;i++)
{
if(mx>i)
p[i]=min(p[*id-i],p[id]+id-i);
else
p[i]=;
for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
} void init()
{
str[]='$';
str[]='#';
for(int i=;i<n;i++)
{
str[i*+]=s[i];
str[i*+]='#';
}
n=n*+;
s[n]=;
} int main()
{
int Case=;
while(scanf("%s",s)!=EOF)
{
if(s[]=='E'&&s[]=='N'&&s[]=='D')
break;
n=strlen(s);
init();
kp();
int ans=;
for(int i=;i<n;i++)
if(p[i]>ans)
ans=p[i]; printf("Case %d: %d\n",Case++,ans-);
}
return ;
}

回文串---Palindrome的更多相关文章

  1. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  3. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  4. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  9. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

随机推荐

  1. ViewHolder模式超简洁写法

    ViewHolder是什么就不解释了.大家通常怎么写ViewHolder呢? ViewHolder holder = null; if (convertView == null) { convertV ...

  2. Cocos2d-JS项目之二:studio基础控件的使用

    在studio里把几个基础控件往场景文件一拖,然后导出json格式的资源文件 逻辑代码如下: var HelloWorldLayer = cc.Layer.extend({ sprite:null, ...

  3. elasticsearch + hive环境搭建

    一.环境介绍: elasticsearch:2.3.1 hive:0.12 二.环境搭建 2.1 首先获取elasticsearc-hadoop的jar包 链接地址:http://jcenter.bi ...

  4. saiku、mondrian前奏之——立方体、维度、Schema的基本概念

    以前介绍了几个基本工具:saiku 和 Schema Workbench,算是入门级别的了解多维报表,如果要继续深入,需要深入了解如下几个概念: 1.OLAP 联机分析处理,和他对应的是OLTP(联机 ...

  5. 在Linux 应用层 基于i2c-dev.h 实现i2c读写

    /* i2c-dev.h - i2c-bus driver, char device interface Copyright (C) 1995-97 Simon G. Vogl Copyright ( ...

  6. c++ 职责链模式(Chain of Responsibility)

    职 责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 理它为止.其思想很简单,考虑员工要求加薪.公司的管 ...

  7. python 字符串翻转

    通过步进反转[::-1] ]##[::-1]通过步进反转print b

  8. 自定义组件之MoreListView

    前言 本文针对自定义组件进行一些分析.还是那句老话“授之于鱼不如授之以渔”.今天要讲的是一个自定义的可以分页的ListView. 网上都讲了些ListView分页的方法,那么为什么我在这里还需要自己写 ...

  9. Heavy Transportation(最短路 + dp)

    Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64 ...

  10. 【转】ContextMenuStrip菜单应用

    测试可用的代码: #region 右键快捷菜单单击事件 private void contextMenuStrip1_ItemClick(object sender, EventArgs e) { T ...