hduoj1544(判断连续回文)
2016-04-23 10:52
489 查看
Palindromes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1509 Accepted Submission(s): 712
Problem Description
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
题解:从某一个开始,然后分偶数和奇数向左向右扩展,遇到不相等就退出,用二维dp会超时。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char str[5005]; bool dp[5005][5005]; int main(){ int len,i,j,cnt,l,r; while(scanf("%s",str)!=EOF){ len=strlen(str); cnt=len; for(i = 0 ; i < len ; i++){ l=i-1; r=i+1; while(l>=0&&r<len&&str[l]==str[r]){ l--;r++; cnt++; } l=i;r=i+1; while(l>=0&&r<len&&str[l]==str[r]){ l--;r++; cnt++; } } printf("%d\n",cnt); //cout<<cnt<<endl; } return 0; }
相关文章推荐
- mybatis migrate常用指令
- flask-admin章节一:使用chartkick画报表
- 安全应急响应
- Android For JNI(四)——C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统
- Android For JNI(四)——C的数组,指针长度,堆内存和栈内存,malloc,学生管理系统
- O2O 行业 IT 系统架构实践分享——预告
- uva 10561 Treblecross (SG函数)
- Android WebView常见问题及解决方案汇总
- jQuery 视觉效果-点击图片,图片从左边消失,并从右边出现
- Git 使用问题 - win7 git bash下git pull失败
- How to: Configure an Azure SQL Database firewall using the Azure Portal
- Array.prototype.sort()对数组对象排序的方法
- assert()函数用法总结
- 【BZOJ1271】[BeiJingWc2008]秦腾与教学评估【二分】【神题】
- Android开发之ViewPager切换动画
- AndroidEventBus(事件总线)了解+实战体验
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- codevs1044四子连棋(Dfs)
- Turn the corner
- 1013&&1014上下楼梯