C++ program for Finite Automata algorithm for Pattern Searching

Ayush Gupta
Updated on 03-Oct-2019 11:30:42

674 Views

In this article, we will be discussing a program to execute the Finite Automata algorithm for pattern searching.We are provided with a text[0...n-1] and a pattern[0...m-1]. We have to find all the occurrences of the pattern[] in the text[].For this we would preprocess the text[] and build a 2-d array to represent it. After that we just have to traverse between the elements of the text[] and the different states of the automata.Example Live Demo#include #include #define total_chars 256 int calc_nextstate(char *pat, int M, int state, int x) {    if (state < M && x == pat[state])       ... Read More

C++ program to check whether it is possible to finish all the tasks or not from given dependencies

Ayush Gupta
Updated on 03-Oct-2019 11:25:23

91 Views

In this article, we will be discussing a program to check if it is possible to finish all the given tasks on the basis of the given prerequisites.For example, let us say we have been given three tasks and prerequisites are [[1, 0], [2, 1], [3, 2]].( [1, 0] means that to pick up ‘1’ task; the ‘0’ task must be completed first.)Then, in this example since the ‘0’ task doesn’t have any prerequisite it can be completed first. Then the ‘1’ task can be completed, since the ‘0’ task has been completed. Similarly, both ‘2’ and ‘3’ tasks can ... Read More

C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers

Ayush Gupta
Updated on 03-Oct-2019 11:21:14

629 Views

In this article, we will be discussing a program to find ways an integer (say X) can be expressed as sum of n-th power of unique natural numbers.For example, let X = 100 and n = 2Then there would be 3 ways to express 100 as sum of squares of natural numbers.100 = 102 100 = 62 + 82 100 = 12 + 32 + 42 + 52 + 72This can be done easily by using recursion. We would start from 1 and go till the n-th root of the given number. In every run, we would subtract the n-th ... Read More

C++ program to find unique pairs such that each element is less than or equal to N

Ayush Gupta
Updated on 03-Oct-2019 11:17:31

67 Views

In this article, we will be discussing a program to find unique pairs of numbers having elements less than or equal to N and following some certain conditions −The square of difference between the two numbers must be equal to the LCM of those two numbers.The HCF of those two numbers can be represented as a product of any two consecutive numbers.The best approach to solve this problem would be to take two consecutive numbers (starting from 1) and finding the multiples of product of those numbers. Then among the multiples, to specify to one pair of numbers we need ... Read More

C++ program to find union and intersection of two unsorted arrays

Ayush Gupta
Updated on 03-Oct-2019 11:14:47

1K+ Views

In this article, we will be discussing a program to find the union and intersection of two given unsorted arrays.Let us denote the two arrays with ‘A’ and ‘B’. Then union of those arrays is denoted by A ∪ B which is basically an array of all the elements in both the given arrays; provided that each element repeats only once.To find this, we will create a separate array and copy down all the elements from the first array. Then we will traverse through the elements of the second array and check if it is already present in the union ... Read More

C++ program to find uncommon characters in two given strings

Ayush Gupta
Updated on 03-Oct-2019 11:11:58

643 Views

In this article, we will be discussing a program to find out the uncommon characters during comparison of two different given strings.As we know, strings are nothing but an array of characters. Therefore, for comparison we would be traversing through the characters of one string and simultaneously checking if that element exists in the other string.If we let the first string be A and the second string B.Then it would give us A - B. Similarly we can calculate B - A.Combining both of these results we would get( A - B ) ∪ ( B - A )i.e the ... Read More

C++ program to find two numbers with sum and product both same as N

Ayush Gupta
Updated on 03-Oct-2019 11:00:48

448 Views

In this tutorial, we will be discussing a program to find two numbers (say ‘a’ and ‘b’) such that botha+b = N and a*b = N are satisfied.Eliminating ‘a’ from both the equations, we get a quadratic equation in ‘b’ and ‘N’ i.eb2 - bN + N = 0This equation will have two roots which will give us the value of both ‘a’ and ‘b’. Using the determinant method to find the roots, we get the value of ‘a’ and ‘b’ as, $a= (N-\sqrt{N*N-4N)}/2\ b= (N+\sqrt{N*N-4N)}/2 $Example Live Demo#include //header file for the square root function #include using namespace ... Read More

MySQL query to get count of each fileid entry in a table with Id and FileIDs?

AmitDiwan
Updated on 03-Oct-2019 08:56:35

47 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FileID int ) AUTO_INCREMENT=100; Query OK, 0 rows affected (1.36 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FileID) values(50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(FileID) values(60); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FileID) values(50); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FileID) values(70); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(FileID) values(60); Query OK, 1 row affected (0.28 sec) mysql> ... Read More

What are the differences between JavaScript and PHP cookies?

Anvi Jain
Updated on 03-Oct-2019 08:09:22

537 Views

JavaScript CookiesUsing JavaScript cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.PHP CookiesCookies are text files stored on the client computer and they are kept for tracking purpose. PHP transparently supports HTTP cookies.How JavaScript cookies work?Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another page on your site, the browser ... Read More

How to define methods for an Object in JavaScript?

Nancy Den
Updated on 03-Oct-2019 08:07:09

71 Views

Methods are the functions that let the object do something or let something is done to it. There is a small difference between a function and a method – at a function is a standalone unit of statements and a method is attached to an object and can be referenced by this keyword.Methods are used for everything from displaying the contents of the object to the screen to performing complex mathematical operations on a group of local properties and parameters. Here’s an example showing how to use the write() method of the document object to write any content on the document.document.write("This ... Read More

Advertisements