hiho一下第一周 最长回文子串

题目链接:http://hihocoder.com/contest/hiho1/problem/1

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1e6 + 5;
 8 char str[maxn];
 9 
10 int get_palindrome(char *s)
11 {
12     int ans = 1;   // 一个字符也叫回文串
13     for (int i = 1; s[i]; i++) {
14         int l = i, r = i;   // l 的使用能够保证偶数回文串也能用相同公式处理,它保存中间字符最左边的位置 
15         while (s[r+1] == s[i]) {  // 处理aaaaa这种情况
16             r++;
17         }
18         i = r;
19         while (s[r+1] == s[l-1]) {
20             r++;
21             l--;
22         }
23         ans = max(ans, r-l+1);
24     }
25     return ans;
26 }
27 
28 int main()
29 {
30     #ifndef ONLINE_JUDGE
31         freopen("in.txt", "r", stdin);
32     #endif // ONLINE_JUDGE
33     int n;
34     str[0] = '#';   // 插入特殊字符,主要是处理整个字符串都是回文串的情况
35     while (scanf("%d", &n) != EOF) {
36         while (n--) {
37             scanf("%s", str+1);
38             printf("%d
", get_palindrome(str));
39         }
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/windysai/p/4821728.html