zoukankan      html  css  js  c++  java
  • Leetcode_Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'.

    '?' Matches any single character.
    '*' Matches any sequence of characters (including the empty sequence).
    
    The matching should cover the entire input string (not partial).
    
    The function prototype should 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

    class Solution {
    public:
        bool isMatch(const char *s, const char *p) {
           const char * ptr;
           const char *str;
           bool start = false;
           for(ptr=p,str=s; *str!=''  ; ){
               switch(*ptr){
                   case '?':
                   str++,ptr++;
                   break;
                   case '*':
                   start = true;
                   while(*ptr == '*')
                     ptr++;
                   if(*ptr=='')
                   return true;
                   p = ptr;
                   s = str;
                   break;
                   default:
                   if(*ptr != *str){
                       if(!start)
                       return false;
                       ptr = p;
                       str = s+1;
                       s=s+1;
                   }
                  else{
                      ptr++;
                      str++;
                  }
               }
           }
           while(*ptr=='*')  ptr++;
           
           return (*ptr == '');
        }
    };


  • 相关阅读:
    迪杰斯特拉(Dijkstra)算法描述及理解
    KMP初步
    网络流初步
    Cutting Codeforces Round #493 (Div. 2)
    优先队列小结
    树状数组初步理解
    分块思想
    树状数组-逆序对-HDU6318
    线段树
    8.12.5
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6845710.html
Copyright © 2011-2022 走看看