두 개의 문자열 A와 B와 CostA 및 CostB와 같은 두 개의 비용 값이 있다고 가정합니다. A와 B를 동일하게 만들기 위한 최소 비용을 찾아야 합니다. 문자열에서 문자를 삭제할 수 있고 문자열 A에서 삭제하는 비용은 CostA이고 문자열 B에서 삭제하는 데 드는 비용은 CostB입니다. 문자열에서 모든 문자를 제거하는 비용은 동일합니다. 문자열 A ="wxyz", B ="wyzx", CostA는 10, CostB는 20이라고 가정합니다. 따라서 출력은 30이 됩니다. 두 문자열에서 x를 삭제하면 A와 B는 동일합니다. 따라서 비용은 10 + 20 =30입니다.
이것은 Longest Common Subsequence 문제의 변형 중 하나입니다. A와 B에서 LCS의 길이를 찾은 다음 A와 B에서 LCS 길이를 빼야 제거할 문자 수를 얻을 수 있습니다.
예시
#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";
} 출력
Can be represented