Maximize cost of deletions to obtain string having no pair of similar adjacent characters | C ++ program for the above approach ; Function to find maximum cost to remove consecutive characters ; Initialize the count ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Driver Code ; Given string s ; Given cost of removal ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Maxcost ( string s , int cost [ ] ) { int count = 0 ; int maxcost = 0 , i = 0 ; while ( i < s . size ( ) - 1 ) { if ( s [ i ] == s [ i + 1 ] ) { if ( cost [ i ] > cost [ i + 1 ] ) maxcost += cost [ i ] ; else { maxcost += cost [ i + 1 ] ; cost [ i + 1 ] = cost [ i ] ; } } i += 1 ; } return maxcost ; } int main ( ) { string s = " abaac " ; int cost [ ] = { 1 , 2 , 3 , 4 , 5 } ; cout << Maxcost ( s , cost ) ; return 0 ; }