zoj 2744 Palindromes(计算回文子串个数的优化策略)
题目链接:
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(计算回文子串个数的优化策略)的更多相关文章
- 马拉车算法——求回文子串个数zoj4110
zoj的测评姬好能卡时间.. 求回文子串的个数:只要把p[i]/2就行了: 如果s_new[i]是‘#’,算的是没有中心的偶回文串 反之是奇回文串 /* 给定两个字符串s,t 结论:s,t不相同的第一 ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- HDU 3948 不同回文子串个数
集训队论文中有求不同子串个数的做法,就是扫一遍height数组,过程中根据height数组进行去重.对于本题也是雷同的,只是每一次不是根据与排名在上一位的LCP去重,而是与上一次统计对答案有贡献的后缀 ...
- Manacher's Algorithm && 647. Palindromic Substrings 计算回文子串的算法
注:转载自:https://www.cnblogs.com/love-yh/p/7072161.html
- HDU 1544 Palindromes(回文子串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...
- CF 17E Palisection 求相交回文串个数
In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...
- [LeetCode] 647. 回文子串 ☆☆☆(最长子串、动态规划、中心扩展算法)
描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串. 示例 1: 输入: "abc" ...
- URAL 2037 Richness of binary words (回文子串,找规律)
Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...
- 计算字符串的最长回文子串 :Manacher算法介绍
转自: http://www.open-open.com/lib/view/open1419150233417.html Manacher算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...
随机推荐
- 图解FTP服务器搭建(Windows Server 2008)
http://wenku.baidu.com/link?url=aUMoUYvSXKbHdbOHt4lUUCq0BhjnPRTM8jNb80jjwJ4_CM5LFq3lSm6f1ZlPNbFo6HEj ...
- [leet code 4] Median of Two Sorted Arrays
1 题目 There are two sorted arrays A and B of size m and n respectively. Find the median of the two so ...
- 利用 Azure Devops 创建和发布 Nuget 包
利用 Azure Devops 创建和发布 Nuget 包 原 Visual Studio Team Service ,简称 VSTS,能够创建 pipelines 管道以构建应用程序,并将其部署到任 ...
- StriveEngine-TCP
文章中的StriveEngine.dll版本为V3.9.0.0,源码下载请到 https://download.csdn.net/download/hanghangz/10966335 先上代码,建立 ...
- ASP.NET MVC 使用Unity实现Ioc
为什么有这篇文章 最近在学ASP.NET MVC项目中使用Ioc,选用了Unity作为依赖注入的容器组件,在网上找了相关的文章简单实现了依赖注入,但想用文件配置的方式进行容器注入的注册,发现相关的文章 ...
- UWP Background过渡动画
首先说两件事: 1.大爆炸我还记着呢,先欠着吧... 2.博客搬家啦,新地址:https://blog.ultrabluefire.cn/ ==========下面是正文========== 前些日子 ...
- OpenvSwitch端口镜像
OVS上实现端口镜像的基本流程如下: 创建 mirror ,在 mirror 中指定镜像数据源及镜像目的地 将创建的 mirror 应用到 bridge 中 镜像数据源可以通过下面几个选项来指定: s ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- 安装fastdfs文件系统
1.下载源码包 需要下载的源码包: fastDFS源代码:FastDFS_v5.01.tar.gz fastDFS的nginx模块源代码:fastdfs-nginx-module_v1.15.tar. ...
- 社会地位即服务, Status as a Service (二): 社交网络的投资回报率 (ROI)
本文作者 Eugene Wei 是 Amazon 战略部门的第一位分析师,后来还担任过 Flipboard, Hulu, Oculus 的产品负责人. 原文标题是 Status as a Servic ...