dynamic-programming

Convert string to palindrome with fewest number of insertions

落花浮王杯 提交于 2019-12-12 18:20:19
问题 This is a question from https://www.dailycodingproblem.com/: Given a string, find the palindrome that can be made by inserting the fewest number of characters as possible anywhere in the word. If there is more than one palindrome of minimum length that can be made, return the lexicographically earliest one (the first one alphabetically). For example, given the string "race", you should return "ecarace", since we can add three letters to it (which is the smallest amount to make a palindrome).

Use dynamic programming to find a subset of numbers whose sum is closest to given number M

佐手、 提交于 2019-12-12 16:31:18
问题 Given a set A of n positive integers a1, a2,... a3 and another positive integer M, I'm going to find a subset of numbers of A whose sum is closest to M. In other words, I'm trying to find a subset A′ of A such that the absolute value |M - 􀀀 Σ a∈A′| is minimized, where [ Σ a∈A′ a ] is the total sum of the numbers of A′. I only need to return the sum of the elements of the solution subset A′ without reporting the actual subset A′. For example, if we have A as {1, 4, 7, 12} and M = 15. Then, the

Edit distance algorithm explanation

爷,独闯天下 提交于 2019-12-12 13:24:44
问题 According to wikipedia, the definition of the recursive formula which calculates the Levenshtein distance between two strings a and b is the following: I don't understand why we don't take into consideration the cases in which we delete a[j] , or we insert b[i] . Also, correct me if I am wrong, isn't the case of insertion the same as the case of the deletion? I mean, instead of deleting a character from one string, we could insert the same character in the second string, and the opposite. So

Maximum sum of absolute differences dynamic programming

非 Y 不嫁゛ 提交于 2019-12-12 09:24:20
问题 We have an array of N elements. I have to choose K elements from this array(you can't take an element with index that is greater than the index of the element of the next one) in such a way that the sum abs(a1-a0)+abs(a2-a1)+...+(ak-ak-1) is maximized. My idea is 2d DP and goes like this: The solution that involves taking the n -th element while having taken k elements is the maximum of the solutions where element with index j smaller than i is the last one, j from k-1 , j<i plus taking the

Solving the Integer Knapsack

*爱你&永不变心* 提交于 2019-12-12 08:31:29
问题 I a new to dynamic programing and have tried the integer knapsack problem here at SPOJ (http://www.spoj.pl/problems/KNAPSACK/) . However, for the given test cases my solution is not giving the correct output. I'd be thankful to you if you could suggest if the following implementation by me is correct. Please note that the variable back is for backtracking, about which I'm not sure how to do. I hope to have your help in implementing the backtracking too. Thanks. #include <cstdio> #include

Grouping Symbols Maximum Length Balanced Subsequence

安稳与你 提交于 2019-12-12 08:15:26
问题 Consider B to be a sequence of grouping symbols (, ), [, ], {, and }. B is called a Balanced sequence if it is of length 0 or B is of one of the following forms: { X } Y or [ X ] Y or { X } Y where X and Y are Balanced themselves. Example for Balanced : ( ) - { [ ] ( ) } [ ] - ... Now the question is to find an efficient algorithm to find maximum length balanced subsequence (not necessarily contiguous) of a given input which is an string of these grouping symbols. For example if input is ( )

Real code in dynamic programming with problems like knapsack in PHP [closed]

心已入冬 提交于 2019-12-12 08:09:18
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Are there any resources where I could find real code which solves problems in Dynamic Programming like knapsack problem and so on in PHP? I want to analyze code by myself, because I can't quite understand theory. And I can't find any code in Google. Thank you very much. 回答1: Here's the problem solved using PHP

Coin Change DP Algorithm Print All Combinations

可紊 提交于 2019-12-12 06:27:28
问题 The classic coin change problem is well described here: http://www.algorithmist.com/index.php/Coin_Change Here I want to not only know how many combinations there are, but also print out all of them. I'm using the same DP algorithm in that link in my implementation but instead of recording how many combinations in the DP table for DP[i][j] = count , I store the combinations in the table. So I'm using a 3D vector for this DP table. I tried to improve my implementation noticing that when

DP - Counting coin change

早过忘川 提交于 2019-12-12 05:49:51
问题 The problem requires to count number of coin changes for a particular cost. For example, if I have coin values of 50, 20, 10, 5, 1 , I can form costs of: 5 => (5), (11111), which are 2 ways. 10 => (10), (5, 5), (5, 11111), (11111, 11111), which are 4 ways. Here is my function. It is returning wrong results begging from cost of 10 (returns 9 ways while the actual number of ways is only 4) int dp[10000]; int coins[] = { 50, 20, 10, 5, 1 }; int rec(int n) { if (n == 0) return 1; if (dp[n] != -1)

Sum all possible values a player can have in a two player game

折月煮酒 提交于 2019-12-12 04:14:13
问题 This is a classical game where two players play following game: There are n coins in a row with different denominations. In this game, players pick a coin from extreme left or extreme right (they blindly pick from any extreme with a probability of .5, both of them are dumb). I just want to count the expected sum of player who starts the game. For this I want to sum up all the possible combinations of values a player can have. I am using a recursive solution which sums up all the possible