First think of a very fast way to find palindrome substring, is to enumerate the middle position of the string, and then extend the comparison to both sides! But then there is a problem that is when the string itself is a repeating string of only one character when the algorithm is a n^2 algorithm, but the algorithm itself for the other data is very valuable, so only the problem of such a repeating string can be processed alone can be quickly completed solution
#include <cstdio> #include <iostream>using namespace Std;char a[1000010];int Fun () {int ans = 0;for (int i = 1;a[ i];i++) {int T;int s = i,e = I;while (a[e+1] = = A[s]) e++;//Here is the calculation of the repeating string, each time when encountering a repeating string, I can jump directly to the last position, because this paragraph is repeated, So the central position of the palindrome will only appear in the middle position of the substring i = e;while (a[s-1] = = a[e+1]) s--, e++; both sides expand T = e-s +1;if (T > ans) ans = t;} printf ("%d\n", ans); }int Main () {a[0] = ' @ '; int n;cin >> n;for (int i = 0;i < n;i++) {scanf ("%s", a+1); Fun ();} return 0;}
The longest palindrome string problem