1089 longest reply substring

Source: Internet
Author: User
1089 maximum echo substring V2 (manacher algorithm) reference time limit: 1 second space limit: 131072 kb score: 0 difficulty: A base-question echo string is a symmetric string between the left and right sides of ABA, Abba, cccbccc, and AAAA. Input a string 'str' and output the maximum length of the substring in 'str. Input
Input STR (STR length <= 100000)
Output
The maximum length of the input string (l.
Input example
daabaac
Output example
5

Marathon algorithm Template
It is also a further understanding of the marathon algorithm.

1 // marathon algorithm 2 # include <bits/stdc ++. h> 3 # define n 1000000 4 using namespace STD; 5 6 int reslen; 7 int P [N]; 8 int manacher (string S) {9 // Insert '#'10 string T = "$ #"; 11 for (INT I = 0; I <S. length (); ++ I) {12 t + = s [I]; 13 T + = "#"; 14} 15 // process T16 int MX = 0, id = 0, reslen = 0, rescenter = 0; 17 for (INT I = 1; I <t. length (); ++ I) {18 // assign values to the substrings that have been repeatedly searched, and analyze 19 P [I] = Mx> I? Min (P [2 * ID-I], Mx-I): 1; 20 // multiple parts are analyzed by yourself 21 While (T [I + P [I] = T [I-P [I]) 22 + P [I]; 23 24 // update the maximum position to the right and record the current position 25 if (MX <I + P [I]) {26 MX = I + P [I]; 27 id = I; 28} 29 // update the length of the oldest string, and the length of the radius and current position 30 if (reslen <p [I]) {31 reslen = P [I]; 32 // rescenter = I; 33} 34} 35 // return S. substr (rescenter-reslen)/2, reslen-1); 36 return reslen-1; 37} 38 39 string s; 40 int main () {41 CIN> S; 42 cout <manacher (s) <Endl; 43 return 0; 44}

 

1089 longest reply substring

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.