2014-05-12 06:42

题目链接

原题:

Write your own regular expression parser for following condition: 

az*b can match any string that starts with and ends with b and  or more Z's between. for e.g. azb, azzzb etc. 

a.b can match anything between a and b e.g. ajsdskjb etc. 

Your function will have to parameters: Input String and Regex. Return true/false if the input string satisfies the regex condition. Note: The input string can contain multiple regex. For e.g. az*bc.g

题目:实现正则表达式中的“*”和“.”功能,不过题目中给定的“.”实际上是“.*”。

解法:Leetcode上有这题,所以我估计是这题的出题者自己记错了,所以说错了“.”的意义。我的Leetcode题解在此:LeetCode - Regular Expression Matching

代码:

 // http://www.careercup.com/question?id=4639756264669184
#include <cstring>
#include <vector>
using namespace std; class Solution {
public:
bool isMatch(const char *s, const char *p) {
int i, j;
int ls, lp;
vector<int> last_i_arr;
vector<int> last_j_arr; if (s == nullptr || p == nullptr) {
return false;
} ls = strlen(s);
lp = strlen(p);
if (lp == ) {
// empty patterns are regarded as match.
return ls == ;
} // validate the pattern string.
for (j = ; j < lp; ++j) {
if (p[j] == '*' && (j == || p[j - ] == '*')) {
// invalid pattern string, can't match.
return false;
}
} int last_i, last_j; i = j = ;
last_i = -;
last_j = -;
while (i < ls) {
if (j + < lp && p[j + ] == '*') {
last_i_arr.push_back(i);
last_j_arr.push_back(j);
++last_i;
++last_j;
j += ;
} else if (p[j] == '.' || s[i] == p[j]) {
++i;
++j;
} else if (last_j != -) {
if (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]]) {
// current backtracking position is still available.
i = (++last_i_arr[last_i]);
j = last_j_arr[last_j] + ;
} else if (last_j > ) {
while (last_j >= ) {
// backtrack to the last backtracking point.
--last_i;
--last_j;
last_i_arr.pop_back();
last_j_arr.pop_back();
if (last_j >= && (p[last_j_arr[last_j]] == '.' || s[last_i_arr[last_i]] == p[last_j_arr[last_j]])) {
i = (++last_i_arr[last_i]);
j = last_j_arr[last_j] + ;
break;
}
}
if (last_j == -) {
return false;
}
} else {
// no more backtracking is possible.
return false;
}
} else {
return false;
}
} while (j < lp) {
if (j + < lp && p[j + ] == '*') {
j += ;
} else {
break;
}
} last_i_arr.clear();
last_j_arr.clear();
return j == lp;
}
};

Careercup - Microsoft面试题 - 4639756264669184的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. js 数组操作常用方法

    push():在数组后面加入元素,并返回数组的长度: unshift():在数组前面就如元素,并返回数组的长度: pop():删除最后一个元素: var arr =[1,2,3,4,5] ; arr. ...

  2. ionic 2 起航 控件的使用 客户列表场景(三)

    我们来看看客户列表的搜索控件是怎么工作的吧. 1.打开customer.html <ion-content> <ion-searchbar [(ngModel)]="sea ...

  3. Jquery删除table里面checkbox选中的多个行

    自己闲来无聊,写了一篇关于jq选中复选框删除数据的一个功能,不足之处,还望多多包涵 js代码 <script type="text/javascript" src=" ...

  4. Team Foundation 版本控制

    与 Visual Studio 的一流集成. 使用富文件和文件夹差异工具突出显示代码更改. 借助强大的可视化跨分支跟踪代码更改. 集成的代码评审工具有助于在签入代码之前获得反馈. 使用托管版本或本地版 ...

  5. ARM实验1 —— 流水灯实验

    实验内容: 编写GPIO模块程序,实现对FS_4412平台的上的led2,led3,led4 ,led5,的流水灯实现. 实验目的: 熟悉开发环境的使用. 掌握Exynos 4412处理器GPIO功能 ...

  6. [转载]AngularJS入门教程00:引导程序

    我们现在开始准备编写AngularJS应用——phonecat.这一步骤(步骤0),您将会熟悉重要的源代码文件,学习启动包含AngularJS种子项目的开发环境,并在浏览器端运行应用. 进入angul ...

  7. Python风格规范-FYI

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  8. Java代码工具箱之解析单行单列简单Excel

    1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...

  9. 切换Ubuntu超级管理员

    对Ubuntu进行拷贝命令时,如果不是root用户,会出现权限不足的情况,无法操作

  10. 记python版本管理--pyenv

    随记: 众所周知,python2.x版本与3.x版本有比较大的区别,如果你是2.x版本的使用者,突然接了3.x版本的项目,或者反过来,遇到这种情况该怎么办呢?重新安装自己电脑上的python,来匹配对 ...