greedy

greediness in sed

99封情书 提交于 2019-12-19 11:44:11
问题 I want ereg ($rat, $dog, $cat) to become preg_match ('#'.$rat.'#', $dog, $cat) To achieve this, I did echo 'ereg ($rat, $dog, $cat)' | sed "s/ereg\(.*\)(\(.*\),/preg_match\1('#'.\2.'#',/g" but, this regex changed the ereg ($rat, $dog, $cat) into preg_match ('#'.$rat, $dog.'#', $cat) instead of preg_match ('#'.$rat.'#', $dog, $cat) Can someone help me to build a regex that changes ereg ($rat, $dog, $cat) into preg_match ('#'.$rat.'#', $dog, $cat) 回答1: Just exclude ','... echo 'ereg ($rat, $dog

Dynamic programming for primitive calculator

寵の児 提交于 2019-12-18 09:08:36
问题 I'm dealing with the problem, that is pretty similar to change coins problem. I need to implement a simple calculator, that can perform the following three operations with the current number x: multiply x by 2, multiply x by 3, or add 1 to x. Goal is given a positive integer n, find the minimum number of operations needed to obtain the number n starting from the number 1. I made a greedy approach to that, bur it shows incorrect results import sys def optimal_sequence(n): sequence = [] while n

Why does the greedy coin change algorithm not work for some coin sets?

删除回忆录丶 提交于 2019-12-17 10:15:46
问题 I understand how the greedy algorithm for the coin change problem (pay a specific amount with the minimal possible number of coins) works - it always selects the coin with the largest denomination not exceeding the remaining sum - and that it always finds the correct solution for specific coin sets. But for some coin sets, there are sums for which the greedy algorithm fails. For example, for the set {1, 15, 25} and the sum 30, the greedy algorithm first chooses 25, leaving a remainder of 5,

Max coverage disjoint intervals

六眼飞鱼酱① 提交于 2019-12-14 02:33:20
问题 Assume you have k<=10^5 intervals [a_i, b_i] \in [1,10^18] (some of them may overlap), and you need to choose a set of intervals mutually disjoint such that their union is maximal. Not maximum number of disjoint intervals, but the union must cover the most. Can't try all possible subsets 2^k infeasible. Greedy approaches ordering by a_i ( interval covering algorithm) and ordering by b_i ( maximum number of disjoint intervals algorithm ) didn't work Can't figure out if there is a dynamic

min change greedy algorithm in java

送分小仙女□ 提交于 2019-12-13 15:07:06
问题 Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's supposed to look like this: > run Coins Enter the amount of given money: [1.73] Give the seller 8 coins: 1 dollars, 2 quarters, 2 dime, 0 nickels, 3 pennies. This is What I have so far: import java.util.Scanner; class Coins { public static void main

Reverse Huffman's algorithm?

对着背影说爱祢 提交于 2019-12-13 08:52:18
问题 I have a problem simlar to Huffman's encoding, I'm not sure exactly how it can be solved or if it is a reverse Huffman's encoding. But it definitely can be solved using a greedy approach. Consider a set of length, each associated with a probability. i.e. X={a1=(100,1/4),a2=(500,1/4),a3=(200,1/2)} Obviously, the sum of all the probabilities = 1. Arrange the lengths together on a line one after the other from a starting point. For example: {a2,a1,a3} in that order from start to finish. Define

How can I get a list of all possible matches in between non-greedy and greedy

给你一囗甜甜゛ 提交于 2019-12-12 19:57:43
问题 I have the string "I like lettuce and carrots and onions" in Python. I thought I could get the following matches ["I like lettuce", "I like lettuce and carrots", "I like lettuce and carrots and onions"] by using a regex like .* and . (The regex should match any character up to " and".) However, using the greedy version ( .* and ) gives me only the last match, and using the non-greedy version ( .*? and ) gives me only the first match. How can I get all three matches? (I do not need a regex

regex greedy problem (C#)

痴心易碎 提交于 2019-12-12 10:43:28
问题 I've a input string like "===text=== and ===text===" and I want to replace wiki syntax with the corresponding html tag. input: ===text=== and ===text=== desirable output: <h1>text</h2> and <h1>text</h2> but with the following code I get this output: var regex = new Regex("---(.+)---"); var output = regex.Replace("===text=== and ===text===", "<h1>$1</h1>"); <h1>text=== and ===text</h1> I know the problem is that my regex matches greedy. But how to make them non greedy. Thank you and kind

length arrangement with probability and cost

人盡茶涼 提交于 2019-12-12 05:39:58
问题 Consider a set of length, each associated with a probability. i.e. X={a1=(100,1/4),a2=(500,1/4),a3=(200,1/2)} Obviously, the sum of all the probabilities = 1. Arrange the lengths together on a line one after the other from a starting point. For example: {a2,a1,a3} in that order from start to finish. Define the cost of an element a_i as its the total length from the starting line to the end of this element multiplied by its probability. So from the previous arrangement: cost(a2) = (500)*(1/4)

Given a grid, would best first search jump to a cell across the board if heuristics are better?

二次信任 提交于 2019-12-11 09:56:37
问题 I am currently implementing the Greedy Best First Search with a 2D array to represent a grid. My implementation right now returns the opened nodes. I am using a PriorityQueue. When I return the traversed path/opened nodes and take a look at the the nodes it appears that the algorithm jumps from one side of the grid to another some times. Is it supposed to do this? It doesn't make sense for a player when traversing the grid to jump to a cell on the other side of the grid just because the