zoukankan      html  css  js  c++  java
  • 剑指offer-面试题19-正则表达式匹配-字符串

    /*
    题目:
    	实现一个函数用来匹配包含'.'和'*'的正则表达式。
    	'.'表示比配任意字符,‘*’表示匹配0个或多个字符串。
    */
    /*
    思路:
    	采用递归的方法。
    	基础条件:当字符串和模式串存在空的情况。
    	其它情况:考虑模式为'×*’的情况和不为‘×*'的情况。
    */
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #include<stdio.h>
    using namespace std;
    bool coreMatch(char* str,char* pattern){
        //str或pattern有空的情况下
        if(*str == '' && *pattern == ''){
            return true;
        }
        if(*str != '' && *pattern == ''){
            return false;
        }
        if(*str == '' && *pattern != ''){
            if(*(pattern+1) == '*' ){
                return coreMatch(str,pattern+2);
            }
            return false;
        }
        //str和pattern均不空
        if(*(pattern+1) == '*'){
                if(*pattern != *str &&  *pattern != '.'){
                    bool flag = coreMatch(str,pattern+2);
    
                     return flag;
                }else{
                    bool flag = (coreMatch(str,pattern+2) || coreMatch(str+1,pattern) || coreMatch(str+1,pattern+2));
    
                     return flag;
                }
    
        }else if(*str == *pattern || *pattern == '.' ){
                return coreMatch(str+1,pattern+1);
        }
        return false;
    }
    
     bool match(char* str, char* pattern)
    {
        if(str == nullptr || pattern == nullptr){
            return false;
        }
        return coreMatch(str,pattern);
    }
    
    
    int main(){
        cout<<coreMatch("",".*");
    }
    

       

  • 相关阅读:
    【C++基础】重载,覆盖,隐藏
    【Lintcode】003.Digit Counts
    【C++ Primer 5th】Chapter 15
    【Lintcode】120.Word Ladder
    牛客网上的题
    二叉树中和为某个值得路径
    数据库
    二叉搜索树的后序遍历序列
    从上往下打印二叉树
    二叉树的镜像
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/11892361.html
Copyright © 2011-2022 走看看