九度oj 1528 最长回文子串

原题链接:http://ac.jobdu.com/problem.php?pid=1528 
小白书上的做法,不过这个还要简单些。。。

 1 #include<algorithm>

 2 #include<iostream>

 3 #include<cstdlib>

 4 #include<cstring>

 5 #include<cstdio>

 6 using std::max;

 7 const int Max_N = 200010;

 8 char ret[Max_N];

 9 void solve() {

10     int i, j, ans = 0, n = strlen(ret);

11     for (i = 0; i < n; i++) {

12         for (j = 0; i >= j && i + j < n; j++) {

13             if (ret[i - j] != ret[i + j]) break;

14             ans = max(ans, j << 1 | 1);

15         }

16         for (j = 0; i >= j && i + j + 1 < n; j++) {

17             if (ret[i - j] != ret[i + j + 1]) break;

18             ans = max(ans, (j << 1) + 2);

19         }

20     }

21     printf("%d\n", ans);

22 }

23 int main() {

24 #ifdef LOCAL

25     freopen("in.txt", "r", stdin);

26     freopen("out.txt", "w+", stdout);

27 #endif

28     while (gets(ret)) solve();

29     return 0;

30 }
View Code

 

你可能感兴趣的:(回文)