[LeetCode] [C ++] Wildcard Matching, leetcodewildcard

Source: Internet
Author: User

[LeetCode] [C ++] Wildcard Matching, leetcodewildcard
Question

'? 'Matches any single character.
'*' Matches any sequence of characters (including the empty sequence ).

The matching shocould cover the entire input string (not partial ).

The function prototype shocould be:
Bool isMatch (const char * s, const char * p)

Some examples:
IsMatch ("aa", "a") → false
IsMatch ("aa", "aa") → true
IsMatch ("aaa", "aa") → false
IsMatch ("aa", "*") → true
IsMatch ("aa", "a *") → true
IsMatch ("AB", "? * ") → True
IsMatch ("aab", "c * a * B") → false

Ideas

This is very similar to the Regular Expression Matching in question 10, except that the question '*' can represent any string, and the question 10 can only represent several first letters. For example, in this question, * can represent a, AB, abdfefa, or empty. However, in question 10, * cannot appear independently. It must form an expression with the letters above it. For example, a * Indicates several a, that is, a, aa, aaa, or empty.
Still use DP, set a vectorre (n + 1), store the matching condition of the first string to this position, and traverse from the second string. * special processing is required, for each digit of string 2, you must search for the matching position from string 1 and mark the corresponding position as true. The status of each position is determined by the previous position. That is to say, the previous position does not match. After the traversal ends, the matching results at the last position are output.

Code
Class Solution {public: bool isMatch (const char * s, const char * p) {int m = strlen (s); int n = strlen (p ); if (n = 0) return m = 0; if (m> 30000) return false; // if this sentence does not exist, there is a case: aaaaaa is mostly... Vector <bool> re (m + 1, false); re [0] = true; // match by default, because it is possible that p [0] = * for (int I = 0; I <n; I ++) {if (p [I] = '*') {// determines whether it is *. * special processing is required for (int k = 0; k <m; k ++) {re [k + 1] = re [k + 1] | re [k]; // if it is already true, it continues to be true; otherwise, it depends on the previous digit. } Else {for (int j = m; j> 0; j --) {// depending on the previous digit, and requires this matching re [j] = re [J-1] & (p [I] = s [J-1] | p [I] = '? ') ;}} Re [0] = re [0] & p [I] =' * '; // if this location is not *, then the zeroth digit cannot be true} return re [m] ;}};

Reprinted please indicate the source: http://blog.csdn.net/monkeyduck
Leave a message.

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.