Manacher hdoj 3068 Longest palindrome

Source: Internet
Author: User

Topic Portal

About solving the longest palindrome substring, there are DP practices, but also have the same n^2 but only with O (1) of space, there are kmp, suffix array??

1 intMainvoid)    {2      while(SCANF ("%s", str +1) ==1)  {3         intlen = strlen (str +1);4Memset (DP,false,sizeof(DP));5          for(intI=1; i<=len; ++i) Dp[i][i] =true;6          for(intI=1; i<len; ++i) {7dp[i][i+1] = (Str[i] = = str[i+1]);8         }9         intAns =1;Ten          for(intI=3; i<=len; ++i) { One              for(intj=1; j+i-1<=len; ++j) { A                 if(dp[j+1][j+i-2] && str[j] = = str[j+i-1]) { -dp[j][j+i-1] =true; -Ans =Max (ans, i); the                 } -             } -         } -printf ("%d\n", ans); +     } -  +     return 0; A}
n^2

But the complexity of the n^2 is not over 3068 of the problem, the following with O (n) algorithm to solve

1 /*2 Test Instructions: Finding the longest palindrome (not the longest palindrome sequence)3 Manasher: A good understanding, easy to implement, O (n) time complex very dick algorithm. 4 simply say the principle: first insert the original string between each adjacent to a character that is unlikely to appear in the string, this can solve the parity problem5 then P[i] indicates that I is the center of the palindrome, the longest palindrome length (unilateral), however in the original string ans = max P[i]-16 I think the essence of the algorithm is to seek p[i] full use of the nature of palindrome, skipping some useless scanning7 English version of Chinese version8 */9 /************************************************Ten * Author:running_time One * Created time:2015-8-7 19:27:46 A * File Name:Manacher.cpp -  ************************************************/ -  the#include <cstdio> -#include <algorithm> -#include <iostream> -#include <sstream> +#include <cstring> -#include <cmath> +#include <string> A#include <vector> at#include <queue> -#include <deque> -#include <stack> -#include <list> -#include <map> -#include <Set> in#include <bitset> -#include <cstdlib> to#include <ctime> + using namespacestd; -  the #defineLson L, Mid, RT << 1 * #defineRson mid + 1, R, RT << 1 | 1 $typedefLong Longll;Panax Notoginseng Const intMAXN = 1e5 +10000; - Const intINF =0x3f3f3f3f; the Const intMOD = 1e9 +7; + CharS[MAXN], str[maxn*2]; A intp[maxn*2]; the  + intManacher (void)  { -str[0] ='$'; str[1] ='#'; $     intLen =strlen (s); $      for(intI=0; i<len; ++i) { -str[i*2+2] = S[i]; str[i*2+3] ='#'; -     } theLen = Len *2+2; Str[len] =' /'; -     intMX =0, id =0;Wuyi      for(intI=1; i<len; ++i) { the         if(mx > i) p[i] = min (p[2*id-i], MX-i); -         ElseP[i] =1; Wu          while(Str[i-p[i] [= Str[i+p[i]]) p[i]++; -         if(MX < p[i] +i) { AboutMX = p[i] + i; ID =i; $         } -     } -     intRET =0; -      for(intI=0; i<len; ++i) { ARET =Max (ret, p[i]); +     } the     returnRET-1; - } $  the intMainvoid)    {//hdoj 3068 Longest palindrome the      while(SCANF ("%s", s) = =1)  { theprintf ("%d\n", Manacher ()); the     } -  in     return 0; the}

Manacher hdoj 3068 Longest palindrome

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.