HDU1544 Palindromes回文

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU1544 Palindromes回文相关的知识,希望对你有一定的参考价值。

Palindromes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1985 Accepted Submission(s): 944

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

Author
LIU, Yaoting

Source
Zhejiang Provincial Programming Contest 2006

问题链接HDU1544 Palindromes
问题简述:给定一个字符串,计算其子串中的回文数量
问题分析:简单的回文有关的字符串计算问题,用枚举法来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU1544 Palindromes */

#include <bits/stdc++.h>

using namespace std;

const int N = 5000 + 1;
char s[N];

int main()
{
    while (~scanf("%s", s)) {
        int len = strlen(s), cnt = 0;
        for (int i = 0; i < len; i++) {
            int l = i - 1;
            int r = i + 1;
            // 子串长度为奇数
            while (l >= 0 && r < len && s[l] == s[r])
                cnt++, l--, r++;
            // 子串长度为偶数
            l = i;
            r = i + 1;
            while (l >= 0 && r < len && s[l] == s[r])
                cnt++, l--, r++;
        }

        printf("%d\\n", cnt + len);
    }

    return 0;
}

以上是关于HDU1544 Palindromes回文的主要内容,如果未能解决你的问题,请参考以下文章

HDOJ/HDU 2163 Palindromes(判断回文串~)

HDU - 5340 Three Palindromes(manacher算法)

hdu5340—Three Palindromes—(Manacher算法)——回文子串

HDU 2029 Palindromes _easy version

HDU2029 Palindromes _easy version入门

HDU2029 Palindromes _easy version入门