2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)
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 (字符串处理)的更多相关文章
- 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 ...
- 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)
Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...
- 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 ...
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)
Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- 2017Summmer_上海金马五校 F题,G题,I题,K题,J题
以下题目均自己搜 F题 A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...
- HDU 5923 Prediction(2016 CCPC东北地区大学生程序设计竞赛 Problem B,并查集)
题目链接 2016 CCPC东北地区大学生程序设计竞赛 B题 题意 给定一个无向图和一棵树,树上的每个结点对应无向图中的一条边,现在给出$q$个询问, 每次选定树中的一个点集,然后真正被选上的是这 ...
- “盛大游戏杯”第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 描述 在一个矩形的灰度图像上,每个 ...
随机推荐
- c#一些常用的方法集合
是从一个asp.net mvc的项目里看到的.挺实用的. 通过身份证号码获取出生日期和性别 通过身份证号码获取出生日期和性别 #region 由身份证获得出生日期 public static stri ...
- 常用js方法合集
var Default = { init: function () { }, addCookie: function (name,data) { var expdate = new Date(); / ...
- Hadoop世界中的HelloWorld之WordCount具体分析
MapReduce 应用举例:单词计数 WorldCount可以说是MapReduce中的helloworld了,下面来看看hadoop中的例子worldcount对其进行的处理过程,也能对mapre ...
- Spring Boot学习(一):入门篇
目录 Spring Boot简介 Spring Boot快速搭建 1 新建项目 2 运行项目 3 设置spring boot可以热部署(修改后端代码后,自动部署,不用手动部署) 3.1:配置pom.x ...
- C++编码规范101
组织和策略问题 第0条 不要拘泥于小节(又名:了解哪些东西不应该标准化) 第1条 在高警告级别干净利落地进行编译 第2条 使用自动构建系统 第3条 使用版本控制系统 第4条 在代码审查上投入 设计风格 ...
- express框架 中间件
- Spark探索经典数据集MovieLens
Spark探索经典数据集MovieLens 阅读目录 前言 环境 初步预览 探索用户数据 探索电影数据 探索评级数据 回到顶部 前言 MovieLens数据集包含多个用户对多部电影的评级数据,也包括电 ...
- P1338 末日的传说
题目描述 只要是参加jsoi活动的同学一定都听说过Hanoi塔的传说:三根柱子上的金片每天被移动一次,当所有的金片都被移完之后,世界末日也就随之降临了. 在古老东方的幻想乡,人们都采用一种奇特的方式记 ...
- Codeforces Round #390 (Div. 2) E(bitset优化)
题意就是一个给出2个字符矩阵,然后进行匹配,输出每个位置的匹配的结果 (超出的部分循环处理) 一种做法是使用fft,比较难写,所以没有写 这里使用一个暴力的做法,考虑到一共只出现26个字符 所以使用一 ...
- [洛谷P4015]运输问题
题目大意:有m个仓库和n个商店.第i个仓库有 $a_{i}$ 货物,第j个商店需要$b_{j}$个货物.从第i个仓库运送每单位货物到第j个商店的费用为$c_{i,j}$.求出最小费用和最大费用 题 ...