
- 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
Count all Palindromic Subsequence in a given String in C++
In this tutorial, we will be discussing a program to find the number of all palindromic subsequences in a given string.
For this we will be provided with a string. Our task is to find the number of palindromic subsequences that can be made in that given string.
Example
#include<iostream> #include<cstring> using namespace std; //returning total palindromic sequence int count_palin(string str){ int N = str.length(); //creating a 2D array int cps[N+1][N+1]; memset(cps, 0 ,sizeof(cps)); for (int i=0; i<N; i++) cps[i][i] = 1; for (int L=2; L<=N; L++){ for (int i=0; i<N; i++){ int k = L+i-1; if (str[i] == str[k]) cps[i][k] = cps[i][k-1] + cps[i+1][k] + 1; else cps[i][k] = cps[i][k-1] + cps[i+1][k] - cps[i+1][k-1]; } } return cps[0][N-1]; } int main(){ string str = "abcb"; cout << "Total palindromic subsequence are : " << count_palin(str) << endl; return 0; }
Output
Total palindromic subsequence are : 6
- Related Articles
- Counting all possible palindromic subsequence within a string in JavaScript
- Find a palindromic string B such that given String A is a subsequence of B in C++
- Count subsequence of length three in a given string in C++
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Find all distinct palindromic sub-strings of a given String in Python
- Longest Palindromic Subsequence in C++
- Longest Palindromic Subsequence
- Find all palindromic sub-strings of a given string - Set 2 in Python
- Count all Prime Length Palindromic Substrings in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Print all palindromic partitions of a string in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Java Program for Longest Palindromic Subsequence
- Find the count of palindromic sub-string of a string in its sorted form in Python
- Count Unique Characters of All Substrings of a Given String in C++

Advertisements