
- 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
Minimum Cost To Make Two Strings Identical in C++
Suppose we have two strings A and B, and another two cost values like CostA, and CostB. We have to find the minimum cost to make A and B identical. We can delete characters from string, the cost for deleting from string A is CostA, and cost for deleting from string B is CostB. Cost of removing all characters from a string is same. Suppose the string A = “wxyz”, B = “wyzx”, CostA is 10 and CostB is 20. So the output will be 30. If we delete x from both the strings, then A and B will be identical. So cost is 10 + 20 = 30.
This is one of the variation of Longest Common Subsequence problem. We have to find the length of LCS from A and B, then subtract LCS length from A and B, thus we can get the number of characters to be removed.
Example
#include <iostream> using namespace std; bool isRepresentedInDDigits(int num, int d, int base) { if (d==1 && num < base) return true; if (d > 1 && num >= base) return isRepresentedInDDigits(num/base, --d, base); return false; } bool checkNumber(int num, int d) { // Check for all bases one by one for (int base=2; base<=32; base++) if (isRepresentedInDDigits(num, d, base)) return true; return false; } int main() { int num = 8; int dig = 2; if(checkNumber(num, dig)) cout << "Can be represented"; else cout << "Can not be represented"; }
Output
Can be represented
- Related Articles
- Minimum Cost to make two Numeric Strings Identical in C++
- Minimum Number of Steps to Make Two Strings Anagram in C++
- Program to find minimum deletions to make strings strings in Python
- Minimum Swaps to Make Strings Equal in C++
- Minimum number of given operations required to make two strings equal using C++.
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Using Counter() in Python 3.x. to find minimum character removal to make two strings anagram
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++
- Minimum move to end operations to make all strings equal in C++
- Minimum ASCII Delete Sum for Two Strings in C++
- Program to split two strings to make palindrome using Python
- Program to find the minimum edit distance between two strings in C++
- Program to find minimum cost to pick up gold in given two locations in Python
- Minimum Cost to Connect Sticks in C++
- Minimum Cost to Merge Stones in C++

Advertisements