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. javaweb 工程 tomcat启动报错的问你

    2015-03-03 14:39:32,657 INFO (org.springframework.web.context.ContextLoader:296) - Root WebApplicati ...

  2. ADO数据库编程详解(C++)----初级入门篇

    一.概述 ADO即Microsoft ActiveXData Object,是Microsoft继ODBC之后,基于OLE DB技术的一种数据库操作技术,使您能够编写通过 OLE DB提供者对在数据库 ...

  3. 使用CSS设置Chrome打印背景色

    以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...

  4. jsop解析获得htmldome

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  5. java Vamei快速教程01

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是完全面向对象的语言.Java通过虚拟机的运行机制,实现“跨平台”的理念. ...

  6. linux 查看帐号创建时间

    查看用户的home目录的创建时间 查看日志 用stat 命令,可以看到目录的三个时间.不过这个时间只是用来参考的,确定一个范围. 查看日志是最准确的方法 /var/log/auth.log ,前提是你 ...

  7. 问题 F: 等比数列

    问题 F: 等比数列 时间限制: 1 Sec  内存限制: 64 MB提交: 2699  解决: 1214[提交][状态][讨论版][命题人:外部导入] 题目描述 已知q与n,求等比数列之和: 1+q ...

  8. 题解P3951【小凯的疑惑】

    相信参加OI的oiers都是数学高手吧 我好像不是 (滑稽 那应该大家都接触过邮资问题吧! 所谓邮资问题,就类似于这一题,给定a和b两种邮资数,求最大的不能凑出的邮资 数.这里给出公式:最大的不能集出 ...

  9. Linux学习记录(三)

    1.Linux的软件安装 1.1.jdk安装 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看当前Linux系统是否已经安装java 输入 rpm -qa | g ...

  10. JS位运算和遍历

    JS位运算符 整数 有符号整数:允许使用正数和负数,第32位作为符号位,前31位才是存储位 无符号整数:只允许用正数 如果用n代表位 位数 = 2^n-1 由于位数(1.2.4.8.16...)中只有 ...