杭电 1544

题意:数出一个字符串中的回文子串数。

Analyse:水题,分奇数长度与偶数长度的子串来数即可。

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char str[5005];
 6     int i,j,head,tail;
 7     int counter;
 8     while(gets(str))
 9     {
10         head=0;
11         tail=strlen(str)-1;
12         counter=0;
13         //奇数长度
14         for(i=head;i<=tail;i++)
15         {
16             for(j=0;i-j>=head && i+j<=tail;j++)
17             {
18                 if(str[i-j]!=str[i+j])
19                     break;
20                 counter++;
21             }
22         }
23         //偶数长度
24         for(i=head;i<tail;i++)
25         {
26             if(str[i]==str[i+1])
27             {
28                 counter++;
29                 for(j=1;i-j>=head && i+j+1<=tail;j++)
30                 {
31                     if(str[i-j]!=str[i+j+1])
32                         break;
33                     counter++;
34                 }
35             }
36         }
37         printf("%d\n",counter);
38     }
39     return 0;
40 }
posted @ 2012-07-22 23:57  Hogg  阅读(178)  评论(0)    收藏  举报