dynamic-programming

How can we make maximum number of palindromic substrings by rearranging the characters in a string?

久未见 提交于 2020-01-06 14:41:34
问题 A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, abc , ab , and c are substrings of the string abc , while ac and d are not. Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string aaa is 6, because all of its substrings are palindromes. And the

maximum matrix cost path,

走远了吗. 提交于 2020-01-06 05:36:14
问题 I am trying to solve this problem through dynamic programming: You are given a matrix of n rows and m columns. Every element has a number and the rabbit staying at the top left element. Calculate the maximum sum such that the rabbit gets to the bottom element and only two moved allowed: Two steps right and 1 step down ( x +2, y +1); Two steps down and 1 step to the right ( x +1, y +2); Input: The first line contains two naturals n and m ( 1 ≤ n, m ≤ 10 3 ) – the quantity of rows and columns

Python number line cluster exercise

旧城冷巷雨未停 提交于 2020-01-05 06:27:19
问题 I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s) . Why is the book using s in the for loop as well as setting it to the function f(s) ? How should one go about implementing that line in Python? [current code at bottom] My current code is this so far: x

Python number line cluster exercise

霸气de小男生 提交于 2020-01-05 06:25:13
问题 I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s) . Why is the book using s in the for loop as well as setting it to the function f(s) ? How should one go about implementing that line in Python? [current code at bottom] My current code is this so far: x

Python number line cluster exercise

隐身守侯 提交于 2020-01-05 06:25:08
问题 I am working through an exercise in my textbook (Ex 4.7) and am implementing the code in Python to practice dynamic programming. I am having some trouble actually executing Algorithm 4.8. I understand what is going on until I get to 'Otherwise range s from 1 to t-1 and set s to minimize f(s) . Why is the book using s in the for loop as well as setting it to the function f(s) ? How should one go about implementing that line in Python? [current code at bottom] My current code is this so far: x

Is is possible to move from (a,b) to (c,d)

。_饼干妹妹 提交于 2020-01-05 04:21:06
问题 The problem was to output whether it is possible to move from a given point (a,b) to target (c,d) We are restricted to positive co-ordinates only The following two moves are possible (a,b) -> (a+b,b) (a,b) -> (a,b+a) For instance, (1,1) to (5,4) is True You can do the following: Using 2nd move 3 times, (1,1) -> (1,2) -> (1,3) -> (1,4) Using 1st move 1 time (1,4) -> (5,4) I came up with the following recursive method def move(a,b,c,d): if a==c and b==d: return True elif a>c or b>d: return

Wrong answer to dynamic programming problem of finding minimum stair cost

Deadly 提交于 2020-01-05 04:09:15
问题 I am trying to solve the following problem on Leetcode: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1. This is my solution so far. I believe I'm not correctly taking into account the fact that I can start at stair 0, or stair 1, and I'm not sure how to do

Find longest path less than or equal to given value of an acyclic, directed graph in Python

别说谁变了你拦得住时间么 提交于 2020-01-05 04:00:54
问题 So let's say I have an acyclic, directed graph G with nodes a0, a1, a2, b0, b1, b2, c0, c1, c2 and I know both the outgoing distance for each node to any of it's neighbors. What algorithm can I use to find the longest path between any two nodes that is less than a given length? EDIT: I have looked at the Wikipedia page for the Longest path problem but I don't know whether to use the algorithm outlined in the 'Acyclic graphs and critical paths' section or the one in the 'Parameterized

0/1 Knapsack - A few clarification on Wiki's pseudocode

大兔子大兔子 提交于 2020-01-05 02:44:30
问题 Here's the code on en.wikipedia's article on the Knapsack problem: // Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) // Knapsack capacity (W) for w from 0 to W do m[0, w] := 0 end for for i from 1 to n do for j from 0 to W do if j >= w[i] then m[i, j] := max(m[i-1, j], m[i-1, j-w[i]] + v[i]) else m[i, j] := m[i-1, j] end if end for end for I've got two points which my tired brain cannot work out. They're minor, I'm sure, but I could really

one Local Olampyad Questions on Informatic in 2011

烂漫一生 提交于 2020-01-04 15:35:11
问题 An agent is works between n producer and m consumers. i th producer, generates s_i (product like candy) candy and j th consumer consumes (like eat!) b_j candy in this year. For each candy that sales, agent get 1 dollar payoff. For some problems, one strict rule was defined that a producer should be sales candy to any producer that the distance between them is not greater than 100 KM (kilometers). if we have list of all pairs of producer-consumer that the distance between them is lower than