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. visual studio2010中C#生成的,ArcGIS二次开发的basetool的dll,注册为COM组件tlb文件,并在arcmap中加载使用

    写了个标题好长啊~~~~ 这两天又认识了一个新玩意,记录一下下,啦啦啦~~~~~ 话说,认识arcgis快十年了,从桌面版到engine的二次开发,其实不过才认识到它的冰山一角, 它总是能带来很多还未 ...

  2. iOS界面设计切图小结

    iOS界面设计切图小结 APR 12TH, 2013 1.基本尺寸 (1)界面 实际设计时按: iPhone4.4s:640px*960px iPhone5: 640px*1136px iPad:15 ...

  3. 棋盘V(最小费用最大流)

    棋盘V 时间限制: 1 Sec  内存限制: 128 MB提交: 380  解决: 44[提交] [状态] [讨论版] [命题人:admin] 题目描述 有一块棋盘,棋盘的边长为100000,行和列的 ...

  4. python_24_test

    product_list=[ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Py ...

  5. 【转帖】Linux mount 域控权限的共享目录

    https://www.linuxidc.com/Linux/2012-09/71388.htm 之前一直以为没法 映射 home 域的内容 其实还有一个地方.. 注意 空格的话 需要用 \ 进行转移 ...

  6. C# 创建子目录

    运用DirectoryInfo类创建子目录是非常容易的,你只要调用其中CreateSubdirectory()方法即可,演示代码如下. DirectoryInfo dir = new Director ...

  7. indexOf和contains查找的字符串是空字符,返回值是什么呢?

    一直以为indexOf方法查找的字符串如果不匹配返回值就是-1.今天发现空字符返回值是0.看源码原来如此,阴沟里翻船啊!

  8. nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument解决

    先附上错误信息: (myblog) root@Dapeng:/home/uwsgi# service nginx status ● nginx.service - A high performance ...

  9. [mysql] Can't read from messagefile

    系统:windows 重启mysql服务出现 Server] Can't read from messagefile 等错误时候, 应先执行 mysqld --initialize-insecure ...

  10. tcl之string操作