TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
wildcard_matching.cpp
Go to the documentation of this file.
1
13
14
#include <cassert>
15
#include <cstdint>
16
#include <iostream>
17
#include <vector>
18
23
namespace
backtracking
{
29
namespace
wildcard_matching
{
39
std::vector<std::vector<int64_t>>
dpTable
(1000, std::vector<int64_t>(1000, -1));
40
bool
wildcard_matching
(std::string s, std::string p, uint32_t pos1,
41
uint32_t pos2) {
42
uint32_t n = s.length();
43
uint32_t m = p.length();
44
// matching is successfull if both strings are done
45
if
(pos1 == n && pos2 == m) {
46
return
true
;
47
}
48
49
// matching is unsuccessfull if pattern is not finished but matching string
50
// is
51
if
(pos1 != n && pos2 == m) {
52
return
false
;
53
}
54
55
// all the remaining characters of patterns must be * inorder to match with
56
// finished string
57
if
(pos1 == n && pos2 != m) {
58
while
(pos2 < m && p[pos2] ==
'*'
) {
59
pos2++;
60
}
61
62
return
pos2 == m;
63
}
64
65
// if already calculted for these positions
66
if
(
dpTable
[pos1][pos2] != -1) {
67
return
dpTable
[pos1][pos2];
68
}
69
70
// if the characters are same just go ahead in both the string
71
if
(s[pos1] == p[pos2]) {
72
return
dpTable
[pos1][pos2] =
73
wildcard_matching(s, p, pos1 + 1, pos2 + 1);
74
}
75
76
else
{
77
// can only single character
78
if
(p[pos2] ==
'?'
) {
79
return
dpTable
[pos1][pos2] =
80
wildcard_matching(s, p, pos1 + 1, pos2 + 1);
81
}
82
// have choice either to match one or more charcters
83
else
if
(p[pos2] ==
'*'
) {
84
return
dpTable
[pos1][pos2] =
85
wildcard_matching(s, p, pos1, pos2 + 1) ||
86
wildcard_matching(s, p, pos1 + 1, pos2);
87
}
88
// not possible to match
89
else
{
90
return
dpTable
[pos1][pos2] = 0;
91
}
92
}
93
}
94
95
}
// namespace wildcard_matching
96
}
// namespace backtracking
97
102
static
void
test
() {
103
// 1st test
104
std::cout <<
"1st test "
;
105
std::string matching1 =
"baaabab"
;
106
std::string pattern1 =
"*****ba*****ab"
;
107
assert(backtracking::wildcard_matching::wildcard_matching(matching1,
108
pattern1, 0, 0) ==
109
1);
// here the pattern matches with given string
110
std::cout <<
"passed"
<< std::endl;
111
112
// 2nd test
113
std::cout <<
"2nd test "
;
114
std::string matching2 =
"baaabab"
;
115
std::string pattern2 =
"ba*****ab"
;
116
assert(backtracking::wildcard_matching::wildcard_matching(matching2,
117
pattern2, 0, 0) ==
118
1);
// here the pattern matches with given string
119
std::cout <<
"passed"
<< std::endl;
120
121
// 3rd test
122
std::cout <<
"3rd test "
;
123
std::string matching3 =
"baaabab"
;
124
std::string pattern3 =
"ba*ab"
;
125
assert(backtracking::wildcard_matching::wildcard_matching(matching3,
126
pattern3, 0, 0) ==
127
1);
// here the pattern matches with given string
128
std::cout <<
"passed"
<< std::endl;
129
130
// 4th test
131
std::cout <<
"4th test "
;
132
std::string matching4 =
"baaabab"
;
133
std::string pattern4 =
"a*ab"
;
134
assert(backtracking::wildcard_matching::wildcard_matching(matching4,
135
pattern4, 0, 0) ==
136
1);
// here the pattern matches with given string
137
std::cout <<
"passed"
<< std::endl;
138
139
// 5th test
140
std::cout <<
"5th test "
;
141
std::string matching5 =
"baaabab"
;
142
std::string pattern5 =
"aa?ab"
;
143
assert(backtracking::wildcard_matching::wildcard_matching(matching5,
144
pattern5, 0, 0) ==
145
1);
// here the pattern matches with given string
146
std::cout <<
"passed"
<< std::endl;
147
}
148
153
int
main
() {
154
test
();
// run self-test implementations
155
return
0;
156
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
backtracking
for vector container
wildcard_matching
Functions for the Wildcard Matching problem.
backtracking::wildcard_matching::dpTable
std::vector< std::vector< int64_t > > dpTable(1000, std::vector< int64_t >(1000, -1))
The main function implements if pattern can be matched with given string.
backtracking
wildcard_matching.cpp
Generated by
1.18.0