zoukankan      html  css  js  c++  java
  • Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'.

    '.' Matches any single character.
    '*' Matches zero or more of the preceding element.
    
    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", "a*") → true
    isMatch("aa", ".*") → true
    isMatch("ab", ".*") → true
    isMatch("aab", "c*a*b") → true

    思路:

    代码:

     1     bool isMatch(const char *s, const char *p) {
     2         // IMPORTANT: Please reset any member data you declared, as
     3         // the same Solution instance will be reused for each test case.  
     4         if(*p == '')
     5             return *s == '';
     6         if(*(p+1) == '*'){
     7             for(const char *t = s; *t != '' && (*t == *p || *p == '.'); t++){
     8                 if(isMatch(t+1, p+2))
     9                     return true;
    10             }
    11             return isMatch(s, p+2);
    12         }
    13         else{
    14             if(*s != '' && (*s == *p || *p == '.'))
    15                 return isMatch(s+1, p+1);
    16             return false;
    17         }
    18     }
  • 相关阅读:
    CF785E Anton and Permutation
    P4054 [JSOI2009]计数问题
    P4396 [AHOI2013]作业
    AD PCB中各层的含义
    AD21 使用手册 快捷键(二)
    AD 汉化和界面恢复
    AD21 使用手册 快捷键(一)
    TINA-TI 安装
    B站下载 视频
    restful-work基本组件
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3441863.html
Copyright © 2011-2022 走看看