codeparrot/xlcost-text-to-code huggingface dataset:
Longest Repeated Subsequence | C ++ program to find the longest repeated subsequence ; This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; THIS PART OF CODE IS SAME AS BELOW POST . IT FILLS dp [ ] [ ] https : www . geeksforgeeks . org / longest - repeating - subsequence / OR the code mentioned above . ; THIS PART OF CODE FINDS THE RESULT STRING USING DP [ ] [ ] Initialize result ; Traverse dp [ ] [ ] from bottom right ; If this cell is same as diagonally adjacent cell just above it , then same characters are present at str [ i - 1 ] and str [ j - 1 ] . Append any of them to result . ; Otherwise we move to the side that that gave us maximum result ; Since we traverse dp [ ] [ ] from bottom , we get result in reverse order . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string longestRepeatedSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; string res = " " ; int i = n , j = n ; while ( i > 0 && j > 0 ) { if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) { res = res + str [ i - 1 ] ; i -- ; j -- ; } else if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) i -- ; else j -- ; } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string str = " AABEBCDD " ; cout << longestRepeatedSubSeq ( str ) ; return 0 ; }
Sub | CPP code to find the sub - tree with minimum color difference in a 2 - coloured tree ; Tree traversal to compute minimum difference ; Initial min difference is the color of node ; Traversing its children ; Not traversing the parent ; If the child is adding positively to difference , we include it in the answer Otherwise , we leave the sub - tree and include 0 ( nothing ) in the answer ; DFS for colour difference : 1 colour - 2 colour ; Minimum colour difference is maximum answer value ; Clearing the current value to check for colour2 as well ; Interchanging the colours ; DFS for colour difference : 2 colour - 1 colour ; Checking if colour2 makes the minimum colour difference ; Driver code ; Nodes ; Adjacency list representation ; Edges ; Index represent the colour of that node There is no Node 0 , so we start from index 1 to N ; Printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int node , int parent , vector < int > tree [ ] , int colour [ ] , int answer [ ] ) { answer [ node ] = colour [ node ] ; for ( auto u : tree [ node ] ) { if ( u == parent ) continue ; dfs ( u , node , tree , colour , answer ) ; answer [ node ] += max ( answer [ u ] , 0 ) ; } } int maxDiff ( vector < int > tree [ ] , int colour [ ] , int N ) { int answer [ N + 1 ] ; memset ( answer , 0 , sizeof ( answer ) ) ; dfs ( 1 , 0 , tree , colour , answer ) ; int high = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { high = max ( high , answer [ i ] ) ; answer [ i ] = 0 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( colour [ i ] == -1 ) colour [ i ] = 1 ; else colour [ i ] = -1 ; } dfs ( 1 , 0 , tree , colour , answer ) ; for ( int i = 1 ; i < N ; i ++ ) high = max ( high , answer [ i ] ) ; return high ; } int main ( ) { int N = 5 ; vector < int > tree [ N + 1 ] ; tree [ 1 ] . push_back ( 2 ) ; tree [ 2 ] . push_back ( 1 ) ; tree [ 1 ] . push_back ( 3 ) ; tree [ 3 ] . push_back ( 1 ) ; tree [ 2 ] . push_back ( 4 ) ; tree [ 4 ] . push_back ( 2 ) ; tree [ 3 ] . push_back ( 5 ) ; tree [ 5 ] . push_back ( 3 ) ; int colour [ ] = { 0 , 1 , 1 , -1 , -1 , 1 } ; cout << maxDiff ( tree , colour , N ) ; return 0 ; }
Maximum elements that can be made equal with k updates | C ++ program to find maximum elements that can be made equal with k updates ; Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ElementsCalculationFunc ( int pre [ ] , int maxx [ ] , int x , int k , int n ) { for ( int i = 0 , j = x ; j <= n ; j ++ , i ++ ) { if ( x * maxx [ j ] - ( pre [ j ] - pre [ i ] ) <= k ) return true ; } return false ; } void MaxNumberOfElements ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; for ( int i = 0 ; i <= n ; ++ i ) { pre [ i ] = 0 ; maxx [ i ] = 0 ; } for ( int i = 1 ; i <= n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i - 1 ] ; maxx [ i ] = max ( maxx [ i - 1 ] , a [ i - 1 ] ) ; } int l = 1 , r = n , ans ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( ElementsCalculationFunc ( pre , maxx , mid - 1 , k , n ) ) { ans = mid ; l = mid + 1 ; } else r = mid - 1 ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 2 , 4 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; MaxNumberOfElements ( arr , n , k ) ; return 0 ; }
Remove array end element to maximize the sum of product | CPP program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  50 NEW_LINE using namespace std ; int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) return a [ low ] * turn ; if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ; dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high - 1 , turn + 1 ) ) ; return dp [ low ] [ high ] ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 5 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int dp [ MAX ] [ MAX ] ; memset ( dp , 0 , sizeof ( dp ) ) ; cout << solve ( dp , arr , 0 , n - 1 , 1 ) << endl ; return 0 ; }
Maximum sum bitonic subarray | C ++ implementation to find the maximum sum bitonic subarray ; function to find the maximum sum bitonic subarray ; ' msis [ ] ' to store the maximum sum increasing subarray up to each index of ' arr ' from the beginning ' msds [ ] ' to store the maximum sum decreasing subarray from each index of ' arr ' up to the end ; to store the maximum sum bitonic subarray ; building up the maximum sum increasing subarray for each array index ; building up the maximum sum decreasing subarray for each array index ; for each array index , calculating the maximum sum of bitonic subarray of which it is a part of ; if true , then update ' max ' bitonic subarray sum ; required maximum sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumBitonicSubArr ( int arr [ ] , int n ) { int msis [ n ] , msds [ n ] ; int max_sum = INT_MIN ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) if ( arr [ i ] > arr [ i + 1 ] ) msds [ i ] = msds [ i + 1 ] + arr [ i ] ; else msds [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( max_sum < ( msis [ i ] + msds [ i ] - arr [ i ] ) ) max_sum = msis [ i ] + msds [ i ] - arr [ i ] ; return max_sum ; } int main ( ) { int arr [ ] = { 5 , 3 , 9 , 2 , 7 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ Sum ▁ = ▁ " << maxSumBitonicSubArr ( arr , n ) ; return 0 ; }
Shortest possible combination of two strings | C ++ program to print supersequence of two strings ; Prints super sequence of a [ 0. . m - 1 ] and b [ 0. . n - 1 ] ; Fill table in bottom up manner ; Below steps follow above recurrence ; Following code is used to print supersequence ; Create a string of size index + 1 to store the result ; Start from the right - most - bottom - most corner and one by one store characters in res [ ] ; If current character in a [ ] and b are same , then current character is part of LCS ; Put current character in result ; reduce values of i , j and indexs ; If not same , then find the smaller of two and go in the direction of smaller value ; Copy remaining characters of string ' a ' ; Copy remaining characters of string ' b ' ; Print the result ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } int index = dp [ m ] [ n ] ; string res ( index + 1 , ' \0' ) ; int i = m , j = n ; while ( i > 0 && j > 0 ) { if ( a [ i - 1 ] == b [ j - 1 ] ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; j -- ; index -- ; } else if ( dp [ i - 1 ] [ j ] < dp [ i ] [ j - 1 ] ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; index -- ; } else { res [ index - 1 ] = b [ j - 1 ] ; j -- ; index -- ; } } while ( i > 0 ) { res [ index - 1 ] = a [ i - 1 ] ; i -- ; index -- ; } while ( j > 0 ) { res [ index - 1 ] = b [ j - 1 ] ; j -- ; index -- ; } cout << res ; } int main ( ) { string a = " algorithm " , b = " rhythm " ; printSuperSeq ( a , b ) ; return 0 ; }
Find length of longest subsequence of one string which is substring of another string | Base Case ; if the last char of both strings are equal ; if the last char of both strings are not equal ; Driver code ; as minimum length can be 0 only . ; traversing for every length of Y .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubsequenceSubstring ( string & X , string & Y , int n , int m ) { if ( n == 0 m == 0 ) return 0 ; if ( X [ n - 1 ] == Y [ m - 1 ] ) { return 1 + maxSubsequenceSubstring ( X , Y , n - 1 , m - 1 ) ; } else { return maxSubsequenceSubstring ( X , Y , n - 1 , m ) ; } } int main ( ) { string X = " abcd " ; string Y = " bacdbdcd " ; int n = X . size ( ) , m = Y . size ( ) ; int maximum_length = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { int temp_ans = maxSubsequenceSubstring ( X , Y , n , i ) ; if ( temp_ans > maximum_length ) maximum_length = temp_ans ; } cout << " Length ▁ for ▁ maximum ▁ possible ▁ Subsequence ▁ of ▁ string ▁ X ▁ which ▁ is ▁ Substring ▁ of ▁ Y ▁ - > ▁ " << maximum_length ; return 0 ; }
Minimum time to write characters using insert , delete and copy operation | C ++ program to write characters in minimum time by inserting , removing and copying operation ; method returns minimum time to write ' N ' characters ; declare dp array and initialize with zero ; first char will always take insertion time ; loop for ' N ' number of times ; if current char count is even then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy ; if current char count is odd then choose minimum from result for ( i - 1 ) chars and time for insertion and result for half of chars and time for copy and one extra character deletion ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minTimeForWritingChars ( int N , int insert , int remove , int copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; int dp [ N + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] = insert ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ; else dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ ( i + 1 ) / 2 ] + copy + remove ) ; } return dp [ N ] ; } int main ( ) { int N = 9 ; int insert = 1 , remove = 2 , copy = 1 ; cout << minTimeForWritingChars ( N , insert , remove , copy ) ; return 0 ; }
Count the number of ways to tile the floor of size n x m using 1 x m size tiles | C ++ implementation to count number of ways to tile a floor of size n x m using 1 x m tiles ; function to count the total number of ways ; table to store values of subproblems ; Fill the table upto value n ; recurrence relation ; base cases and for i = m = 1 ; i = = m ; required number of ways ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int n , int m ) { int count [ n + 1 ] ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m i == 1 ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } int main ( ) { int n = 7 , m = 4 ; cout << " Number ▁ of ▁ ways ▁ = ▁ " << countWays ( n , m ) ; return 0 ; }
Longest alternating subsequence | C ++ program for above approach ; Function for finding longest alternating subsequence ; " inc " and " dec " initialized as 1 as single element is still LAS ; Iterate from second element ; " inc " changes iff " dec " changes ; " dec " changes iff " inc " changes ; Return the maximum length ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LAS ( int arr [ ] , int n ) { int inc = 1 ; int dec = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { inc = dec + 1 ; } else if ( arr [ i ] < arr [ i - 1 ] ) { dec = inc + 1 ; } } return max ( inc , dec ) ; } int main ( ) { int arr [ ] = { 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LAS ( arr , n ) << endl ; return 0 ; }
Minimum number of deletions to make a string palindrome | C ++ program for above approach ; Function to return minimum Element between two values ; Utility function for calculating Minimum element to delete ; Condition to compare characters ; Recursive function call ; Return value , incrementing by 1 ; Function to calculate the minimum Element required to delete for Making string pelindrom ; Utility function call ; Driver code
#include <iostream> NEW_LINE using namespace std ; int min ( int x , int y ) { return ( x < y ) ? x : y ; } int utility_fun_for_del ( string str , int i , int j ) { if ( i >= j ) return 0 ; if ( str [ i ] == str [ j ] ) { return utility_fun_for_del ( str , i + 1 , j - 1 ) ; } return 1 + min ( utility_fun_for_del ( str , i + 1 , j ) , utility_fun_for_del ( str , i , j - 1 ) ) ; } int min_ele_del ( string str ) { return utility_fun_for_del ( str , 0 , str . length ( ) - 1 ) ; } int main ( ) { string str = " abefbac " ; cout << " Minimum ▁ element ▁ of ▁ deletions ▁ = ▁ " << min_ele_del ( str ) << endl ; return 0 ; }
Minimum number of deletions to make a string palindrome | Function definition ; Base cases ; Checking the ndesired condition ; If yes increment the count ; If no ; Return the value form the table ; Else store the max tranforamtion from the subsequence ; Return the dp [ - 1 ] [ - 1 ] ; Driver code ; Initialize the array with - 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 2000 ] [ 2000 ] ; int transformation ( string s1 , string s2 , int i , int j ) { if ( i >= ( s1 . size ( ) ) || j >= ( s2 . size ( ) ) ) return 0 ; if ( s1 [ i ] == s2 [ j ] ) { dp [ i ] [ j ] = 1 + transformation ( s1 , s2 , i + 1 , j + 1 ) ; } if ( dp [ i ] [ j ] != -1 ) { return dp [ i ] [ j ] ; } else dp [ i ] [ j ] = max ( transformation ( s1 , s2 , i , j + i ) , transformation ( s1 , s2 , i + 1 , j ) ) ; return dp [ s1 . size ( ) - 1 ] [ s2 . size ( ) - 1 ] ; } int main ( ) { string s1 = " geeksforgeeks " ; string s2 = " geeks " ; int i = 0 ; int j = 0 ; memset ( dp , -1 , sizeof dp ) ; cout << " MINIMUM ▁ NUMBER ▁ OF ▁ DELETIONS : ▁ " << ( s1 . size ( ) ) - transformation ( s1 , s2 , 0 , 0 ) << endl ; cout << " MINIMUM ▁ NUMBER ▁ OF ▁ INSERTIONS : ▁ " << ( s2 . size ( ) ) - transformation ( s1 , s2 , 0 , 0 ) << endl ; cout << ( " LCS ▁ LENGTH : ▁ " ) << transformation ( s1 , s2 , 0 , 0 ) ; }
Largest sum Zigzag sequence in a matrix | C ++ program to find the largest sum zigzag sequence ; Returns largest sum of a Zigzag sequence starting from ( i , j ) and ending at a bottom cell . ; If we have reached bottom ; Find the largest sum by considering all possible next elements in sequence . ; Returns largest possible sum of a Zizag sequence starting from top and ending at bottom . ; Consider all cells of top row as starting point ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int largestZigZagSumRec ( int mat [ ] [ MAX ] , int i , int j , int n ) { if ( i == n - 1 ) return mat [ i ] [ j ] ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return zzs + mat [ i ] [ j ] ; } int largestZigZag ( int mat [ ] [ MAX ] , int n ) { int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } int main ( ) { int n = 3 ; int mat [ ] [ MAX ] = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; cout << " Largest ▁ zigzag ▁ sum : ▁ " << largestZigZag ( mat , n ) ; return 0 ; }
Perfect Sum Problem ( Print all subsets with given sum ) | C ++ program to count all subsets with given sum . ; dp [ i ] [ j ] is going to store true if sum j is possible with array elements from 0 to i . ; A recursive function to print all subsets with the help of dp [ ] [ ] . Vector p [ ] stores current subset . ; If we reached end and sum is non - zero . We print p [ ] only if arr [ 0 ] is equal to sun OR dp [ 0 ] [ sum ] is true . ; Display Only when Sum of elements of p is equal to sum ; If sum becomes 0 ; If given sum can be achieved after ignoring current element . ; Create a new vector to store path ; If given sum can be achieved after considering current element . ; Prints all subsets of arr [ 0. . n - 1 ] with sum 0. ; Sum 0 can always be achieved with 0 elements ; Sum arr [ 0 ] can be achieved with single element ; Fill rest of the entries in dp [ ] [ ] ; Now recursively traverse dp [ ] [ ] to find all paths from dp [ n - 1 ] [ sum ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool * * dp ; void display ( const vector < int > & v ) { for ( int i = 0 ; i < v . size ( ) ; ++ i ) printf ( " % d ▁ " , v [ i ] ) ; printf ( " STRNEWLINE " ) ; } void printSubsetsRec ( int arr [ ] , int i , int sum , vector < int > & p ) { if ( i == 0 && sum != 0 && dp [ 0 ] [ sum ] ) { p . push_back ( arr [ i ] ) ; if ( arr [ i ] == sum ) display ( p ) ; return ; } if ( i == 0 && sum == 0 ) { display ( p ) ; return ; } if ( dp [ i - 1 ] [ sum ] ) { vector < int > b = p ; printSubsetsRec ( arr , i - 1 , sum , b ) ; } if ( sum >= arr [ i ] && dp [ i - 1 ] [ sum - arr [ i ] ] ) { p . push_back ( arr [ i ] ) ; printSubsetsRec ( arr , i - 1 , sum - arr [ i ] , p ) ; } } void printAllSubsets ( int arr [ ] , int n , int sum ) { if ( n == 0 sum < 0 ) return ; dp = new bool * [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { dp [ i ] = new bool [ sum + 1 ] ; dp [ i ] [ 0 ] = true ; } if ( arr [ 0 ] <= sum ) dp [ 0 ] [ arr [ 0 ] ] = true ; for ( int i = 1 ; i < n ; ++ i ) for ( int j = 0 ; j < sum + 1 ; ++ j ) dp [ i ] [ j ] = ( arr [ i ] <= j ) ? dp [ i - 1 ] [ j ] || dp [ i - 1 ] [ j - arr [ i ] ] : dp [ i - 1 ] [ j ] ; if ( dp [ n - 1 ] [ sum ] == false ) { printf ( " There ▁ are ▁ no ▁ subsets ▁ with ▁ sum ▁ % d STRNEWLINE " , sum ) ; return ; } vector < int > p ; printSubsetsRec ( arr , n - 1 , sum , p ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int sum = 10 ; printAllSubsets ( arr , n , sum ) ; return 0 ; }
Count ways to increase LCS length of two strings by one | C ++ program to get number of ways to increase LCS by 1 ; Utility method to get integer position of lower alphabet character ; Method returns total ways to increase LCS length by 1 ; Fill positions of each character in vector ; Initializing 2D array by 0 values ; Filling LCS array for prefix substrings ; Filling LCS array for suffix substrings ; Looping for all possible insertion positions in first string ; Trying all possible lower case characters ; Now for each character , loop over same character positions in second string ; If both , left and right substrings make total LCS then increase result by 1 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  26 NEW_LINE int toInt ( char ch ) { return ( ch - ' a ' ) ; } int waysToIncreaseLCSBy1 ( string str1 , string str2 ) { int m = str1 . length ( ) , n = str2 . length ( ) ; vector < int > position [ M ] ; for ( int i = 1 ; i <= n ; i ++ ) position [ toInt ( str2 [ i - 1 ] ) ] . push_back ( i ) ; int lcsl [ m + 2 ] [ n + 2 ] ; int lcsr [ m + 2 ] [ n + 2 ] ; for ( int i = 0 ; i <= m + 1 ; i ++ ) for ( int j = 0 ; j <= n + 1 ; j ++ ) lcsl [ i ] [ j ] = lcsr [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsl [ i ] [ j ] = 1 + lcsl [ i - 1 ] [ j - 1 ] ; else lcsl [ i ] [ j ] = max ( lcsl [ i - 1 ] [ j ] , lcsl [ i ] [ j - 1 ] ) ; } } for ( int i = m ; i >= 1 ; i -- ) { for ( int j = n ; j >= 1 ; j -- ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsr [ i ] [ j ] = 1 + lcsr [ i + 1 ] [ j + 1 ] ; else lcsr [ i ] [ j ] = max ( lcsr [ i + 1 ] [ j ] , lcsr [ i ] [ j + 1 ] ) ; } } int ways = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { for ( int j = 0 ; j < position [ toInt ( c ) ] . size ( ) ; j ++ ) { int p = position [ toInt ( c ) ] [ j ] ; if ( lcsl [ i ] [ p - 1 ] + lcsr [ i + 1 ] [ p + 1 ] == lcsl [ m ] [ n ] ) ways ++ ; } } } return ways ; } int main ( ) { string str1 = " abcabc " ; string str2 = " abcd " ; cout << waysToIncreaseLCSBy1 ( str1 , str2 ) ; return 0 ; }
Count of strings that can be formed using a , b and c under given constraints | C ++ program to count number of strings of n characters with ; n is total number of characters . bCount and cCount are counts of ' b ' and ' c ' respectively . ; Base cases ; Three cases , we choose , a or b or c In all three cases n decreases by 1. ; Driver code ; Total number of characters
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countStr ( int n , int bCount , int cCount ) { if ( bCount < 0 cCount < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( bCount == 0 && cCount == 0 ) return 1 ; int res = countStr ( n - 1 , bCount , cCount ) ; res += countStr ( n - 1 , bCount - 1 , cCount ) ; res += countStr ( n - 1 , bCount , cCount - 1 ) ; return res ; } int main ( ) { int n = 3 ; cout << countStr ( n , 1 , 2 ) ; return 0 ; }