-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternMatchingDB.cpp
More file actions
68 lines (58 loc) · 1.97 KB
/
PatternMatchingDB.cpp
File metadata and controls
68 lines (58 loc) · 1.97 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
using namespace std;
bool dfs(map<char, string> &m, set<string>& s, int start_p, int start_t,
const string& P, const string& T) {
if (start_p == P.length() && start_t == T.length()) {
return true;
}
if (m.find(P[start_p]) != m.end()) {
if (T.length() - start_t < m[P[start_p]].length()) {
return false;
} else {
//match the substring with the string in the map
if (T.substr(start_t, m[P[start_p]].length()) != m[P[start_p]]) {
return false;
} else {
if (dfs(m, s, start_p + 1, start_t + m[P[start_p]].length(), P, T)) {
return true;
}
}
}
} else {
for (int i = start_t + 1; i <= T.length(); i++) {
if (s.find(T.substr(start_t, i - start_t)) == s.end()) {
m[P[start_p]] = T.substr(start_t, i - start_t);
s.insert(T.substr(start_t, i - start_t));
if (dfs(m, s, start_p + 1, i, P, T)) {
return true;
}
s.erase(T.substr(start_t, i - start_t));
m.erase(m.find(P[start_p]));
}
}
}
return false;
}
bool isMatch(string P, string T) {
if (T.length() < P.length() || (
P.length() == 0 && T.length() != 0)) {
return false;
}
map<char, string> m;
set<string> used;
return dfs(m, used, 0, 0, P, T);
}
int main(int argc, char *argv[])
{
string P[] = {"abba", "abba", "aaaa", "abba", ""};
string T[] = {"redbluebluered", "redblueyellowred",
"redredredred", "redredredred", "abc"};
for (int i = 0; i < 5; i ++) {
cout << isMatch(P[i], T[i]) << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}