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. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  2. How to set pycharm configure for remoting development

    配置pycharm远程连接,点击pycharm的tools,选择deployment选项,选择configuration. 2 点击左侧的加号按钮,新增一个连接,取个名字,根据个人配置选择协议,这里选 ...

  3. 【多线程】 Task ,async ,await

    [多线程]Task ,async ,await 一. WinForm 里经常会用到多线程, 多线程的好出就不多说了,来说说多线程比较麻烦的地方 1. UI 线程与其他线程的同步,主要是 Form 和 ...

  4. 14.0 native webview H5切换

    在讲这章之前先说明一个问题,那就是 native webview 都是属于原生的...webview目前用的比较多的是谷歌内核和腾讯X5内核  H5是网页! 还是安卓市场---直接写好脚本进入个人中心 ...

  5. LeetCode 61——旋转链表

    1. 题目 2. 解答 2.1. 方法一 将链表每个节点向右移动 1 个位置,其实就是让链表最后一个结点指向第一个结点. 因此,向右移动 k 个位置就重复上述过程 k 次即可. 然后,我们注意到,若链 ...

  6. nonebot 源码阅读笔记

    前言 nonebot 是一个 QQ 消息机器人框架,它的一些实现机制,值得参考. nonebot NoneBot 初始化(配置加载) 阅读 nonebot 文档,第一个示例如下: import non ...

  7. oracle INSERT INTO多个值

    稍微熟悉Oracle的都知道,如果我们想一条SQL语句向表中插入多个值的话,如果INSERT INTO 某表 VALUES(各个值),VALUES(各个值),.....;这样会报错的,因为oracle ...

  8. TCP的挥手协议和握手协议2

    三次握手协议:三次握手协议的主要过程是交互彼此之间的初始序列号,如果没有确认的ACK帧可以么?肯定是可以的 client A -------> server B client A 发送了自己的初 ...

  9. 域名/网站名/URL

    http://mail.163.com/index.html 1)http://:协议,也就是HTTP超文本传输协议,网页在网上传输的协议. 2)mail:服务器名,代表着是一个邮箱服务器,所以是ma ...

  10. struts2的验证

    1.原理 当浏览器向服务器提交表单数据时,在服务器端需要对表单数据的有效性进行校验. “校验方法”会在“业务方法”之前调用. 2.实现验证的两种方式 struts2校验的两种实现方法: 1. 手工编写 ...