zoj 2744 Palindromes (求回文子序列的个数)
2014-06-15 19:11
302 查看
链接:zoj 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
求回文子序列的个数,如 aba 的回文子序列有 a, b, a, aba.
解析:一个序列,若一个序列中间的两个连续字符或三个连续字符的子序列不回文,则其肯定不回文
代码:
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
求回文子序列的个数,如 aba 的回文子序列有 a, b, a, aba.
解析:一个序列,若一个序列中间的两个连续字符或三个连续字符的子序列不回文,则其肯定不回文
代码:
#include<stdio.h> #include<string.h> int main() { char s[5005]; int i,l,r,t,n; while(scanf("%s",s)!=EOF){ t=n=strlen(s); for(i=0;i<n;i++){ l=i; r=i+1; while(l>=0&&r<n){ if(s[l--]==s[r++]) t++; else break; } } for(i=0;i<n;i++){ l=i; r=i+2; while(l>=0&&r<n){ if(s[l--]==s[r++]) t++; else break; } } printf("%d\n",t); } return 0; }
相关文章推荐
- zoj 2744 Palindromes
- zoj2744 Palindromes 字符串的题
- ZOJ 2744 Palindromes 区间DP
- zoj 2744 Palindromes
- zoj_2744 Palindromes 回文
- zoj 2744 - Palindromes
- zoj 2744 - Palindromes
- ZOJ Problem Set - 2744 Palindromes(字符串二分处理的好题)
- ZOJ 2744 Palindromes(动态规划)
- ZOJ-2744-Palindromes
- ZOJ 2744 Palindromes
- zoj 2744 Palindromes(计算回文子串个数的优化策略)
- ZOJ 2744 Palindromes
- zoj 2744 Palindromes
- zoj 2744 Palindromes
- ZOJ 2744 Palindromes
- 动态规划(二)最长回文子序列
- UVa 11404 回文子序列(LCS求最长回文串长度)
- zoj 2744 DP做法
- hdu 4745 Two Rabbits (最长非连续回文子序列)