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算法 在介绍算法之前,首先介绍一下什么是回文串,所谓回文串,简 ...
随机推荐
- 查找对端mac地址
1.ping对端mac: 2.arp命令查找:
- string转Date转回String(JAVA)
String dateString = "2012-12-06 "; SimpleDateFormat sdf = new SimpleDateFormat("yyy ...
- Git的一些资源链接
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- ubuntu16.04 LTS把下载源改为阿里云的源
为什么要切换下载源到国内的源上? Ubuntu的中国服务器下载速度很慢,我们可以尝试修改软件更新源,这样下载和更新软件的速度会加快很多. 一.linux系统版本: ubuntukylin-16.04- ...
- openvSwitch 基本命令
建立ovs接口连接两个namespace组成二层网络 环境搭建拓扑 br0 +--------------------------------------+ +--+ +--+ +---+ | tap ...
- linux上安装redis4.0.9
redis安装从3.0的版本到现在4.0的版本,现在装一个4.0的版本供大家学习使用. 先yum安装gcc yum -y install gcc 已加载插件:fastestmirror, langpa ...
- Note of The Linux Command Line
心得 在用鼠标点击的图形化桌面之前,单纯用键盘操作软件的时代已经很成熟了.并且还在这样延续下去.鼠标不是电脑操作的唯一模式,至少不是程序员的. 在黑色屏幕下,因为没有鼠标所以只能用按键来操作软件.包括 ...
- 彻底弄懂“PKIX path building failed”问题
SSL的基础知识 SSL的全称是Secure Socket Layer.它的通信流程如下图所示,客户端与服务端会通过几次通信,通过非对称加密创建出一个加密密钥,用于以后的对称信息加密. 1,客户端明文 ...
- 【sping揭秘】6、IOC容器之统一资源加载策略
Spring中的resource 我们先看看类之间的关系 注意我们的application是间接继承了resourceloader的,也就是说我们的application其实就是一个resourcel ...
- 08-02 Java 代码块,代码块执行的先后顺序问题
代码块 /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代 ...