codeparrot/xlcost-text-to-code huggingface dataset:
Optimal Strategy for a Game | Set 3 | C ++ implementation ; Calculates the maximum score possible for P1 If only the bags from beg to ed were available ; Length of the game ; Which turn is being played ; Base case i . e last turn ; if it is P1 's turn ; if P2 's turn ; Player picks money from the left end ; Player picks money from the right end ; if it is player 1 's turn then  current picked score added to  his total. choose maximum of  the two scores as P1 tries to  maximize his score. ; if player 2 ' s ▁ turn ▁ don ' t add current picked bag score to total . choose minimum of the two scores as P2 tries to minimize P1 's score. ; Driver Code ; Input array ; Function Calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , string > maxScore ( vector < int > money , int beg , int ed ) { int totalTurns = money . size ( ) ; int turnsTillNow = beg + ( ( totalTurns - 1 ) - ed ) ; if ( beg == ed ) { if ( turnsTillNow % 2 == 0 ) return { money [ beg ] , " L " } ; else return { 0 , " L " } ; } pair < int , string > scoreOne = maxScore ( money , beg + 1 , ed ) ; pair < int , string > scoreTwo = maxScore ( money , beg , ed - 1 ) ; if ( turnsTillNow % 2 == 0 ) { if ( money [ beg ] + scoreOne . first > money [ ed ] + scoreTwo . first ) { return { money [ beg ] + scoreOne . first , " L " + scoreOne . second } ; } else return { money [ ed ] + scoreTwo . first , " R " + scoreTwo . second } ; } else { if ( scoreOne . first < scoreTwo . first ) return { scoreOne . first , " L " + scoreOne . second } ; else return { scoreTwo . first , " R " + scoreTwo . second } ; } } int main ( ) { int ar [ ] = { 10 , 80 , 90 , 30 } ; int arraySize = sizeof ( ar ) / sizeof ( int ) ; vector < int > bags ( ar , ar + arraySize ) ; pair < int , string > ans = maxScore ( bags , 0 , bags . size ( ) - 1 ) ; cout << ans . first << " ▁ " << ans . second << endl ; return 0 ; }
Minimum number of Fibonacci jumps to reach end | A Dynamic Programming based C ++ program to find minimum number of jumps to reach Destination ; Function that returns the min number of jump to reach the destination ; We consider only those Fibonacci numbers which are less than n , where we can consider fib [ 30 ] to be the upper bound as this will cross 10 ^ 5 ; DP [ i ] will be storing the minimum number of jumps required for the position i . So DP [ N + 1 ] will have the result we consider 0 as source and N + 1 as the destination ; Base case ( Steps to reach source is ) ; Initialize all table values as Infinite ; Go through each positions till destination . ; Calculate the minimum of that position if all the Fibonacci numbers are considered ; If the position is safe or the position is the destination then only we calculate the minimum otherwise the cost is MAX as default ; - 1 denotes if there is no path possible ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1e9 NEW_LINE int minJumps ( int arr [ ] , int N ) { int fib [ 30 ] ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 30 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; int DP [ N + 2 ] ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= N + 1 ; i ++ ) DP [ i ] = MAX ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { for ( int j = 1 ; j < 30 ; j ++ ) { if ( ( arr [ i - 1 ] == 1 i == N + 1 ) && i - fib [ j ] >= 0 ) DP [ i ] = min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) ; } } if ( DP [ N + 1 ] != MAX ) return DP [ N + 1 ] ; else return -1 ; } int main ( ) { int arr [ ] = { 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minJumps ( arr , n ) ; return 0 ; }
Subsequence X of length K such that gcd ( X [ 0 ] , X [ 1 ] ) + ( X [ 2 ] , X [ 3 ] ) + ... is maximized | C ++ program to find the sum of the addition of all possible subsets ; Recursive function to find the maximum value of the given recurrence ; If we get K elements ; If we have reached the end and K elements are not there ; If the state has been visited ; Iterate for every element as the next possible element and take the element which gives the maximum answer ; If this element is the first element in the individual pair in the subsequence then simply recurrence with the last element as i - th index ; If this element is the second element in the individual pair , the find gcd with the previous element and add to the answer and recur for the next element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int recur ( int ind , int cnt , int last , int a [ ] , int n , int k , int dp [ ] [ MAX ] ) { if ( cnt == k ) return 0 ; if ( ind == n ) return -1e9 ; if ( dp [ ind ] [ cnt ] != -1 ) return dp [ ind ] [ cnt ] ; int ans = 0 ; for ( int i = ind ; i < n ; i ++ ) { if ( cnt % 2 == 0 ) ans = max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) ) ; else ans = max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) ) ; } return dp [ ind ] [ cnt ] = ans ; } int main ( ) { int a [ ] = { 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 4 ; int dp [ n ] [ MAX ] ; memset ( dp , -1 , sizeof dp ) ; cout << recur ( 0 , 0 , 0 , a , n , k , dp ) ; }
Maximum number of given operations to remove the entire string | C ++ implementation of the approach ; Function to return the maximum number of given operations required to remove the given string entirely ; If length of the string is zero ; Single operation can delete the entire string ; To store the prefix of the string which is to be deleted ; Prefix s [ 0. . i ] ; To store the substring s [ i + 1. . .2 * i + 1 ] ; If the prefix s [ 0. . . i ] can be deleted ; 1 operation to remove the current prefix and then recursively find the count of operations for the substring s [ i + 1. . . n - 1 ] ; Entire string has to be deleted ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( string s ) { if ( s . length ( ) == 0 ) return 0 ; int c = 1 ; string d = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { d += s [ i ] ; string s2 = s . substr ( i + 1 , d . length ( ) ) ; if ( s2 == d ) { c = 1 + find ( s . substr ( i + 1 ) ) ; break ; } } return c ; } int main ( ) { string s = " abababab " ; cout << find ( s ) ; return 0 ; }
Sum of the distances from every node to all other nodes is maximum | C ++ program to implement the above approach ; Function to add an edge to the tree ; Function to run DFS and calculate the height of the subtree below it ; Initially initialize with 1 ; Traverse for all nodes connected to node ; If node is not parent then recall dfs ; Add the size of the subtree beneath it ; Function to assign weights to edges to maximize the final sum ; Initialize it which stores the height of the subtree beneath it ; Call the DFS function to ; Sort the given array ; Stores the number of times an edge is part of a path ; Iterate for all edges and find the number of nodes on the left and on the right ; Node 1 ; Node 2 ; If the number of nodes below is less then the other will be n - dp [ node ] ; Second condition ; Sort the number of times an edges occurs in the path ; Find the summation of all those paths and return ; Driver code ; Add an edge 1 - 2 in the tree ; Add an edge 2 - 3 in the tree ; Add an edge 3 - 4 in the tree ; Add an edge 3 - 5 in the tree ; Array which gives the edges weight to be assigned
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addEdge ( vector < pair < int , int > > & edges , list < int > * tree , int x , int y ) { edges . push_back ( { x , y } ) ; tree [ x ] . push_back ( y ) ; tree [ y ] . push_back ( x ) ; } void dfs ( vector < pair < int , int > > & edges , list < int > * tree , int node , int parent , int dp [ ] ) { dp [ node ] = 1 ; for ( auto it : tree [ node ] ) { if ( it != parent ) { dfs ( edges , tree , it , node , dp ) ; dp [ node ] += dp [ it ] ; } } } int maximizeSum ( int a [ ] , vector < pair < int , int > > & edges , list < int > * tree , int n ) { int dp [ n + 1 ] = { 0 } ; dfs ( edges , tree , 1 , 0 , dp ) ; sort ( a , a + ( n - 1 ) ) ; vector < int > ans ; for ( auto it : edges ) { int x = it . first ; int y = it . second ; if ( dp [ x ] < dp [ y ] ) { int fi = dp [ x ] ; int sec = n - dp [ x ] ; ans . push_back ( fi * sec ) ; } else { int fi = dp [ y ] ; int sec = n - dp [ y ] ; ans . push_back ( fi * sec ) ; } } sort ( ans . begin ( ) , ans . end ( ) ) ; int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { res += ans [ i ] * a [ i ] ; } return res ; } int main ( ) { int n = 5 ; vector < pair < int , int > > edges ; list < int > * tree = new list < int > [ n + 1 ] ; addEdge ( edges , tree , 1 , 2 ) ; addEdge ( edges , tree , 1 , 3 ) ; addEdge ( edges , tree , 3 , 4 ) ; addEdge ( edges , tree , 3 , 5 ) ; int a [ ] = { 6 , 3 , 1 , 9 , 3 } ; cout << maximizeSum ( a , edges , tree , n ) ; }
Divide the array in K segments such that the sum of minimums is maximized | C ++ program to find the sum of the minimum of all the segments ; Function to maximize the sum of the minimums ; If k segments have been divided ; If we are at the end ; If we donot reach the end then return a negative number that cannot be the sum ; If at the end but k segments are not formed ; If the state has not been visited yet ; If the state has not been visited ; Get the minimum element in the segment ; Iterate and try to break at every index and create a segment ; Find the minimum element in the segment ; Find the sum of all the segments trying all the possible combinations ; Return the answer by memoizing it ; Driver Code ; Initialize dp array with - 1
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10 ; int maximizeSum ( int a [ ] , int n , int ind , int k , int dp [ MAX ] [ MAX ] ) { if ( k == 0 ) { if ( ind == n ) return 0 ; else return -1e9 ; } else if ( ind == n ) return -1e9 ; else if ( dp [ ind ] [ k ] != -1 ) return dp [ ind ] [ k ] ; else { int ans = 0 ; int mini = a [ ind ] ; for ( int i = ind ; i < n ; i ++ ) { mini = min ( mini , a [ i ] ) ; ans = max ( ans , maximizeSum ( a , n , i + 1 , k - 1 , dp ) + mini ) ; } return dp [ ind ] [ k ] = ans ; } } int main ( ) { int a [ ] = { 5 , 7 , 4 , 2 , 8 , 1 , 6 } ; int k = 3 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int dp [ MAX ] [ MAX ] ; memset ( dp , -1 , sizeof dp ) ; cout << maximizeSum ( a , n , 0 , k , dp ) ; return 0 ; }
Mobile Numeric Keypad Problem | Set 2 | C ++ implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 mylist [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code
#include <iostream> NEW_LINE #include <list> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE int getCount ( int n ) { list < int > mylist [ MAX ] ; mylist [ 0 ] . assign ( { 0 , 8 } ) ; mylist [ 1 ] . assign ( { 1 , 2 , 4 } ) ; mylist [ 2 ] . assign ( { 2 , 1 , 3 , 5 } ) ; mylist [ 3 ] . assign ( { 3 , 6 , 2 } ) ; mylist [ 4 ] . assign ( { 4 , 1 , 7 , 5 } ) ; mylist [ 5 ] . assign ( { 5 , 4 , 6 , 2 , 8 } ) ; mylist [ 6 ] . assign ( { 6 , 3 , 5 , 9 } ) ; mylist [ 7 ] . assign ( { 7 , 4 , 8 } ) ; mylist [ 8 ] . assign ( { 8 , 5 , 0 , 7 , 9 } ) ; mylist [ 9 ] . assign ( { 9 , 6 , 8 } ) ; int Arr [ MAX ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int Arr2 [ MAX ] = { 0 } ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x : mylist [ j ] ) { Arr2 [ x ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } int main ( ) { int n = 2 ; cout << getCount ( n ) ; return 0 ; }
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | C ++ implementation of the approach ; 2 - D dp array saving different states dp [ i ] [ j ] = minimum value of accommodation till i 'th index of the string using j+1 number of buckets. ; Function to find the minimum required sum using dynamic programming ; If both start and bucket reached end then return 0 else that arrangement is not possible so return INT_MAX ; Corner case ; If state if already calculated then return its answer ; Start filling zeroes and ones which to be accommodated in jth bucket then ans for current arrangement will be ones * zeroes + recur ( i + 1 , bucket + 1 ) ; If this arrangement is not possible then don 't calculate further ; Function to initialize the dp and call solveUtil ( ) method to get the answer ; Start with 0 - th index and 1 bucket ; Driver code ; K buckets
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > dp ; int solveUtil ( int start , int bucket , string str , int K ) { int N = str . size ( ) ; if ( start == N ) { if ( bucket == K ) return 0 ; return INT_MAX ; } if ( bucket == K ) return INT_MAX ; if ( dp [ start ] [ bucket ] != -1 ) return dp [ start ] [ bucket ] ; int zeroes = 0 ; int ones = 0 ; int ans = INT_MAX ; for ( int i = start ; i < N ; ++ i ) { if ( str [ i ] == '1' ) ones ++ ; else zeroes ++ ; if ( ones * zeroes > ans ) break ; int temp = solveUtil ( i + 1 , bucket + 1 , str , K ) ; if ( temp != INT_MAX ) { ans = min ( ans , temp + ( ones * zeroes ) ) ; } } return dp [ start ] [ bucket ] = ans ; } int solve ( string str , int K ) { int N = str . size ( ) ; dp . clear ( ) ; dp . resize ( N , vector < int > ( K , -1 ) ) ; int ans = solveUtil ( 0 , 0 , str , K ) ; return ans == INT_MAX ? -1 : ans ; } int main ( ) { string S = "0101" ; int K = 2 ; cout << solve ( S , K ) << endl ; return 0 ; }
Probability of getting more heads than tails when N biased coins are tossed | C ++ implementation of the above approach ; Function to return the probability when number of heads is greater than the number of tails ; Declaring the DP table ; Base case ; Iterating for every coin ; j represents the numbers of heads ; If number of heads is equal to zero there there is only one possibility ; When the number of heads is greater than ( n + 1 ) / 2 it means that heads are greater than tails as no of tails + no of heads is equal to n for any permutation of heads and tails ; Driver Code ; 1 based indexing ; Number of coins ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Probability ( double p [ ] , int n ) { double dp [ n + 1 ] [ n + 1 ] ; memset ( dp , 0.0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1.0 ; for ( int i = 1 ; i <= n ; i += 1 ) { for ( int j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ; } } double ans = 0.0 ; for ( int i = ( n + 1 ) / 2 ; i <= n ; i += 1 ) ans += dp [ n ] [ i ] ; return ans ; } int main ( ) { double p [ ] = { 0.0 , 0.3 , 0.4 , 0.7 } ; int n = sizeof ( p ) / sizeof ( p [ 0 ] ) - 1 ; cout << Probability ( p , n ) ; return 0 ; }
Number of ways to get a given sum with n number of m | C ++ function to calculate the number of ways to achieve sum x in n no of throws ; Function to calculate recursively the number of ways to get sum in given throws and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throws - 1 no of throws left ; Inserting present values in dp ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE int dp [ 55 ] [ 55 ] ; int NoofWays ( int face , int throws , int sum ) { if ( sum == 0 && throws == 0 ) return 1 ; if ( sum < 0 throws == 0 ) return 0 ; if ( dp [ throws ] [ sum ] != -1 ) return dp [ throws ] [ sum ] ; int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throws - 1 , sum - i ) ; } return dp [ throws ] [ sum ] = ans ; } int main ( ) { int faces = 6 , throws = 3 , sum = 12 ; memset ( dp , -1 , sizeof dp ) ; cout << NoofWays ( faces , throws , sum ) << endl ; return 0 ; }
Maximize the happiness of the groups on the Trip | C ++ implementation of the approach ; Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current group ; Current person is a child ; Woman ; Man ; Old person ; Group 's happiness is the sum of happiness  of the people in the group multiplied by  the number of people ; Solution using 0 1 knapsack ; Driver code ; Number of seats ; Groups
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxHappiness ( int A , int N , vector < string > v ) { string str ; int val [ N ] , wt [ N ] , c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { str = v [ i ] ; c = 0 ; for ( int j = 0 ; str [ j ] ; j ++ ) { if ( str [ j ] == ' c ' ) c += 4 ; else if ( str [ j ] == ' w ' ) c += 3 ; else if ( str [ j ] == ' m ' ) c += 2 ; else c ++ ; } c *= str . length ( ) ; val [ i ] = c ; wt [ i ] = str . length ( ) ; } int k [ N + 1 ] [ A + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int w = 0 ; w <= A ; w ++ ) { if ( i == 0 w == 0 ) k [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) k [ i ] [ w ] = max ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] , k [ i - 1 ] [ w ] ) ; else k [ i ] [ w ] = k [ i - 1 ] [ w ] ; } } return k [ N ] [ A ] ; } int main ( ) { int A = 5 ; vector < string > v = { " mmo " , " oo " , " cmw " , " cc " , " c " } ; int N = v . size ( ) ; cout << MaxHappiness ( A , N , v ) ; return 0 ; }
0 | C ++ implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries
#include <bits/stdc++.h> NEW_LINE #define C_MAX  30 NEW_LINE #define max_arr_len  10 NEW_LINE using namespace std ; int dp [ max_arr_len ] [ C_MAX + 1 ] ; bool v [ max_arr_len ] [ C_MAX + 1 ] ; int findMax ( int i , int r , int w [ ] , int n ) { if ( r < 0 ) return INT_MIN ; if ( i == n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i ] [ r ] ; } void preCompute ( int w [ ] , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } int ansQuery ( int w ) { return dp [ 0 ] [ w ] ; } int main ( ) { int w [ ] = { 3 , 8 , 9 } ; int n = sizeof ( w ) / sizeof ( int ) ; preCompute ( w , n ) ; int queries [ ] = { 11 , 10 , 4 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << ansQuery ( queries [ i ] ) << endl ; return 0 ; }
Maximise array sum after taking non | C ++ implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum
#include <bits/stdc++.h> NEW_LINE #define maxLen  10 NEW_LINE using namespace std ; int dp [ maxLen ] ; bool v [ maxLen ] ; int prefix_sum [ maxLen ] ; void findPrefixSum ( int arr [ ] , int n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } int maxSum ( int arr [ ] , int i , int n , int k ) { if ( i + k > n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = 1 ; int x ; if ( i == 0 ) x = prefix_sum [ k - 1 ] ; else x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; dp [ i ] = max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } int main ( ) { int arr [ ] = { 1 , 3 , 7 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 1 ; findPrefixSum ( arr , n ) ; cout << maxSum ( arr , 0 , n , k ) ; return 0 ; }
Find maximum path sum in a 2D matrix when exactly two left moves are allowed | C ++ program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define N  3 NEW_LINE #define M  3 NEW_LINE using namespace std ; int findMaxSum ( int arr [ ] [ M ] ) { int sum = 0 ; int b [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ; b [ i ] [ j ] = max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ; } } return sum ; } int main ( ) { int arr [ N ] [ M ] = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; cout << findMaxSum ( arr ) << endl ; return 0 ; }
Queries to check if string B exists as substring in string A | C ++ implementation of the approach ; Function to return the modular inverse using Fermat 's little theorem ; Function to generate hash ; To store prefix - sum of rolling hash ; Multiplier for different values of i ; Generating hash value for string b ; Generating prefix - sum of hash of a ; Function that returns true if the required sub - string in a is equal to b ; To store hash of required sub - string of A ; If i = 0 then requires hash value ; Required hash if i != 0 ; Comparing hash with hash of B ; Driver code ; Generating hash ; Queries ; Perform queries
#include <bits/stdc++.h> NEW_LINE #define mod  3803 NEW_LINE #define d  26 NEW_LINE using namespace std ; int hash_b ; int * hash_a ; int * mul ; int mi ( int x ) { int p = mod - 2 ; int s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * x ) % mod ; x = ( x * x ) % mod ; p /= 2 ; } return ( s * x ) % mod ; } void genHash ( string & a , string & b ) { hash_a = new int [ a . size ( ) ] ; mul = new int [ a . size ( ) ] ; for ( int i = b . size ( ) - 1 ; i >= 0 ; i -- ) hash_b = ( hash_b * d + ( b [ i ] - 97 ) ) % mod ; mul [ 0 ] = 1 ; hash_a [ 0 ] = ( a [ 0 ] - 97 ) % mod ; for ( int i = 1 ; i < a . size ( ) ; i ++ ) { mul [ i ] = ( mul [ i - 1 ] * d ) % mod ; hash_a [ i ] = ( hash_a [ i - 1 ] + mul [ i ] * ( a [ i ] - 97 ) ) % mod ; } } bool checkEqual ( int i , int len_a , int len_b ) { int x ; if ( i == 0 ) x = hash_a [ len_b - 1 ] ; else { x = ( hash_a [ i + len_b - 1 ] - hash_a [ i - 1 ] + 2 * mod ) % mod ; x = ( x * mi ( mul [ i ] ) ) % mod ; } if ( x == hash_b ) return true ; return false ; } int main ( ) { string a = " abababababa " ; string b = " aba " ; genHash ( a , b ) ; int queries [ ] = { 0 , 1 , 2 , 3 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) { if ( checkEqual ( queries [ i ] , a . size ( ) , b . size ( ) ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } return 0 ; }