Analysis: * can match any character, including more than 0 consecutive characters. * is equivalent to 1 *. * If there are no other characters, * p is * and * s is *, we have two options: Skip * p *, that is to say, the * matches 0 characters and continues backward matching.
One is that we need to use * to match multiple characters to complete the matching.
* If there are other characters, find the character that matches the non-* character backward in the s string. If not found, the character does not match. If found, it may be different. It is certainly good to use the first non-* character in s to match the * character in p. Let's look at another situation. 2. The non-* character that is first encountered in S and * in p cannot match it. After the first matching of e in s and e in p, there is a mismatch in the Process of continued matching, but in fact the s string and p match, that is to say, to continue expanding the * scope, match e. However, we found that p and s both run, p points to g, and s points to the Second e. Therefore, you need to record the position of the Non-* characters after the *, so that the matching can be re-performed when the * does not match in the future.
Implementation:
Bool isMatch (const char * s, const char * p) {const char * backtrack_s = NULL; const char * backtrack_p = NULL; while (* s) {// match if (* p = '? '| * S = * p) {++ s; ++ p;} // don't match. else {// meet * if (* p = '*') {// skip multiply *. while (* p = '*') + + p; if (* p = '\ 0') return true; // record the next position *. backtrack_s = s; backtrack_p = p;} // else {// Note: Check whether * if (backtrack_p) appears before. {// note: when an inequality occurs in the current position, return to the next position and compare it again. // its significance is to continue to expand. S = ++ backtrack_s; p = backtrack_p; // restore the position of p} // The position does not match, but does not match. Else return false ;}}// end whilewhile (* p = '*') // process * ++ p at the end of p; return (* s = '\ 0' & * p =' \ 0 ');}