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.