
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Bitap Algorithm for String Matching
This is a C++ Program to Implement Bitap Algorithm for String Matching. The algorithm tells whether a given text contains a substring which is “approximately equal” to a given pattern, where approximate equality is defined in terms of Levenshtein distance — if the substring and pattern are within a given distance k of each other, then according to the algorithm they are equal. It begins by precomputing a set of bitmasks containing one bit for each element of the pattern. So we are able to do most of the work with bitwise operations, which are extremely fast.
Algorithm
Begin Take the string and pattern as input. function bitmap_search() and it takes argument string text t and string pattern p : Initialize the bit array A. Initialize the pattern bitmasks, p_mask[300] Update the bit array. for i = 0 to 299 p_mask[i] = ~0 for i = 0 to m-1 p_mask[p[i]] and= ~(1L left shift i); for i = 0 to t.length()-1 A |= p_mask[t[i]]; A <<= 1; if ((A and (1L left shift m)) == 0 return i - m + 1 return -1 End
Example Code
#include <string> #include <map> #include <iostream> using namespace std; int bitmap_search(string t, string p) { int m = p.length(); long p_mask[300]; long A = ~1; if (m == 0) return -1; if (m >63) { cout<<"Pattern is too long!";//if pattern is too long return -1; } for (int i = 0; i <= 299; ++i) p_mask[i] = ~0; for (int i = 0; i < m; ++i) p_mask[p[i]] &= ~(1L << i); for (int i = 0; i < t.length(); ++i) { A |= p_mask[t[i]]; A <<= 1; if ((A & (1L << m)) == 0) return i - m + 1; } return -1; } void findPattern(string t, string p) { int position = bitmap_search(t, p);//initialize the position with the function bitmap_search if (position == -1) cout << "\nNo Match\n"; else cout << "\nPattern found at position : " << position; } int main(int argc, char **argv) { cout << "Enter Text:\n"; string t; cin >>t; cout << "Enter Pattern:\n"; string p; cin >>p; findPattern(t, p); }
Output
Enter Text: Tutorialspoint Enter Pattern: point Pattern found at position : 9
- Related Articles
- C++ Program to Implement Wagner and Fisher Algorithm for online String Matching
- C++ Program to Implement String Matching Using Vectors
- C++ Program to Implement the String Search Algorithm for Short Text Sizes
- C++ Program to Implement Kadane’s Algorithm
- C++ Program to Implement Johnson’s Algorithm
- C++ Program to Implement Fisher-Yates Algorithm for Array Shuffling
- C++ Program to Implement the RSA Algorithm
- C++ Program to Implement Nearest Neighbour Algorithm
- C++ Program to Implement Expression Tree Algorithm
- C++ Program to Implement Interpolation Search Algorithm
- C++ Program to Implement Extended Euclidean Algorithm
- C++ Program to Implement Coppersmith Freivald’s Algorithm
- C++ Program to Implement Modular Exponentiation Algorithm
- C program to implement Euclid’ s algorithm
- C++ Program to Implement Levenshtein Distance Computing Algorithm

Advertisements