zoj 2744 Palindromes

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


Author: LIU, Yaoting


Source: Zhejiang Provincial Programming Contest 2006
Submit    Status
//1858692 2009-05-07 18:43:31 Accepted  2744 C++ 730 24624 Wpl 
#include <iostream>
#define MAX 5002
using namespace std;
char str[MAX];
bool mark[MAX][MAX];
int main()
{
    
int i,j,len,r,sum;
    
while(scanf("%s",str+1)!=EOF)
    {
        len
=strlen(str+1);
        sum
=0;
        
for(r=1;r<=len;r++)
            
for(i=1;i<=len-r+1;i++)
            {
                j
=i+r-1;
                mark[i][j]
=false;
                
if(str[i]==str[j])
                {
                    
if(r==1)
                    {
                        sum
++;
                        mark[i][j]
=true;
                    }
                    
if(r==2)
                    {
                        mark[i][j]
=true;
                        sum
++;
                    }
                    
else if(mark[i+1][j-1])
                    {
                        mark[i][j]
=true;
                        sum
++;
                    }
                }
            }
        printf(
"%d\n",sum);
    }
    
return 0;
}
原文地址:https://www.cnblogs.com/forever4444/p/1452565.html