dynamic-programming

Convert string to palindrome string with minimum insertions

家住魔仙堡 提交于 2019-11-30 09:02:20
In order to find the minimal number of insertions required to convert a given string(s) to palindrome I find the longest common subsequence of the string(lcs_string) and its reverse. Therefore the number of insertions to be made is length(s) - length(lcs_string) What method should be employed to find the equivalent palindrome string on knowing the number of insertions to be made? For example : 1) azbzczdzez Number of insertions required : 5 Palindrome string : azbzcezdzeczbza Although multiple palindrome strings may exist for the same string but I want to find only one palindrome? Let S[i, j]

In python, how does one efficiently find the largest consecutive set of numbers in a list that are not necessarily adjacent?

岁酱吖の 提交于 2019-11-30 08:55:27
For instance, if I have a list [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11] This algorithm should return [1,2,3,4,5,6,7,8,9,10,11]. To clarify, the longest list should run forwards. I was wondering what is an algorithmically efficient way to do this (preferably not O(n^2))? Also, I'm open to a solution not in python since the algorithm is what matters. Thank you. Here is a simple one-pass O(n) solution: s = [1,4,2,3,5,4,5,6,7,8,1,3,4,5,9,10,11,42] maxrun = -1 rl = {} for x in s: run = rl[x] = rl.get(x-1, 0) + 1 print x-run+1, 'to', x if run > maxrun: maxend, maxrun = x, run print range(maxend-maxrun

Edit distance recursive algorithm — Skiena

邮差的信 提交于 2019-11-30 08:39:56
I'm reading The Algorithm Design Manual by Steven Skiena, and I'm on the dynamic programming chapter. He has some example code for edit distance and uses some functions which are explained neither in the book nor on the internet. So I'm wondering a) how does this algorithm work? b) what do the functions indel and match do? #define MATCH 0 /* enumerated type symbol for match */ #define INSERT 1 /* enumerated type symbol for insert */ #define DELETE 2 /* enumerated type symbol for delete */ int string_compare(char *s, char *t, int i, int j) { int k; /* counter */ int opt[3]; /* cost of the three

How to count simple paths constrained by ±1 or ±2 steps?

你。 提交于 2019-11-30 07:42:33
问题 I have found this interesting dynamic-programming problem and want to know the approach . We are given an array 'a' of size-'n'. Each element of the array is either '1' or '2'. We start at index '0' . If a[i]=1 , we can go to i+1 or i-1. On the contrary, If a[i]=2 , we can go to i+1 or i+2 or i-1 or i-2. We have to find the number of all possible paths . **Main Constraint ** : - 1) We can go to a particular index in an array only once . 2) We always start at the index-'0' . 3) A path can end

Find maximum product of 3 numbers in an array

徘徊边缘 提交于 2019-11-30 07:25:52
Given an array of integers, which can contain both +ve and -ve numbers. I've to maximize the product of any 3 elements of the array. The elements can be non-contiguous. Some examples: int[] arr = {-5, -7, 4, 2, 1, 9}; // Max Product of 3 numbers = -5 * -7 * 9 int[] arr2 = {4, 5, -19, 3}; // Max Product of 3 numbers = 4 * 5 * 3 I've tried solving it using Dynamic Programming , but I'm not getting the expected result. It is returning the result often involving the same number twice in the multiplication. So, for the array - {4, 2, 1, 9} , it is returning - 32 , which is 4 * 4 * 2 . Here's my

How to find the minimum number of operation(s) to make the string balanced?

谁说胖子不能爱 提交于 2019-11-30 07:22:39
From Codechef : A string is considered balanced if and only if all the characters occur in it equal number of times. You are given a string S ; this string may only contain uppercase English letters. You may perform the following operation any number of times (including zero): Choose one letter in S and replace it by another uppercase English letter. Note that even if the replaced letter occurs in S multiple times, only the chosen occurrence of this letter is replaced. Find the minimum number of operations required to convert the given string to a balanced string. Example: For input: ABCB Here

Optimal filling of grid figure with squares

一个人想着一个人 提交于 2019-11-30 07:13:36
问题 recently I have designed a puzzle for children to solve. However I would like to now the optimal solution. The problem is as follows: You have this figure made up off small squares You have to fill it in with larger squares and it is scored with the following table: | Square Size | 1x1 | 2x2 | 3x3 | 4x4 | 5x5 | 6x6 | 7x7 | 8x8 | |-------------|-----|-----|-----|-----|-----|-----|-----|-----| | Points | 0 | 4 | 10 | 20 | 35 | 60 | 84 | 120 | There are simply to many possible solutions to check

How can I compute the number of characters required to turn a string into a palindrome?

偶尔善良 提交于 2019-11-30 07:06:10
问题 I recently found a contest problem that asks you to compute the minimum number of characters that must be inserted (anywhere) in a string to turn it into a palindrome. For example, given the string: "abcbd" we can turn it into a palindrome by inserting just two characters: one after "a" and another after "d": "a d bcbd a ". This seems to be a generalization of a similar problem that asks for the same thing, except characters can only be added at the end - this has a pretty simple solution in

Dynamic Programming and Knapsack Application

我只是一个虾纸丫 提交于 2019-11-30 05:33:27
Im studying dynamic programming and am looking to solve the following problem, which can be found here http://www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf : You are given a rectangular piece of cloth with dimensions X by Y, where X and Y are positive integers, and a list of n products that can be made using the cloth. For each product i in [1,n] you know that a rectangle of cloth of dimensions ai by bi is needed and that the final selling price of the product is ci. Assume that ai, bi, and ci are all positive integers. You have a machine that can cut any rectangular piece of cloth into

Nth Fibonacci number for n as big as 10^19?

橙三吉。 提交于 2019-11-30 05:29:23
I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) memo[n]=f return f print fib(input()) % 1000000007 My code does not seem to work for large numbers. I get invalid response error. Any suggestions? Getting the Nth fibonacci number when N is 10^19 is not goign to work if you do it the naive way (at least i would guess it won't work). There's a much better way to do it. And this technique works with lots of series' like this.