-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.cpp
More file actions
53 lines (49 loc) · 1.34 KB
/
10.cpp
File metadata and controls
53 lines (49 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// 10.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/16.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Regular Expression Matching
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
bool isMatch(string s, string p) {
const long lens = s.size(), lenp = p.size();
vector<vector<bool>> matched(lens+1, vector<bool>(lenp+1, false));
matched[0][0] = true;
for (int i = 0; i <= lens; i++) {
for (int j = 1; j <= lenp; j++) {
if (p[j-1] != '*') {
matched[i][j] = i > 0 && matched[i-1][j-1] && (p[j-1] == '.' || s[i-1] == p[j-1]);
}
else {
matched[i][j] = matched[i][j-2] || (i > 0 && (matched[i-1][j] && (p[j-2] == '.' || s[i-1] == p[j-2])));
}
}
}
return matched[lens][lenp];
}
};
class Test {
private:
static void runTest(string s, string p) {
Solution solution;
cout << solution.isMatch(s, p) << endl;
}
public:
void sample() {
string s1("ab"), p1(".*c");
string s2("aab"), p2("c*a*b");
string s3("aaa"), p3("ab*a");
string s4("abcd"), p4("d*");
runTest(s1, p1);
runTest(s2, p2);
runTest(s3, p3);
runTest(s4, p4);
}
};