
- 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 Perform Finite State Automaton based Search
This is a C++ program to perform finite state automaton based search. An automaton with a finite number of states is called a Finite Automaton. Here, a text is given text[0 … t-1] and a pattern p[0 ... p-1] is also given. We have to find the pattern in the text and print its all occurrences at the respective indices.
Algorithms
Begin Function void transitiontable(): 1) put the entries in first row and filled it up. All entries in first row are always 0 except the entry for p[0] character. Always we need to go to state 1. for p[0]. 2) Initialize longestprefixsuffix as 0. 3) for i = 1 to P. (Here P is the length of the pattern) a) Copy the entries from the row at index equal to longestprefixsuffix. b) Update the entry for p[i] character to i+1. c) Update longestprefixsuffix = TF[lps][pat[i]] where TT is the 2D array. End
Example
#include<iostream> #include<cstring> #define NO_OF_CHARS 512 using namespace std; // builds the TF table which represents Finite Automata for a given pattern void transitiontable(char *p, int P, int TT[][NO_OF_CHARS]) { int i, longestprefixsuffix = 0, y; // put entries in first row for (y =0; y < NO_OF_CHARS; y++) TT[0][y] = 0; TT[0][p[0]] = 1; // put entries in other rows for (i = 1; i<= P; i++) { // Copy values from row at index longestprefixsuffix for (y = 0; y < NO_OF_CHARS; y++) TT[i][y] = TT[longestprefixsuffix][y]; // Update the entry TT[i][p[i]] = i + 1; // Update lps for next row to be filled if (i < P) longestprefixsuffix = TT[longestprefixsuffix][p[i]]; // TT is the 2D array which is being constructed. } } //Prints all occurrences of pattern in text void patternsearch(char *p, char *t) { int P = strlen(p); int T = strlen(t); int TT[P+1][NO_OF_CHARS]; transitiontable(p, P, TT); // process text over FA. int i, j=0; for (i = 0; i < T; i++) { j = TT[j][t[i]]; if (j == P) { cout<<"\n pattern is found at index: "<< i-P+1; } } } int main() { char *text = "AABAA ABBAACCDD CCDDAABAA"; //take the text as input char *pattern = "AABAA"; //take the pattern as input patternsearch(pattern, text); getchar(); return 0; }
Output
pattern is found at index: 0 pattern is found at index: 20
- Related Articles
- 8085 Program to perform linear search
- C++ Program to Perform Uniform Binary Search
- Program to perform linear search in 8085 Microprocessor
- a 8085 Program to perform bubble sort based on choice
- C++ Program to Perform Searching Based on Locality of Reference
- C++ Program to Perform Dictionary Operations in a Binary Search Tree
- C++ Program to Perform Left Rotation on a Binary Search Tree
- C++ Program to Perform Right Rotation on a Binary Search Tree
- Program to perform bubble sort based on choice in 8085 Microprocessor
- Construct Finite State Machine as Processing input
- Perform MongoDB full text search
- What is a finite state machine in TOC?
- Explain the equivalence between two Finite State Machines.
- How to perform case-insensitive search in Oracle?
- Perform MySQL search between two dates

Advertisements