[LeetCode](week11)Regular Expression Matchin

今天尝试做一道正则表达式判断的String题目, 同时温习一下C语言的使用

题目

Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

题解

这是一段错误的代码, 其思维是线性的; 而在编译原理学正则表达式的时候,我们往往需要构建分析树, 这说明一个语言(这里当然不是指编程语言)在做正则化判断时候不能单纯线性地去考虑, 而且树的形状也应该很容易想到递归才是.

bool isMatch(char* s, char* p) {
    char last = 0;
    while(*p != 0){
        if(*p == '*'){
            if(*s == last || last == '.')
                p--;
            else
                s--;
        }
        else if(*(p+1) == '*'){
            last = *p;
            s--;
        }
        else if(*p != '.' && *p != *s){
            break;
        }
        p++;
        s++;
        
    }
    
    return (*p == 0 && *s == 0);
}

第二版本的代码如下, 采用了递归, 通过了(在几次试错之下)

bool isMatch(char* s, char* p) {
    
    // printf("test: [%s] [%s]\n", s, p);
    
    if(*p == 0 && *s == 0) return true;

    else if(*(p+1) == '*'){
        if(isMatch(s, p+2)) return true;
        while(*s == *p || (*p == '.' && *s != 0)){
            if(isMatch(++s,p+2)) return true;
        }
    }
    else if(*s == *p || (*p == '.' && *s != 0)){
        return isMatch(s+1, p+1);
    }
    
    return false;
}

万万没想到: 这题也是可以用动态规划来做,而且效率很高:

解析 (这个思路真的挺有深度的,值得多次回顾)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,181评论 0 10
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,755评论 0 38
  • 金子涵: 你好! 时光飞逝,一眨眼我们就三年没见了,你在别的学校还好吗?过得还踏实吗? 金子涵,...
    潘仪宸阅读 1,261评论 0 2
  • 好像总是期望能一次性解决所有问题。 读一本书,以找到生命的意义;看一场电影,以发现爱情的真谛;找一份工作,以赚来生...
    一条安静的鱼阅读 1,841评论 0 0

友情链接更多精彩内容