Careercup - Microsoft面试题 - 4639756264669184
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的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- vue-elem-配置静态模拟数据访问接口
使用本地mock数据模拟真实数据配置 static/data.json dev.server.js中 var app=express();之后添加以下代码, var appData=require(' ...
- JAVA方法定义和调用
类的方法代表的是实例的某种行为或功能 定义类的方法 访问修饰 类型 方法名(参数列表){ //方法体 } 1.把方法当作一个模块,是个“黑匣子”,完成某个特定的功能,并返回处理结果 2.方法分类“ 返 ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:7.项目介绍之架构(1)
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 <迷你微信>服务器端是使用Java语言,Mina框架编写的,一个良好的架构关系到后期迭代的方便程度 ...
- Winform调整DEV控件高度
- sk-learning(2)
sk-learning 学习(2) sklearing 训练评估 针对kdd99数据集使用逻辑回归分类训练 然后进行评估 发觉分数有点高的离谱 取出10%数据494021条,并从中选择四分之一作为测试 ...
- zip、rar压缩文件密码破解——使用ARCHPR Professional Edition
直链下载地址: https://pan.abn.cc/weiyun/down.php?u=82441366e3c1f43fc69210e8ece93470.undefined.zip (压缩包内含解压 ...
- IOS UIImageView的帧动画
● UIImageView可以让一系列的图片在特定的时间内按顺序显示 ● 相关属性解析: ● animationImages:要显示的图片(一个装着UIImage的NSArray) ● animati ...
- UVA 11404 Plalidromic Subsquence (回文子序列,LCS)
最长回文子序列可以用求解原串s和反转串rv的LCS来得到,因为要求回文串分奇偶,dp[i][j]保存长度, 要求字典序最小,dp[i][j]应该表示回文子序列的端点,所以边界为单个字符,即i+j=le ...
- 【HDU1542】Atlantis (扫描线的经典运用)
点此看题面 大致题意: 给你\(N\)个矩形,请你求出它们覆盖的面积(重叠的面积只算一次). 扫描线 这道题是一道典型的求矩形面积并问题,是扫描线的一个经典运用.这里就不赘述了. 代码 #includ ...
- 2018.5.17 oracle函数查询
--*********函数*********** --1.显示当前日期 select sysdate from dual; --2.显示当前日期,格式为****年月日,别名为hday select t ...