Description

Given a string S, which consists of lowercase characters, you need to find the longest palindromic sub-string.

A sub-string of a string S is another string S' that occurs "in" S. For example, "abst" is a sub-string of "abaabsta". A palindrome is a sequence of characters which reads the same backward as forward.

Input

There are several test cases.

Each test case consists of one line with a single string S (1 ≤ |S | ≤ 50).

Output

For each test case, output the length of the longest palindromic sub-string.

Sample Input

sasadasa
bxabx
zhuyuan

Sample Output

7
1
3

分析:

题意:给出一个字符串,求这个字符串中的最长回文串的长度。

首先要一个一个看字符串中的字符:

对于第 i 个字符 str[i] ,假如回文子串是奇数个字符,那么考虑以i为中心,同时向左向右扩展,直到发现对称位置字符不相等,假如此时共扫过x个字符,则当前回文串长度为2*x+1。加上的1就是加上i本身。如下图所示:

由第 i 个字符a[i]构成回文偶数串。i在最接近对称轴的左侧。

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#define INF 999999
using namespace std;
int main()
{
int maxlen,i,j,len; ///最长对称子串长度。
char str[1003];
while(gets(str))
{
maxlen = 0;
len = strlen(str);
for(i = 0; i < len; i++)
{
///考虑是i是奇数串的中心,以i为中心,同时往左往右扩展
for(j = 0; i-j>=0&&i+j<=len; j++)
{
if(str[i-j]!=str[i+j])
break;
if(2*j+1>maxlen)
maxlen = 2*j+1;
}
///i是偶数串的中心
for(j = 0; i-j>=0&&i+j+1<len;j++)
{
if(str[i-j]!=str[i+j+1])
break;
if(2*j+2>maxlen)
maxlen = 2*j+2;
}
}
printf("%d\n",maxlen);
}
return 0;
}

2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)的更多相关文章

  1. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  2. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  3. 2017年上海金马五校程序设计竞赛:Problem G : One for You (博弈)

    Description Given a m × n chessboard, a stone is put on the top-left corner (1, 1). Kevin and Bob ta ...

  4. 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)

    Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...

  5. 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)

    Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...

  6. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  7. 2017Summmer_上海金马五校 F题,G题,I题,K题,J题

    以下题目均自己搜 F题  A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...

  8. HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)

    题目链接  2016 CCPC东北地区大学生程序设计竞赛 B题 题意  给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...

  9. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

随机推荐

  1. AV Foundation 实现文字转语音

    AV Foundation 主要框架 CoreAudio 音频处理框架 扩展学习:<Learning CoreAudio> CoreVideo 视频处理的管道模式,逐帧访问 CoreMed ...

  2. Linux-Shell脚本编程-学习-1-Linux基本命令

    在学习Linux-Shell脚本编程之前,我们需要学习一定的Linux基本命令,不然在后面学习Shell脚本编程的的时候,我们就呵呵了. 我学习所用的系统是Ubuntu 16.04版本 也没有什么规则 ...

  3. Qt 解析网络数据出现ssl错误

    最近写了点小东西,哈哈, 网络部分是同学帮我搞的 在编译的时候,出现了一下错误 qt.network.ssl: QSslSocket: cannot call unresolved function ...

  4. 数据库sql命令

    本文为转载,原文地址:http://www.cnblogs.com/cangqiongbingchen/p/4530333.html 1.说明:创建数据库CREATE DATABASE databas ...

  5. Prometheus 普罗米修斯监控

    周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下 这里只介绍客户端的配置 1:首先在POM中添加依赖 <dependency> <groupI ...

  6. beta版本冲刺七

    目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...

  7. LTE QOS

    http://wenku.baidu.com/link?url=ziFIkdKaC7MU2RY-bTOp2bt87WFPw5_02bqmYs5W6w4ktOfPHEcWesK1U2T7YiyXjVSM ...

  8. Linux建立FTP服务器

    http://blog.chinaunix.net/uid-20541719-id-1931116.html http://www.cnblogs.com/hnrainll/archive/2011/ ...

  9. Ext.net中TreePanel动态生成

    这个问题可以参考官网例子:http://examples2.ext.net/#/TreePanel/Basic/Built_in_CodeBehind/ 贴一段本人程序中用到的动态生成核心代码: Ex ...

  10. eclipse安装问题

    eclipse安装之前需要安装JDK. 注意:eclipse和JDK需要一致,如都是64位或者都是32位. 不然会报错.