码迷,mamicode.com
首页 > 其他好文 > 详细

10. Regular Expression Matching

时间:2018-08-10 15:58:31      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:就会   solution   不为   reg   enc   read   and   like   har   

10. Regular Expression Matching

https://www.youtube.com/watch?v=l3hda49XcDE&t=360s

可以大致分为, pattern 的
1. 最后一个char 是个 “。”, 那么取决于t[i-1][j-1]。。 (如果倒数第二个是 “*”, 还是取决于t[i-1][j-1]。  “*” 只对 前面的有影响, 对后面的没影响。然后“。” 对前面的没影响)

2. 最后一个char 是个 “ * ”, 这个又分两种情况
“ * ” 代表的可以是” * “ 之前的数出现0次,  所以取决于 t[i][j-2]


“ * ” 代表的可以是” * “ 之前的数出现不为0次, 这个又分两种情况
看看 pattern 的倒数第二个是什么,

 如果是 “。”,”。” 和 “*” 连在一起就是 一个或者多个“。” (这里注意是在 “*”之前的数出现不为0次 情况之下的 )
一个或者多个“。” 那么起码 只需要看string[i-1]. Pattern 还是看 pattern[j]. 因为pattern 减去 “。” 和 “*” 之后 还是  。*,
 这里表明 0个或者1个或者多个”。“.这里就会从头开始检查所有的判断 。 所以取决于 t[i-1][j]



如果是个普通字符 p, ”p” 和 “*” 连在一起就是, 一个或者多个“普通字符” p. 如果这个p 和 string 最后一位的字符相等, 那么只需要看
string[I-1], pattern[j]. 因为pattern 减去 p 和 “*” 时候 还是  p*, 这里表明 0个或者1个或者多个“普通字符” p.这里就会从头开始检查所有的
判断。 所以取决于 t[i-1][j]


 如果是 “*”,”*” 和 “*” 连在一起就是 0个1个或者多个 “*”, 实际上和有一个 是一样的。 遇到这种
情况就把所有的 “*” 缩成一个“*”。 这个在判断整个字符之前会处理, 所以不用担心这种情况。 




3. 如果最后一个char 不是 “ * ” 也不是 ”。“就是个普通字符 ,看看  pattern 和 string 的 最后一个 char 是否 相等。
 不相等 的话直接 false,

 相等的话, 所以取决于t[i-1][j-1] 

========================================================================================
总结一下就是: 

Equation 1 :   t[I][j] = t[I-1][j-1]           if str[I] = pattern[j] || pattern[j] = ‘.’

Equation 2:   t[I][j] = t[I][j-2 ]              if pattern[j] = “*”   and * means 0 occurrence
            t[I][j] = t[I-1][j ]              if pattern[j] = “*”   and (str[I] == pattern[j-1] || pattern[j-1] == “。”)

Else:
                     t[I][j] = false     

========================================================================================

Ininitalization 
empty string and empty pattern : true 
empty string and pattern  “a*” :  true
empty string and pattern  “。*” :  true

empty string and Others forms of pattern:  false 
empty pattern  and other forms of string  : false


empty string and a* : true
empty string and a*b* :  true。t[0][I] = t[0][I-2]

因为boolean[][] 刚开始default 的 值都是false
所以只要把 true 的 改过来就行了





======
Code 有几点还需要注意, 边界加了padding
 以及 Equation 2:   t[I][j] = t[I][j-2 ]              if pattern[j] = “*”   and * means 0 occurrence
            t[I][j] = t[I-1][j ]              if pattern[j] = “*”   and (str[I] == pattern[j-1] || pattern[j-1] == “。”)
这个地方 先 给  t[I][j] = t[I][j-2 ]   , 然后再 || t[I-1][j]







Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Solution.isMatch(So



class Solution {
    public boolean isMatch(String s, String p ) {
      boolean T[][] = new boolean[s.length() + 1][p.length() + 1];
      
      T[0][0] = true;
      char[] pattern = p.toCharArray();
      char[] string = s.toCharArray();
      
      // empty string and patterns like a* or a*b*.   这里不太懂
      for(int i = 1; i < T[0].length; i++){
        if(pattern[i-1] == ‘*‘){
          T[0][i] = T[0][i-2];
        }
      }
      
      for(int i = 1; i < T.length; i++){  // i = 1 
        for(int j = 1; j < T[0].length; j++){ // j = 1 
          if(pattern[j-1] == string[i-1] || pattern[j-1] == ‘.‘){
            T[i][j] = T[i-1][j-1];
          }else if( pattern[j-1] == ‘*‘){
            T[i][j] = T[i][j-2];
            if(pattern[j-2] == string[i-1] || pattern[i-2] == ‘.‘){
              T[i][j] = T[i][j] || T[i-1][j];
            }
          }else{
            T[i][j] = false;
          }
        }
      }
      return T[s.length()][p.length()];
    }

}

 

10. Regular Expression Matching

标签:就会   solution   不为   reg   enc   read   and   like   har   

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9454964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!