由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - regex 用DP做对不对啊?
相关主题
正则的题请教:string pattern match 题
面试遇到了Regular Expression Matching时间复杂度是多少?问道题string pattern match的题目
java没有指针真麻烦Regular expression matching 在什么输入下时间复杂度是O(2^n)?
一道leetcode变种,twitter常考,怎么做?问个facebook的题目
leetcode regular expression match的问题leetcode里最弄不明白的两道题
问一道题(8)T家店面
regex matching algorithm问一个C++ set和unordered_set iterator的问题
关于wildcard match和regex match的一个问题String Pattern Matching problems
相关话题的讨论汇总
话题: match话题: pattern话题: char话题: string话题: dp
进入JobHunting版参与讨论
1 (共1页)
h******8
发帖数: 55
1
The basic idea is fill in a table for a matching path. If the last pair
matches, the whole string will match.
match[i][j] is the array (to match string[i] with pattern[j]), which is of (
string length + 1)*(pattern length + 1).
match[0][*] and match[*][0] is padded with false.
Then the function is something like:
match[i][j] =
1. if pattern char='.', match[i][j]=match[i-1][j-1]
2. if pattern char='*', match[i][j]=match[i][j-1] //match 0 character
||match[i-1][j] //match any character
3. if pattern char=string char, match[i][j]=match[i-1][j-1]
4. otherwise, not matched.
大家看看对不对?
g**********t
发帖数: 475
2
不对。
match[*][0]要初始化为true,因为任何字符串都匹配空模式。这是用来“起头”用的。
match[0][1:n]要初始化为false,因为空字符串不匹配任何非空模式。这用来保证匹配
是完整的。
其他的条件没来的及看。
h******8
发帖数: 55
3
我的意思是好像可以用DP做,为啥大家都用recursion?

的。

【在 g**********t 的大作中提到】
: 不对。
: match[*][0]要初始化为true,因为任何字符串都匹配空模式。这是用来“起头”用的。
: match[0][1:n]要初始化为false,因为空字符串不匹配任何非空模式。这用来保证匹配
: 是完整的。
: 其他的条件没来的及看。

n*******w
发帖数: 687
4
如果像这么定义 .和* 可以用DP。
很多定义是不能DP的,比如
*表示匹配前一个字符任意次
+表示匹配前一个字符至少一次
a-z表示匹配a-z之间任意字符
。。。
有一些不能DP的正则式定义,因为DP的前提是make a choice之后的子问题跟choice本
身没有dependency。
另外正则式搞复杂了,面试不一定有时间用DP写白板。一个面试session 45分钟,一般
3道coding,至少也得2道。时间还是紧张了。当然牛人除外。

(

【在 h******8 的大作中提到】
: The basic idea is fill in a table for a matching path. If the last pair
: matches, the whole string will match.
: match[i][j] is the array (to match string[i] with pattern[j]), which is of (
: string length + 1)*(pattern length + 1).
: match[0][*] and match[*][0] is padded with false.
: Then the function is something like:
: match[i][j] =
: 1. if pattern char='.', match[i][j]=match[i-1][j-1]
: 2. if pattern char='*', match[i][j]=match[i][j-1] //match 0 character
: ||match[i-1][j] //match any character

j********2
发帖数: 82
5
没错。DP只能用来解wildcard,不能解 regex.即使是wildcard,也比递归要难写(反正
我肯定当场白板写不出来)。

【在 n*******w 的大作中提到】
: 如果像这么定义 .和* 可以用DP。
: 很多定义是不能DP的,比如
: *表示匹配前一个字符任意次
: +表示匹配前一个字符至少一次
: a-z表示匹配a-z之间任意字符
: 。。。
: 有一些不能DP的正则式定义,因为DP的前提是make a choice之后的子问题跟choice本
: 身没有dependency。
: 另外正则式搞复杂了,面试不一定有时间用DP写白板。一个面试session 45分钟,一般
: 3道coding,至少也得2道。时间还是紧张了。当然牛人除外。

h******8
发帖数: 55
6
这个不对吧。* in regex can also be done in the following way
if *(pat+1) == ‘*’, regex_match(str, pat) =
• regex_match(str, pat+2) OR // match 0 occurrence – “ab”, “a*
ab”
• regex_match(str+1, pat) if equal_char(*str, *pat) // match 1
occurrence – “aab”, “a*b”
And the above can be achieved with DP:
match[i][j] is the array (to match string[i] with pattern[j]), which is of (
string length + 1)*(pattern length + 1).
Initialize:
match[0][0] = true
match[*][0] = false if str is not empty
match[0][*] = pattern is “a*b*.*”? true:false
Then the function is something like:
match[i][j] =
1. if pattern char='.', match[i][j]=match[i-1][j-1]
2. if pattern char='*', match[i][j]=match[i][j-2] //match 0 character –
to pass “a*”
|| match[i-1][j] if str[i] = pat[j-1] // match 1 occurrence – “aab”, “a*
b”
3. if pattern char=string char, match[i][j]=match[i-1][j-1]
4. otherwise, not matched.

【在 n*******w 的大作中提到】
: 如果像这么定义 .和* 可以用DP。
: 很多定义是不能DP的,比如
: *表示匹配前一个字符任意次
: +表示匹配前一个字符至少一次
: a-z表示匹配a-z之间任意字符
: 。。。
: 有一些不能DP的正则式定义,因为DP的前提是make a choice之后的子问题跟choice本
: 身没有dependency。
: 另外正则式搞复杂了,面试不一定有时间用DP写白板。一个面试session 45分钟,一般
: 3道coding,至少也得2道。时间还是紧张了。当然牛人除外。

1 (共1页)
进入JobHunting版参与讨论
相关主题
String Pattern Matching problemsleetcode regular expression match的问题
没人讨论热门帖子里的两道概率题?问一道题(8)
GOOG intern interview 题目regex matching algorithm
amazon 找电话号码题一问关于wildcard match和regex match的一个问题
正则的题请教:string pattern match 题
面试遇到了Regular Expression Matching时间复杂度是多少?问道题string pattern match的题目
java没有指针真麻烦Regular expression matching 在什么输入下时间复杂度是O(2^n)?
一道leetcode变种,twitter常考,怎么做?问个facebook的题目
相关话题的讨论汇总
话题: match话题: pattern话题: char话题: string话题: dp