zoj 2744
2009-10-04 19:14
323 查看
Palindromes
Time Limit: 1 Second Memory Limit: 32768 KB
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
此题的方法是:()()()aa()()()..... or ()()aba()()....找到这样的序列之后再向两边拓展,在拓展的过程中两头的字符相同
则个数加1;
Time Limit: 1 Second Memory Limit: 32768 KB
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
此题的方法是:()()()aa()()()..... or ()()aba()()....找到这样的序列之后再向两边拓展,在拓展的过程中两头的字符相同
则个数加1;
#include <iostream> using namespace std; char c[5001]; int l,num; void is_Pal(int t, int k) { if(c[t] == c[k]) { num++; while(--t>=0&&++k<l) { if(c[t]==c[k]) num++; else return; } } } int main() { while(scanf("%s",c)!=EOF) { l = strlen(c); num = 0; for( int i = 1; i<=2 ; i++) { for(int j = 0; j < l-i; j++) { is_Pal(j,j+i); } } num += l; cout<<num<<endl; } return 0; }
相关文章推荐
- ZOJ 2744 Palindromes
- ZOJ Problem Set - 2744 Palindromes(字符串二分处理的好题)
- ZOJ 2744 Palindromes 区间DP
- ZOJ-2744-Palindromes
- zoj 2744
- zoj 2744 Palindromes (求回文子序列的个数)
- zoj 2744 Palindromes(计算回文子串个数的优化策略)
- ZOJ 2744 Palindromes(动态规划)
- zoj 2744 Palindromes
- zoj 2744 - Palindromes
- zoj_2744 Palindromes 回文
- zoj 2744 Palindromes
- zoj 2744 DP做法
- zoj 2744 Palindromes
- zoj2744 Palindromes 字符串的题
- zoj 2744
- zoj 2744 - Palindromes
- ZOJ 2744 Palindromes
- ZOJ 2744 Palindromes
- ZOJ-2744