题目:

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). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

思路:看大神的总结说是递归   反正鄙人完全没思路

解法1:

 /  test cases passed.
Status: Accepted
Runtime: ms
Submitted: minutes ago
    public boolean isMatch(String s, String p) {
if (p.contains(".") || p.contains("*")) {
if (p.length() == 1 || p.charAt(1) != '*')
return comp(s, p, s.length(), 0) && isMatch(s.substring(1), p.substring(1));
for (int i = 0; i == 0 || comp(s, p, s.length(), i - 1); i++) {
if (isMatch(s.substring(i), p.substring(2)))
return true;
}
}
return s.equals(p);
} private boolean comp(String s, String p, int sLen, int i) {
return sLen > i && (p.charAt(0) == s.charAt(i) || p.charAt(0) == '.');
}

解法2:

 /  test cases passed.
Status: Accepted
Runtime: ms
Submitted: minutes ago
public class Solution {
public boolean isMatch(String s, String p) {
if (p.isEmpty()) {
return s.isEmpty();
} if (p.length() == 1 || p.charAt(1) != '*') {
if (s.isEmpty() || (p.charAt(0) != '.' && p.charAt(0) != s.charAt(0))) {
return false;
} else {
return isMatch(s.substring(1), p.substring(1));
}
} //P.length() >=2
while (!s.isEmpty() && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')) {
if (isMatch(s, p.substring(2))) {
return true;
}
s = s.substring(1);
} return isMatch(s, p.substring(2));
}
}

解法3:有穷状态自动机

regular expression is the expression of regregular language, and regregular language can be expressed by a DFA.

I notice that nothing about DFA is talked about in the discuss,so I think I should post my codes to raise this topic.

During building the DFA, there's a small trick to make the code clean.

 /  test cases passed.
Status: Accepted
Runtime: ms
Submitted: minutes ago
public class Solution {
String input;
public boolean isMatch(String s, String p) {
input=s; //----------building DFA------------
Node start=new Node();
Node pre=start; int i=0;
while(i<p.length()){
if(i+1<p.length() && p.charAt(i+1)=='*'){
Node n1=new Node();
Node n2=new Node();
pre.addEdge(new Edge(null,n1));
pre.addEdge(new Edge(null,n2));
n1.addEdge(new Edge(p.charAt(i),n1));
n1.addEdge(new Edge(p.charAt(i),n2));
pre=n2;
i+=2;
}
else{
Node n=new Node();
pre.addEdge(new Edge(p.charAt(i),n));
pre=n;
i++;
}
}
pre.isEnd=true; //----------walking DFA------------- return walk(start,0);
} private boolean walk(Node n,int begin){
if(begin==input.length()){
if(n.isEnd) return true;
else if(n.edges.size()==0) return false;
} for(Edge e:n.edges){
if(e.take==null) {if(walk(e.to,begin)) return true;}
else if(begin<input.length() && e.take=='.') {if(walk(e.to,begin+1)) return true;}
else{
if(begin<input.length() && e.take==input.charAt(begin)) {if(walk(e.to,begin+1)) return true;}
else continue;
}
}
return false;
} //-------------below are just some datastruct to implement DFA------------- private class Node{
List<Edge> edges;
boolean isEnd; Node(){
edges=new ArrayList<Edge>();
} void addEdge(Edge e){
this.edges.add(e);
}
} private class Edge{
Character take;
Node to; Edge(Character c,Node n){
this.take=c;
this.to=n;
}
}
}

Regular Expression Matching2015年6月24日的更多相关文章

  1. SQL PASS将于8月24日在北京中医药大学举办线下活动

    活动主题:复制架构的实现和调优以及SQL Server BI在传统行业的应用 地点:北三环东路11号 北京中医药大学 白色的1号楼教学楼后楼5层511房间 时间:2013年8月24日 9:00-12: ...

  2. 2016年12月24日 星期六 --出埃及记 Exodus 21:19

    2016年12月24日 星期六 --出埃及记 Exodus 21:19 the one who struck the blow will not be held responsible if the ...

  3. 2016年11月24日 星期四 --出埃及记 Exodus 20:15

    2016年11月24日 星期四 --出埃及记 Exodus 20:15 "You shall not steal.不可偷盗.

  4. 2016年10月24日 星期一 --出埃及记 Exodus 19:8

    2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...

  5. 2016年6月24日 星期五 --出埃及记 Exodus 14:21

    2016年6月24日 星期五 --出埃及记 Exodus 14:21 Then Moses stretched out his hand over the sea, and all that nigh ...

  6. 6月24日AppCan移动开发者大会礼品清单遭泄露

    6月24日,第一届AppCan移动开发者大会将在北京国际会议中心举办,大会以”平台之上,应用无限”为主题,全景展现移动应用发展趋势.AppCan 移动技术蓝图及80万开发者的技术实践成果. 大会现场礼 ...

  7. 2018年3月24日上海MVP线下技术交流活动简报

    2018年3月24日下午,几位上海MVP自发组织了一次线下的技术交流会,主要由MVP胡浩牵头,我(陈晴阳).刘鑫.朱兴亮和胡浩各自做了一次主题演讲,具体主题是: 陈晴阳:<这还是我认识的Visu ...

  8. 北京Uber优步司机奖励政策(4月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. 成都Uber优步司机奖励政策(4月24日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

随机推荐

  1. 性能测试培训:批量执行Jmeter脚本之ant调用

    性能测试培训:批量执行Jmeter脚本之ant调用   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的load ...

  2. Oracle DBA管理包脚本系列(二)

    该系列脚本结合日常工作,方便DBA做数据管理.迁移.同步等功能,以下为该系列的脚本,按照功能划分不同的包.功能有如下: 1)数据库对象管理(添加.修改.删除.禁用/启用.编译.去重复.闪回.文件读写. ...

  3. my first blogs(我的处女博)

    末夏的夕阳送走一批批下班的人,些许的轻风给一天烦躁的心带来一丝丝的清凉.我倒是挺喜欢在这种天气,提前下了公交车然后漫步回家.这样我能多点时间回顾一天的事情,俗话说是思考人生. 不知不觉毕业两年多了,在 ...

  4. [笔记]GBDT理论知识总结

    一. GBDT的经典paper:<Greedy Function Approximation:A Gradient Boosting Machine> Abstract Function ...

  5. 第二章 Struts 2的应用

    2.1 Struts 2的应用    2.1.1 使用步骤        1.创建web项目,添加jar包,创建helloWorld.jsp页面        2.创建HelloWorldAction ...

  6. MySQL操作符

    简要介绍MySQL操作符 常用: 算术运算符.比较操作符.逻辑操作符.位运算符-- 一.算术运算符 +:加 -:减 *:乘 /:除,返回商 %,mod():除,返回余数 mysql> %,mod ...

  7. iOS原生地图与高德地图的使用

    原生地图 1.什么是LBS LBS: 基于位置的服务 Location Based Service 实际应用:大众点评,陌陌,微信,美团等需要用到地图或定位的App 2.定位方式 1.GPS定位 2. ...

  8. mvc中razor的一个bug

    具体东西就不多说了,所有编译,代码都是木有问题的. 结果预览页面的时候竟然告诉我编译错误,尼玛这不科学啊. 来看看错误页面 看着问题大概应该是缺少} ,或者多了个} 倒置的编译错误才对,但是编译生成完 ...

  9. js字符串的操作

    js中字符串的使用非常普遍,以下是一些常用的方法和属性,字符串以str='abcdabc'举例. 1.length属性 获取字符串的长度,str.length返回7 2.replace()方法 str ...

  10. javaEE-string家族三大流氓

    最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBuilder的东西,现在整理一下. 关于这三个类在字符串处理中的位置不言而喻,那 ...