Title Link: https://leetcode.com/problems/longest-palindromic-substring/
Output the longest palindrome string
1 classSolution {2 Public:3 intpre[22222];4 Charx[22222];5 Charstr[22222];6 7 intMinintXinty) {8 returnx < y?x:y;9 }Ten One intInitChar*STR,Char*x) { A intLen =strlen (x); -str[0] ='$'; - for(inti =0; I <= Len; i++) { thestr[2* i +1] ='#'; -str[2* i +2] =X[i]; - } -Len =2* Len +2; +Str[len] =0; - returnLen; + } A voidManacher (int*pre,Char*STR,intLen) { at intID =0; - intMX =0; - for(inti =1; i < Len; i++) { -Pre[i] = mx > I? Min (pre[2* Id-i], mx-i):1; - while(Str[i + pre[i]] = = Str[i-pre[i]]) pre[i]++; - if(Pre[i] + i >mx) { inID =i; -MX = Pre[id] +ID; to } + } - } the stringLongestpalindrome (strings) { *memset (PRE,0,sizeof(pre)); $memset (str,0,sizeof(str));Panax Notoginseng intlen = init (str, const_cast<Char*>(S.data ())); - Manacher (Pre, STR, len); the intFuck =1, pos =0; + for(inti =0; i < Len; i++) { A if(Fuck <Pre[i]) { theFuck =Pre[i]; +pos =i; - } $ } $pos--; -fuck--; - stringans; the for(inti = Pos-fuck +1; I <= pos + fuck +1; i++) { - ifI2==0) {Wuyi Ans.push_back (Str[i]); the } - } Wu returnans; - } About};
[Leetcode] Longest palindromic Substring