题目链接:

  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2744

题目描述:

  A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.

Now give you a string S, you should count how many palindromes in any consecutive substring of S.

Input

There are several test cases in the input. Each case contains a non-empty string which has no more than 5000 characters.

Proceed to the end of file.

Output

A single line with the number of palindrome substrings for each case.

Sample Input

aba
aa

Sample Output

4
3

 /*问题 求一个字符串的回文子串的个数
解题思路 首先弄明白一个字符串的子串的个数,比如abaa,子串有a,b,a,a,ab,aba,abaa,aba,ab。
一般的解题思路是找出所有子串一一判断,但是当字符串很长的时候可能会出现超时错误。
现提供一种优化策略:
对于一个串来说,如果该串的中心保证回文才能保证以该中心为基础的扩展串是回文串,换句话说,只要该串的中心
不是回文则直接判定向外扩展的串没有一个回文串,直接结束扩展。
具体实现比较考验耐心,具体的数带入几个串计算一下即可。*/
#include <cstdio>
#include <cstring> int main()
{
char str[];
int half,left,right,i,len,count;
while(scanf("%s",str) != EOF)
{
count=;
len=strlen(str);
for(i=;i<=len-;i++){//钉住最后一个字符,从左往右
half=(len--i)/;
if((len-i) & )//该串长度为奇数
{
left=i+half-;
right=left+;
}
else//该串长度为偶数
{
left=i+half;
right=left+;
}
while(left>=i){
if(str[left] == str[right]){
count++;
left--;
right++;
}
else
break;
}
} for(i=len-;i>=;i--){//钉住第一个字符,从右往左
half=i/;
if(i+ & )//该串长度为奇数
{
left=half-;
right=left+;
}
else//该串长度为偶数
{
left=half;
right=left+;
}
while(left>=){
if(str[left] == str[right]){
count++;
left--;
right++;
}
else
break;
}
}
printf("%d\n",len+count);
}
return ;
}

zoj 2744 Palindromes(计算回文子串个数的优化策略)的更多相关文章

  1. 马拉车算法——求回文子串个数zoj4110

    zoj的测评姬好能卡时间.. 求回文子串的个数:只要把p[i]/2就行了: 如果s_new[i]是‘#’,算的是没有中心的偶回文串 反之是奇回文串 /* 给定两个字符串s,t 结论:s,t不相同的第一 ...

  2. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  3. HDU 3948 不同回文子串个数

    集训队论文中有求不同子串个数的做法,就是扫一遍height数组,过程中根据height数组进行去重.对于本题也是雷同的,只是每一次不是根据与排名在上一位的LCP去重,而是与上一次统计对答案有贡献的后缀 ...

  4. Manacher's Algorithm && 647. Palindromic Substrings 计算回文子串的算法

    注:转载自:https://www.cnblogs.com/love-yh/p/7072161.html

  5. HDU 1544 Palindromes(回文子串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...

  6. CF 17E Palisection 求相交回文串个数

    In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...

  7. [LeetCode] 647. 回文子串 ☆☆☆(最长子串、动态规划、中心扩展算法)

    描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "abc" ...

  8. URAL 2037 Richness of binary words (回文子串,找规律)

    Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...

  9. 计算字符串的最长回文子串 :Manacher算法介绍

    转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...

随机推荐

  1. (使用STL自带的排序功能进行排序7.3.2)POJ 2092 Grandpa is Famous(结构体排序)

    /* * POJ_2092.cpp * * Created on: 2013年11月1日 * Author: Administrator */ #include <iostream> #i ...

  2. 查找对端mac地址

    1.ping对端mac: 2.arp命令查找:

  3. adb push 和 adb pull命令

    adb push命令 :从电脑上传送文件到手机: adb pull命令 :从手机传送文件到电脑上             @Cocos 下次需要权限的目录可以执行chmod 777 目录名      ...

  4. 前端开发 - HTML/CSS

    概述 HTML是英文HyperText Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记). 相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  5. [杂谈] 一个关于 as 的小测试

    代码如下:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 procedure TForm1.Button1Click(Sender: TObject); va ...

  6. C# Winform模仿百度日历

    想写博客不知道从何处开始,就从回忆开始吧. 第一个就从自定义日历控件开始 产生背景: 大概2015年时候有个项目要用到日历,用默认日历展示给用户看,用户毫不客气都说界面太丑,最好做成像百度日历那样方便 ...

  7. apache ab

    ab -p postfile.json -T 'application/json' -n 100 -c 10 -v 2 http://192.168.1.103:3002/checkStashSlot ...

  8. 解压cpio.gz

    #gunzip 文件名.cpio.gz #cpio -idmv < 文件名.cpio

  9. Spring中使用StandardServletMultipartResolver进行文件上传

    从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...

  10. 不一样的日期、时间转换(moment.js)

    无意中遇到了一种很奇怪的日期格式,从接口中返回的日期是这样的,如 2018-02-06T11:59:22+08:00 .然而这却不是我们想要的,我们要的是这种,YYYY-MM-DD HH:mm:ss. ...