由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - wildcard matching 大case runtime error
相关主题
leetcode regular expression match的问题Wildcard Matching题求助
已经用了dp,我的wildcard怎么还是过不了大ojWildcard Matching 和 Regular Expression Matching 区别是什么
wildcard string matching,谁有最简洁的非递归解法?请问大牛们关于Regular expression matching
leetcode上wild match问个Zenefits电面题目,他家好难。。。
贡献个regular expression DP解法贴几道某大公司的面试题
实现regex(.*+)和wildcard(?*)匹配的题请教一个字符串比较排序的问题 (转载)
Leetcode Timeout刚做了一道题挺有意思
一道字符串题目做了一下scramble string
相关话题的讨论汇总
话题: offset话题: start话题: int话题: dp话题: lenp
进入JobHunting版参与讨论
1 (共1页)
b*******m
发帖数: 10
1
如下代码,大case 会runtime error 这是什么原因?
哪个大牛帮忙看下
bool isMatch(const char *s, const char *p) {
int lens = strlen(s);
int lenp = strlen(p);
vector> dp(lens+1, vector(false, lenp+1));
dp[lens][lenp] = true;
for (int i = lenp-1; i >= 0; i--) {
if (p[i] == '*' && dp[lens][i+1]) {
dp[lens][i] = true;
} else {
break;
//dp[len][i] = false;
}
}
for (int i = lenp-1; i >= 0; i--) {
for (int j = lens-1; j >= 0; j--) {
if (s[j] == p[i] || p[i] == '?') {
dp[j][i] = dp[j+1][i+1];
} else if (p[i] == '*') {
for (int k = j+1; k <= lens; k++) {
if (dp[k][i+1] == true) {
dp[j][i] = true;
break;
}
}
}
}
}
return dp[0][0];
}
b******g
发帖数: 14
2
This problem has a trick. Depending on the size of s and p, you can rule out
the possibility of the match without matching them.
s***e
发帖数: 403
3
我对这个的理解是
把pattern按照*分开成子串
然后按照顺序依次搜索
在开头和最后的串要特别调整一下.
class Solution {
public:
#define charMatch(s, p) (p == '?' || p == s)
int subStr (const char* s, int start, const char* p, int len)
{
while (s[start] != 0)
{
if (charMatch (s[start], p[0]))
{
bool match = true;
for (int j = 1; j < len; ++j)
if (s[start + j] == 0)
return -1;
else if (!charMatch (s[start + j], p[j]))
{
match = false;
break;
}

if (match)

return start + len;

}



++start;

}

return -1;

}

#undef charMatch



bool isMatch (const char* s, const char* p)

{

bool front_vary = *p == '*';

bool back_vary = false;

int s_offset = 0;
int p_start = 0;
int p_offset = 0;
while (p[p_offset] != 0)
{
p_start = p_offset;
while (p[p_start] == '*' && p[p_start] != 0) ++p_start;

if (p[p_start] == 0)
{
// is pattern empty?
if (p_start != 0)
back_vary = p[p_start - 1] == '*';
break;
}

p_offset = p_start + 1;
while (p[p_offset] != '*' && p[p_offset] != 0) ++p_offset;

if (p[p_offset] == 0 && p_start != 0)
{
// for the last block we need to do matching backwards
int old_soffset = s_offset;
while (s[s_offset] != 0) ++s_offset;

int pi = p_offset;
int si = s_offset;
while(pi >= p_start && si >= old_soffset)
{
if (p[pi] != '?' && p[pi] != s[si])
return false;
--pi, --si;
}

if (pi >= p_start)
return false; // s does not have enough space
}
else
{
s_offset = subStr (s, s_offset, p + p_start, p_offset -
p_start);

if (s_offset == -1 || (p_start == 0 && !front_vary && s_
offset != p_offset - p_start))
return false;
}
}

if (!back_vary && s[s_offset] != 0)
return false;
else
return true;
}
};
1 (共1页)
进入JobHunting版参与讨论
相关主题
做了一下scramble string贡献个regular expression DP解法
LeetCode Scramble String 疑问实现regex(.*+)和wildcard(?*)匹配的题
longest common prefix 和 longest common substringLeetcode Timeout
问大牛们一个Leetcode上的题一道字符串题目
leetcode regular expression match的问题Wildcard Matching题求助
已经用了dp,我的wildcard怎么还是过不了大ojWildcard Matching 和 Regular Expression Matching 区别是什么
wildcard string matching,谁有最简洁的非递归解法?请问大牛们关于Regular expression matching
leetcode上wild match问个Zenefits电面题目,他家好难。。。
相关话题的讨论汇总
话题: offset话题: start话题: int话题: dp话题: lenp