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;
}

相关文章:

  • 2021-05-27
  • 2022-02-10
  • 2021-12-12
  • 2021-09-27
  • 2021-08-10
  • 2022-12-23
  • 2021-12-29
  • 2021-05-15
猜你喜欢
  • 2021-07-04
  • 2022-01-22
  • 2021-06-30
  • 2022-02-06
  • 2022-01-22
  • 2021-05-16
  • 2021-07-16
相关资源
相似解决方案