由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 实现regex(.*+)和wildcard(?*)匹配的题
相关主题
C++ Q53: throw (C7)发帖铭记深刻的Java教训。
select2perform上面C++测试挺头疼的问个弱问题:为啥要设立throw exception这种机制呢?
请教 C++ exception 面试问题G家电面
C++ Q39: throw new (C1)实现vector的iterator,template问题
请问大牛们Leetcode Palindrome Number 这道题(思路很简单,就是程序写不对)请教输入可能为null的处理方法
Bloomberg Phone Interview面试中的follow-up 总会问如果这段代码放到production code 中应该怎么改
请教个java exception的问题有经验的看过来
一个c++题(exception handling)王银看kotlin(本文建议零售价 ¥15) (转载)
相关话题的讨论汇总
话题: ptn话题: pidx话题: return话题: ip话题: iidx
进入JobHunting版参与讨论
1 (共1页)
k***t
发帖数: 276
1
网上有标准解吗?谢。
q****x
发帖数: 7404
2
递归。

【在 k***t 的大作中提到】
: 网上有标准解吗?谢。
k***t
发帖数: 276
3
大拿给写个Sample?

【在 q****x 的大作中提到】
: 递归。
k***t
发帖数: 276
4
参照网上的思路,写了一个wildcard match。
做面试用还算简洁。谁给Review一下?
#include
using namespace std;
bool match(char* in, int iidx, char* ptn, int pidx)
{
if (!in || !ptn) return false;
// base case
if (!in[iidx] && !ptn[pidx]) return true;
if (!ptn[pidx]) return false;
if (!in[iidx]) {
while(ptn[pidx]) if (ptn[pidx++]!='*') return false;
return true;
}
if (ptn[pidx]=='?' || ptn[pidx]==in[iidx])
return match(in, iidx+1, ptn, pidx+1);
if (ptn[pidx]=='*') {
while (in[iidx]) {
if (match(in, iidx++, ptn, pidx+1)) return true;
}
while(ptn[++pidx]) if (ptn[pidx]!='*') return false;
return true;
}
return false;
}
int main()
{
char input[] = "regular expression";
char pattern[] = "?egu*pr??*o*";
cout << input << " => " << pattern << " : " <<
match(input, 0, pattern, 0) << endl;
cout << "ab" << " => " << "a" << " : " <<
match("ab", 0, "a", 0) << endl;
}

【在 k***t 的大作中提到】
: 网上有标准解吗?谢。
i**********e
发帖数: 1145
5
请问你的代码是实现 wildcard 还是 regex?
板上之前讨论过。
http://www.mitbbs.com/article_t/JobHunting/31930815.html

【在 k***t 的大作中提到】
: 参照网上的思路,写了一个wildcard match。
: 做面试用还算简洁。谁给Review一下?
: #include
: using namespace std;
: bool match(char* in, int iidx, char* ptn, int pidx)
: {
: if (!in || !ptn) return false;
: // base case
: if (!in[iidx] && !ptn[pidx]) return true;
: if (!ptn[pidx]) return false;

k***t
发帖数: 276
6
我的是.*的wildcard match. 谢谢你的Link.

【在 i**********e 的大作中提到】
: 请问你的代码是实现 wildcard 还是 regex?
: 板上之前讨论过。
: http://www.mitbbs.com/article_t/JobHunting/31930815.html

i**********e
发帖数: 1145
7
我之前制造的一些测试数据(Regular Expression Matching),方便大家用来测试代码:
http://www.leetcode.com/onlinejudge
t****s
发帖数: 1
8
a Java version for regex match as below, let me know if there is any issue.
boolean isMatch(String s, String p) throws Exception{
if (p==null || p.length()==0)
return s==null || s.length()==0;
return isMatch(s,p,0,0);
}
boolean isMatch(String s, String p, int is, int ip) throws Exception{
if (ip>=p.length())
return s==null || is>=s.length();
if (ip==p.length()-1 || p.charAt(ip+1)!='*'){
if (p.charAt(ip)=='*')
throw new Exception("illegal");
if (is>=s.length())
return false;
if (p.charAt(ip)!=s.charAt(is) && p.charAt(ip)!='.')
return false;
return isMatch(s,p,is+1,ip+1);
}
is--;
do{
if (isMatch(s,p,++is,ip+2))
return true;
} while (is (is)));
return false;
}
i**********e
发帖数: 1145
9
Your code is correct.
It got "Time Limit Exceeded" due to this test case,
s = "aaaaaaaaaaaaab", p = "a*a*a*a*a*a*a*a*a*a*a*a*c"
which happens to Java solution but not C++ solution.
I have updated the test case so that a Java recursive solution could pass.
Sorry about that.

【在 t****s 的大作中提到】
: a Java version for regex match as below, let me know if there is any issue.
: boolean isMatch(String s, String p) throws Exception{
: if (p==null || p.length()==0)
: return s==null || s.length()==0;
: return isMatch(s,p,0,0);
: }
: boolean isMatch(String s, String p, int is, int ip) throws Exception{
: if (ip>=p.length())
: return s==null || is>=s.length();
: if (ip==p.length()-1 || p.charAt(ip+1)!='*'){

1 (共1页)
进入JobHunting版参与讨论
相关主题
王银看kotlin(本文建议零售价 ¥15) (转载)请问大牛们Leetcode Palindrome Number 这道题(思路很简单,就是程序写不对)
问一道c++的题Bloomberg Phone Interview
C++ 工程师 电话面试请教个java exception的问题
leecode上的divide two integers问题一个c++题(exception handling)
C++ Q53: throw (C7)发帖铭记深刻的Java教训。
select2perform上面C++测试挺头疼的问个弱问题:为啥要设立throw exception这种机制呢?
请教 C++ exception 面试问题G家电面
C++ Q39: throw new (C1)实现vector的iterator,template问题
相关话题的讨论汇总
话题: ptn话题: pidx话题: return话题: ip话题: iidx