The longest palindrome string problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.