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. C#的内存管理

    栈的填充方式是从高到低,高数位到低数位的填充 堆的填充方式是从低向高,低数位到高数位的填充 内存堆上没有被栈引用的东西,才会被垃圾回收器回收. GC垃圾自动回收会重新排列堆里面的内存占用,自动回收运行 ...

  2. JavaScript RegExp 身份证、账号密码、email正则

    什么是正则表达式 正则表达式是构成搜索模式. 在文本中搜索数据时,可以使用此搜索模式来描述正在搜索的内容. 正则表达式可以是单个字符,也可以是更复杂的模式. 正则表达式可用于执行所有类型的文本搜索和文 ...

  3. Django数据模型--表关系(一对多)

    一.一对一关系 使用方法:models.ForeignKey(要关联的模型) 举例说明:年级.教师和学生 from django.db import models class Grade(models ...

  4. Tensorflowonspark安装

    1.实验环境 Centos7+Python3.6+Java8+Hadoop2.6+Spark2.3+Tensorflow1.10.0 2.Tensorflow安装 最简单的方式:pip install ...

  5. picker组件,mode=date,苹果机年份从1开始

    由于在IOS上复制图片不方便, 所以用了张别的网站的图 这是在没有设置value和start的情况下出现的,安卓机上显示是好的.尝试完网上相关说法, 发现都不中! 通过各种挣扎啊!~ 心里苦啊~ 复制 ...

  6. PAT 1065 单身狗

    https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...

  7. Log4Net讲解

    声明:本文内容主要译自Nauman Leghari的Using log4net,亦加入了个人的一点心得(节3.1.4). 1           简介 1.1          Log4net的优点: ...

  8. 一个类似植物大战僵尸的python源码

    # 1 - Import library import pygame from pygame.locals import * import math import random # 2 - Initi ...

  9. JSON简介(2)

    例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  10. 【bzoj2141】排队 [国家集训队2011]排队(魏铭) 树套树 线段树套替罪羊树

    这个题就是动态偏序对,每次操作做两个删除两个插入就好了. #include<cstdio> #include<iostream> #include<cstring> ...