Classical algorithm question -- find the maximum length of countermeasure string

Source: Internet
Author: User

Question: enter a string and output the maximum length of the symmetric substring. If you enter google, output 4.

 

Method 1: The idea is quite satisfactory. traverse this string. If two adjacent characters are found to be equal, cyclically judge whether the two adjacent characters are equal,

Keep down the number of characters that meet the conditions. The maximum number is required.

Method 2: slightly changed on the basis of method 1. The idea is the same, but the loop starts when two adjacent characters are found to be equal,

It compares the two matching characters based on the number of symmetric characters that occurred last time,

If it doesn't wait, it must be loop-free. Do we need the maximum length? Haha...

If the characters are equal, the system loops to the center to determine whether the characters are equal. If the characters are equal, the system exits from the loop. If the characters are equal, the longer length occurs,

We start to cycle outward until the character length is not equal. The final value is the desired value.

 

Method 1

Int counterplan1 (const string str) {int strlen = str. length (); int maxlen = 0; for (int I = 0; I <strlen-1; I ++) {if (str [I] = str [I + 1]) {int start = I-1; int end = I + 2; while (start> = 0 & end <strlen) {if (str [start] = str [end]) {-- start; ++ end;} else {break;} if (maxlen <end-start-1) {maxlen = end-start-1 ;}}} return maxlen;} method 2

Int counterplan2 (const string str) {int strlen = str. length (); int maxlen = 0; for (int I = 0; I <strlen-1; I ++) {if (str [I] = str [I + 1]) {int k = maxlen/2; if (I + k> = strlen) {break;} int start = I-k; int end = I + k + 1; bool isMore = true; while (start <I & end> I) {if (str [start ++]! = Str [end --]) {isMore = false; break ;}} if (! IsMore) {continue;} start = I-k; end = I + k + 1; while (start>-1 & end <strlen) {if (str [start --] = str [end ++]) {maxlen + = 2 ;}}} return maxlen ;}method 2 is a bit messy and needs to be improved, there may be bugs.

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.