Title: Longest palindrome string (c + +)

Source: Internet
Author: User

See the first reaction to this question is done ah, and then began to write, and so finish a test. Emmmmm, originally is the longest palindrome string is not the longest palindrome subsequence, but write is written, I will change the code slightly to adapt to the topic, the code is as follows:

Static ConstAuto IO_SPEED_UP =[] () {Std::ios::sync_with_stdio (false);    Cin.tie (nullptr); return 0;} ();classSolution { Public:    stringLongestpalindrome (strings) {intn =s.length (); if(n = =1|| n = =0)            returns; intMax =0; intBegin =0; Vector<vector<int> > DP (n,vector<int>(n));  for(intj =0; J < N; J + +) {Dp[j][j]=1;  for(inti = J-1; I >=0; i--)            {                if(S[i]! =S[j])Continue; if(Dp[i +1][j-1] ==0&& I! = J-1)                    Continue; DP[I][J]= Dp[i +1][j-1] +2; if(Max >=Dp[i][j])Continue; Max=Dp[i][j]; Begin=i; }        }        if(max = =0)            returnS.SUBSTR (Begin,1); Else            returns.substr (begin, Max); }};

Then this code a look is very rubbing ah, completely ill should question, guessing efficiency must be very low, and so on after the submission, sure enough, only more than 13.74% of the code. Had to delete the rewrite.

The second write code is more efficient than the 32.9% code below:

Static ConstAuto IO_SPEED_UP =[] () {Std::ios::sync_with_stdio (false);    Cin.tie (nullptr); return 0;} ();classSolution { Public:    stringLongestpalindrome (strings) {intn =s.length (); if(n = =1|| n = =0)            returns; intMax =0; intdiff =0; intBegin =0; intEnd =0; intMaxbegin =0; intMaxend =0;  for(inti =0; I < n; ++i) { for(intj = N-1; J >0; --j) {if(Max > J-i) Break; Begin=i; End=J;  while(S[begin] = = S[end] && begin <=end) {                    ++begin; --end; }                if(End < begin && Begin-end <3) {diff= J-i; if(Max <diff) {Max=diff; Maxbegin=i; Maxend=J; }                }            }            if(Max > N-i-1)                 Break; }        returnS.substr (Maxbegin, Max +1); }};

Well, I feel like this is the best solution I can figure out, and then I looked at the solution on the website. which

The second-to-last solution is this one.

classSolution { Public:    stringLongestpalindrome (strings) {intLen =0; intleft =0; intright =0; stringresult =""; inti =0;  while(I <s.size ()) { Left=i; Right=i;  while(Right +1< S.size () && s[right] = = S[right +1]) right++; I= right +1;  while(Left >=0&& Right < S.size () && s[left] = =S[right]) { Left--; Right++; }            if(Len < Right-left-1) {len= Right-left-1; Result= S.substr (left +1, Len); }        }        returnresult; }};

You didn't. Optimize the input and output stream, and then split the efficiency of the result string multiple times, it can achieve 6ms execution Speed row second.

I looked at the code of the Great God and found that the main idea was to start with a central subscript, and then he used a clever way to deal with even-length strings starting with two characters, that is, after the center is selected, if the character after the loop is typeface equal to the selected word, then +1. This way, if an even-length string is present, left and right point to the adjacent two characters at the beginning. I added this big god code to the speed, and then dropped to the leetcode on the detection, the modified code is as follows:

Static ConstAuto IO_SPEED_UP =[] () {Std::ios::sync_with_stdio (false);    Cin.tie (nullptr); return 0;} ();classSolution { Public:    stringLongestpalindrome (strings) {intLen =0; intleft =0; intright =0; intMaxres =0; intLeftres =0; inti =0;  while(I <s.size ()) { Left=i; Right=i;  while(Right +1< S.size () && s[right] = = S[right +1]) right++; I= right +1;  while(Left >=0&& Right < S.size () && s[left] = =S[right]) { Left--; Right++; }        if(Len < Right-left-1) {len= Right-left-1; Leftres=Left ; Maxres=Len; }    }    returnS.SUBSTR (Leftres +1, Maxres); }};

The test results are then displayed as follows:

More than 100% and show execution speed of 0ms, bad.

After a general look at the first row of the algorithm, found that the "#" to fill the character as the original 2n+1 and then from the central character calculus, completely without the brother's ingenious, here will not mention.

Title: Longest palindrome string (c + +)

Related Article

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.