Programming Articles - Page 2647 of 3366

C++ Program to Implement Trie

Farhan Muhamed
Updated on 15-Jul-2025 18:04:38

2K+ Views

A Trie, also known as a prefix tree, is used to store and search for large sets of strings. In this section we will discuss all about Trie data structure, its operations, and how to implement it in C++. Trie Data Structure A Trie is a data structure similar to a tree that is used to store a dynamic set of strings. The root node represents an empty string, and each edge represents a character. The path from the root to a node represents a prefix of a string stored in the Trie. The image below show how a ... Read More

C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome

Smita Kapse
Updated on 30-Jul-2019 22:30:26

209 Views

Here we shall discuss a C++ Program to find number of ways to Partition a word in such a way that each word is a Palindrome.AlgorithmsBegin    Take the word as input.    Function partitionadd(vector &u, string &s, vector &tmp, int index):    if (index == 0)       tmp.clear()    for i = index to length-1       st = st + s[i]       if (checkPalin(st))          tmp.push_back(st)          if (i+1 < length)             partitionadd(u, s, tmp, i+1)          else   ... Read More

C++ Program to Find the Longest Prefix Matching of a Given Sequence

Anvi Jain
Updated on 30-Jul-2019 22:30:26

548 Views

Here we shall discuss a C++ program to find the Longest Subsequence Common to All Sequences in a Set of Sequences.AlgorithmsBegin Take the array of strings as input. function matchedPrefixtill(): find the matched prefix between string s1 and s2 :    n1 = store length of string s1.    n2 = store length of string s2.    for i = 0, j = 0 to i

C++ Program to Find the Shortest Supersequence that Contains Two or more Sequences as Subsequences

Anvi Jain
Updated on 30-Jul-2019 22:30:26

180 Views

Here we shall discuss a C++ Program to find the Shortest Supersequence that contains two or more Sequences as Subsequences.AlgorithmBegin    function ShortestSubSeq() returns supersequence of A and B:    1) Declare an array ss[i][j] which contains length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1].    2) Find the length of the possible supersequence in bottom up manner using recursion.    3) Declare an array ss[i][j] which stores length of shortest supersequence for A[0 .. i-1] and B[0 .. j-1] in index.    4) Declare a string s to store the shortest subsequence.    5) Initialize ... Read More

C++ Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

214 Views

Here we shall discuss a C++ program to find the Longest Subsequence Common to All Sequences in a Set of Sequences.AlgorithmsBegin Take the array of strings as input. function matchedPrefixtill(): find the matched prefix between string s1 and s2 :    n1 = store length of string s1.    n2 = store length of string s2.    for i = 0, j = 0 to i

C++ Program to Implement Kadane’s Algorithm

Farhan Muhamed
Updated on 16-Jul-2025 16:00:26

2K+ Views

The Kadane's algorithm is used to find the maximum subarray sum in a given array of integers. This section will explain the Kadane's algorithm with example and C++ implementation. Given an array of integers, our task is to find the maximum possible sum for a subarray using Kadane's algorithm. For example:ScenarioConsider the following example with input/output scenario: Input: [-2, 1, -3, 4, -1, 2, 1, -5, 4] Output: 6 Explanation: The subarray [4, -1, 2, 1] has the largest sum = 6. What is Kadane's Algorithm? Kadane's algorithm is an optimized method to find the maximum ... Read More

C++ Program to Implement Affine Cipher

Anvi Jain
Updated on 30-Jul-2019 22:30:26

3K+ Views

In the Affine cipher, each letter in an alphabet is mapped to its numeric equivalent, is a type of monoalphabetic substitution cipher. Encryption is done using a simple mathematical function and converted back to a letter.The letters of an alphabet of size m are first mapped to the integers in the range 0 … m-1, in the Affine cipher, The ‘key’ for the Affine cipher consists of 2 numbers, a and b. a should be chosen to be relatively prime to m.EncryptionTo transform the integer, it uses modular arithmetic that each plaintext letter corresponds to into another integer that correspond ... Read More

C++ Program to Implement the Vigenere Cypher

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

8K+ Views

Vigenere Cipher is a kind of polyalphabetic substitution method of encrypting alphabetic text.Vigenere Cipher Table is used in which alphabets from A to Z are written in 26 rows, for encryption and decryption in this method.EncryptionKey: WELCOMEMessage: ThisistutorialspointHere we have to obtain a key by repeating the given key till its length becomes equal to original message length.For encryption take first letter of message and key i.e. T and W. Take the alphabet in Vigenere Cipher Table where T row and W column coincides i.e. P.Repeat the same process for all remaining alphabets in message text.Finally, the encrypted message text ... Read More

C++ Program to Implement the Hill Cypher

Smita Kapse
Updated on 30-Jul-2019 22:30:26

8K+ Views

Based on linear algebra Hill cipher is a polygraphic substitution cipher in cryptography.To encrypt message: The key string and message string are represented as matrix form. They are multiplied then, against modulo 26. The key matrix should have inverse to decrypt the message.To decrypt message: The encrypted message is multiplied by inverse key matrix used for encryption against modulo 26 to get decrypt message.For exampleKey matrix1 0 1 2 4 0 3 5 6Message string ‘ABC’ in matrix form −0 1 2For encryptionAfter multiplying above two matrices we get, 2 4 17Which will be the encrypted message ‘CER’For decryptionInverse of ... Read More

C++ Program to Implement the RSA Algorithm

Anvi Jain
Updated on 17-May-2021 06:43:38

18K+ Views

RSA is an asymmetric cryptography algorithm which works on two keys-public key and private key.AlgorithmsBegin    1. Choose two prime numbers p and q.    2. Compute n = p*q.    3. Calculate phi = (p-1) * (q-1).    4. Choose an integer e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.    5. Calculate d as d ≡ e−1 (mod phi(n)); here, d is the modular multiplicative inverse of e modulo phi(n).    6. For encryption, c = me mod n, where m = original message.    7. For ... Read More

Advertisements