Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
wildcard_matching.cpp File Reference

Implementation of the Wildcard Matching problem. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for wildcard_matching.cpp:

Namespaces

namespace  backtracking
 for vector container
 
namespace  wildcard_matching
 Functions for the Wildcard Matching problem.
 

Functions

bool backtracking::wildcard_matching::wildcard_matching (std::string s, std::string p, uint32_t pos1, uint32_t pos2)
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

std::vector< std::vector< int64_t > > backtracking::wildcard_matching::dpTable (1000, std::vector< int64_t >(1000, -1))
 The main function implements if pattern can be matched with given string.
 

Detailed Description

Implementation of the Wildcard Matching problem.

Given a matching string and a pattern, implement wildcard pattern matching with support for ? and *. ? matches any single character. * matches any sequence of characters (including the empty sequence). The matching should cover the entire matching string (not partial). The task is to determine if the pattern matches with the matching string

Author
Swastika Gupta

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
152 {
153 test(); // run self-test implementations
154 return 0;
155}
static void test()
Self-test implementations.
Definition wildcard_matching.cpp:101
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
101 {
102 // 1st test
103 std::cout << "1st test ";
104 std::string matching1 = "baaabab";
105 std::string pattern1 = "*****ba*****ab";
106 assert(backtracking::wildcard_matching::wildcard_matching(matching1,
107 pattern1, 0, 0) ==
108 1); // here the pattern matches with given string
109 std::cout << "passed" << std::endl;
110
111 // 2nd test
112 std::cout << "2nd test ";
113 std::string matching2 = "baaabab";
114 std::string pattern2 = "ba*****ab";
115 assert(backtracking::wildcard_matching::wildcard_matching(matching2,
116 pattern2, 0, 0) ==
117 1); // here the pattern matches with given string
118 std::cout << "passed" << std::endl;
119
120 // 3rd test
121 std::cout << "3rd test ";
122 std::string matching3 = "baaabab";
123 std::string pattern3 = "ba*ab";
124 assert(backtracking::wildcard_matching::wildcard_matching(matching3,
125 pattern3, 0, 0) ==
126 1); // here the pattern matches with given string
127 std::cout << "passed" << std::endl;
128
129 // 4th test
130 std::cout << "4th test ";
131 std::string matching4 = "baaabab";
132 std::string pattern4 = "a*ab";
133 assert(backtracking::wildcard_matching::wildcard_matching(matching4,
134 pattern4, 0, 0) ==
135 1); // here the pattern matches with given string
136 std::cout << "passed" << std::endl;
137
138 // 5th test
139 std::cout << "5th test ";
140 std::string matching5 = "baaabab";
141 std::string pattern5 = "aa?ab";
142 assert(backtracking::wildcard_matching::wildcard_matching(matching5,
143 pattern5, 0, 0) ==
144 1); // here the pattern matches with given string
145 std::cout << "passed" << std::endl;
146}
T endl(T... args)
Here is the call graph for this function:

◆ wildcard_matching()

bool backtracking::wildcard_matching::wildcard_matching ( std::string s,
std::string p,
uint32_t pos1,
uint32_t pos2 )
40 {
41 uint32_t n = s.length();
42 uint32_t m = p.length();
43 // matching is successfull if both strings are done
44 if (pos1 == n && pos2 == m) {
45 return true;
46 }
47
48 // matching is unsuccessfull if pattern is not finished but matching string
49 // is
50 if (pos1 != n && pos2 == m) {
51 return false;
52 }
53
54 // all the remaining characters of patterns must be * inorder to match with
55 // finished string
56 if (pos1 == n && pos2 != m) {
57 while (pos2 < m && p[pos2] == '*') {
58 pos2++;
59 }
60
61 return pos2 == m;
62 }
63
64 // if already calculted for these positions
65 if (dpTable[pos1][pos2] != -1) {
66 return dpTable[pos1][pos2];
67 }
68
69 // if the characters are same just go ahead in both the string
70 if (s[pos1] == p[pos2]) {
71 return dpTable[pos1][pos2] =
72 wildcard_matching(s, p, pos1 + 1, pos2 + 1);
73 }
74
75 else {
76 // can only single character
77 if (p[pos2] == '?') {
78 return dpTable[pos1][pos2] =
79 wildcard_matching(s, p, pos1 + 1, pos2 + 1);
80 }
81 // have choice either to match one or more charcters
82 else if (p[pos2] == '*') {
83 return dpTable[pos1][pos2] =
84 wildcard_matching(s, p, pos1, pos2 + 1) ||
85 wildcard_matching(s, p, pos1 + 1, pos2);
86 }
87 // not possible to match
88 else {
89 return dpTable[pos1][pos2] = 0;
90 }
91 }
92}
Functions for the Wildcard Matching problem.
T length(T... args)
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.

Variable Documentation

◆ dpTable

std::vector< std::vector< int64_t > > backtracking::wildcard_matching::dpTable(1000, std::vector< int64_t >(1000, -1)) ( 1000 ,
std::vector< int64_t > 1000, -1 )

The main function implements if pattern can be matched with given string.

Parameters
sis the given matching string
pis the given pattern
pos1is the starting index
pos2is the last index
Returns
1 if pattern matches with matching string otherwise 0