九度oj 题目1252:回文子串

题目描述:

输入一个字符串,输出该字符串中对称的子字符串的最大长度。
比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。

输入:

存在多组数据,每组数据一行字符串,长度不大于100。

输出:

输出回文子串的最大长度。

样例输入:
google
样例输出:
4

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 char temp[102];
 8 int cnt[102];
 9 
10 int findHJ(int n, int len) {
11     int i = n-1, j = n+1;
12     int ans = 1;
13     while(i >= 0 && j < len) {
14         if(temp[i] != temp[j]) {
15             return ans;
16         }
17         i--;
18         j++;
19         ans = ans + 2;
20     }
21     return ans;
22 }
23 
24 int findHO(int n, int len) {
25     if(n + 1 > len-1) {
26         return 1;
27     }
28     if(temp[n] != temp[n+1]) {
29         return 1;
30     }
31     int i = n-1, j = n+2;
32     int ans = 2;
33     while(i >= 0 && j < len) {
34         if(temp[i] != temp[j]) {
35             return ans;
36         }
37         i--;
38         j++;
39         ans = ans + 2;
40     }
41     return ans;
42 }
43 
44 int main(int argc, char const *argv[])
45 {
46     while(scanf("%s",temp) != EOF){
47         int len = strlen(temp);
48         int max = 0;
49         for(int i = 0; i < len; i++) {
50             int temp = findHJ(i, len);
51             if(max < temp) {
52                 max = temp;
53             }
54             temp = findHO(i, len);
55             if(max < temp) {
56                 max = temp;
57             }
58         }
59         printf("%d
",max);
60     }
61         
62     return 0;
63 }
原文地址:https://www.cnblogs.com/jasonJie/p/5769652.html