06_最长回文子串长度


一、测试程序

 1 /*
 2  *    最长回文子串长度
 3  *
 4  */
 5 
 6 #include 
 7 #include <string.h>
 8 
 9 #define MAX(a, b)    (a)>(b) ? (a):(b)
10 
11 int get_lps_len(unsigned char *str)
12 {
13     unsigned int len = 0;
14     int right = 0;
15     int left = 0;
16     int max = 0;
17     int i = 0;
18     int j = 0;
19     
20     len =  strlen(str);
21     for (; i) {
22         j = 3;
23         while (--j) {
24             left = i;
25             right = left + j;
26             while (left>=0 && right// 分别穷举长度为奇数和偶数的回文子串
27                 left--;
28                 right++;
29             }
30             max = MAX(max, right-left-1);
31         }
32     }
33     
34     return max;
35 }
36 
37 int main()
38 {
39     unsigned char str[10240] = {0};
40     
41     while (gets(str))
42         printf("%d\n", get_lps_len(str));
43     
44     return 0;
45 }

 

相关