Programmer's pen-[maximum reply string]

Source: Internet
Author: User
Document directory
  • 1. Generally, there are two types of return strings: an even number or an odd number.
  • 2. Unify the even odd numbers
  • 3. the time complexity of the above practice is still not ideal.

Maximum return stringI believe everyone is familiar with it. There are many similar questions.

There is simply the maximum return string in the solution string, for example, "abcgoogleaba". The maximum return string is goog. Although ABA is also a return string, the length is short.

There is also a question that NetEase game has obtained: "abcgoogleaba". If it is a reply, it is regarded as a whole output. Otherwise, the output format is a, B, c, goog, L, E, ABA.

Returns the maximum input string. There are many methods, such as the brute force method. However, I believe that when you perform a test or interview, the examiner will require the best possible time and space complexity.

Therefore, we need to find a better solution.

First, the largest input string 1. The general practice is to divide the input string into an even number or an odd number.

Like the example above: abcgoogleaba, two input files, one is goog and the other is ABA.

# Include "stdafx. H "# include <iostream> using namespace STD; int getmaxhuiwen (char * P, int Len) {int I = 0, j = 0; int max = 0; int X, y; for (I = 0; I <Len; I ++) {for (j = 0; I-j> = 0 & I + j <Len; j ++) // returns an odd number of characters {If (P [I-j]! = P [I + J]) break; else {If (2 * j + 1> MAX) {max = 2 * j + 1; X = I-j; y = I + J ;}}for (j = 0; I-j> = 0 & I + J + 1 <Len; j ++) // returns an even number of characters {If (P [I-j]! = P [I + J + 1]) break; else {If (2 * j + 2> MAX) {max = 2 * j + 2; X = I-j; y = I + J + 1 ;}}}for (int K = x; k <= y; k ++) // print the return {cout <p [k];} return Max; // return the longest return length} int main (INT argc, char * argv []) {char * P = "abcgooglrcabac"; int Len = strlen (p); int max = getmaxhuiwen (p, Len); cout <Endl; cout <max <Endl; return 0 ;}

2. Unify the even odd numbers

How can we unify it?

We can insert a special character between each character in this way.

For example, the above string: abcgoogleaba. You can perform the following operations: # A # B # C # G # O # G # L # e # A # B # #

In this way, the strings in the reply are all odd, so you do not need to consider the even number.

Int getmaxhuiwenchar (char * P, int Len) {int nlen = 2 * Len + 3; char * STR = new char [nlen]; int I = 0; int Max; int J = 0, x, y; STR [0] = '$'; STR [1] = '#'; For (; I <Len; I ++) // The essence is here {STR [I * 2 + 2] = P [I]; STR [I * 2 + 3] = '#';} STR [nLen-1] = 0; for (I = 0; I <nlen; I ++) {cout <STR [I];} cout <Endl; for (I = 1; I <nlen; I ++) {for (j = 0; I-j> = 0 & I + j <nlen; j ++) {If (STR [I-j]! = STR [I + J]) break; else {If (j> MAX) {max = J; X = I-j; y = I + J ;}}}} for (int K = x + 1; k <= Y-1; k + = 2) // print the return {cout <STR [k];} return Max ;}

Input: abcgottoglrcabac

3. the time complexity of the above practice is still not ideal.

Next we will use a better method O (N)

Int getmaxhuiwencharp (char * s, int Len) {int nlen = 2 * Len + 3; char * STR = new char [nlen]; int I = 0; int max = 0; STR [0] = '$'; STR [1] = '#'; For (; I <Len; I ++) {STR [I * 2 + 2] = s [I]; STR [I * 2 + 3] = '#';} STR [nLen-1] = 0; int * P = new int [nlen]; // defines an int array for marking (I = 1; I <nlen; I ++) {cout <STR [I]; P [I] = 0;} cout <Endl; int id = 0; for (I = 1; I <nlen; I ++) {If (max> I) P [I] = min (P [2 * ID-I], p [ID] + id-I ); // here we define a macro else P [I] = 1; for (; STR [I + P [I] = STR [I-P [I]; P [I] ++) NULL; If (P [I] + I> MAX) {max = P [I] + I; id = I ;}} int MX = 0; for (I = 1; I <nlen; I ++) {If (MX <p [I]-1) MX = P [I]-1;} return MX ;}

Second, Netease game pen questions

According to the method provided above, let's soften it!

Thank you! T provides a better solution!

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.