greedy

Matching text between delimiters: greedy or lazy regular expression?

给你一囗甜甜゛ 提交于 2019-12-02 19:15:13
For the common problem of matching text between delimiters (e.g. < and > ), there's two common patterns: using the greedy * or + quantifier in the form START [^END]* END , e.g. <[^>]*> , or using the lazy *? or +? quantifier in the form START .*? END , e.g. <.*?> . Is there a particular reason to favour one over the other? Some advantages: [^>]* : More expressive. Captures newlines regardless of /s flag. Considered quicker, because the engine doesn't have to backtracks to find a successful match (with [^>] the engine doesn't make choices - we give it only one way to match the pattern against

What is the difference between Greedy-Search and Uniform-Cost-Search?

一世执手 提交于 2019-12-02 18:57:08
When searching in a tree, my understanding of uniform cost search is that for a given node A, having child nodes B,C,D with associated costs of (10, 5, 7), my algorithm will choose C, as it has a lower cost. After expanding C, I see nodes E, F, G with costs of (40, 50, 60). It will choose 40, as it has the minimum value from both 3. Now, isn't it just the same as doing a Greedy-Search, where you always choose what seems to be the best action? Also, when defining costs from going from certain nodes to others, should we consider the whole cost from the beginning of the tree to the current node,

Regex - Greedyness - matching HTML tags, content and attributes

倖福魔咒の 提交于 2019-12-02 14:45:00
问题 I am trying to match specific span-tags from an HTML source. The lang-attribute and the inner HTML of the tag are used as parameters for a function which returns a new string. I want replace the old tags, attributes and content with the result of the called function. The subject would be something like this: <p>Some codesnippet:</p> <span lang="fsharp">// PE001 let p001 = [0..999] |> List.filter (fun n -> n % 3 = 0 || n % 5 = 0) |> List.sum </span> <p>Another code snippet:</p> <span lang="C#"

Minimum number of line segments to cover a bigger line

落爺英雄遲暮 提交于 2019-12-02 12:09:39
问题 I am given the coordinates of n line segments (1-dimensional) of same length, and I need to find the minimal number of these line segments to fully cover the bigger line or find out that this is impossible. The bigger line starts from 0 and ends at L . The line segments can start from the range [0-D, L-D] and all have the same length 2*D . So, for example for the following input: 15 2 4 // L, n, D -2 7 // beginning coordinates of line segments 21 14 4 10 4 6 3 16 17 -1 2 14 11 12 8 5 1 9 9 3

Regex - Greedyness - matching HTML tags, content and attributes

别来无恙 提交于 2019-12-02 08:58:40
I am trying to match specific span-tags from an HTML source. The lang-attribute and the inner HTML of the tag are used as parameters for a function which returns a new string. I want replace the old tags, attributes and content with the result of the called function. The subject would be something like this: <p>Some codesnippet:</p> <span lang="fsharp">// PE001 let p001 = [0..999] |> List.filter (fun n -> n % 3 = 0 || n % 5 = 0) |> List.sum </span> <p>Another code snippet:</p> <span lang="C#">//C# testclass class MyClass { } </span> In order to extract the value of the lang attribute and the

Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of each group is minimized

大憨熊 提交于 2019-12-01 08:48:01
Given N items with values x[1], ..., x[n] and an integer K find a linear time algorithm to arrange these N items in K non empty groups such that in each group the range (difference between minimum and maximum element values/keys in each group) is minimized and therefore the sum of the ranges is minimum. For example given N=4 , K=2 and the elements 1 1 4 3 the minimum range is 1 for groups (1,1) and (4,3) . You can binary search the answer. Assume the optimal answer is x . Now you should verify whether we can group the items into k groups where the maximum difference between the group items is

Arrange n items in k nonempty groups such that the difference between the minimum element and the maximum element of each group is minimized

帅比萌擦擦* 提交于 2019-12-01 06:13:35
问题 Given N items with values x[1], ..., x[n] and an integer K find a linear time algorithm to arrange these N items in K non empty groups such that in each group the range (difference between minimum and maximum element values/keys in each group) is minimized and therefore the sum of the ranges is minimum. For example given N=4 , K=2 and the elements 1 1 4 3 the minimum range is 1 for groups (1,1) and (4,3) . 回答1: You can binary search the answer. Assume the optimal answer is x . Now you should

php regex: lookbehind and lookahead and greediness problem

好久不见. 提交于 2019-12-01 05:29:31
This should be simple but I'm a noob and I can't for the life of me figure it out. I'm trying to use regex to match text inside of special open/close tags: [p2][/p2] So in this text: apple [p2]banana[/p2] grape [p2]lemon[/p2] it should match "banana" and "lemon". The regex I've worked up so far is: (?<=\[p2\]).+(?=\[\/p2\]) But this is too greedy. It matches starting with the "b" in banana and ends with the "n" in lemon, matching banana[/p2] grape [p2]lemon . How do I just match banana and lemon? This should do it: (?<=\[p2\]).+?(?=\[\/p2\]) I added the question mark to make the quantifier non

php regex: lookbehind and lookahead and greediness problem

十年热恋 提交于 2019-12-01 03:04:24
问题 This should be simple but I'm a noob and I can't for the life of me figure it out. I'm trying to use regex to match text inside of special open/close tags: [p2][/p2] So in this text: apple [p2]banana[/p2] grape [p2]lemon[/p2] it should match "banana" and "lemon". The regex I've worked up so far is: (?<=\[p2\]).+(?=\[\/p2\]) But this is too greedy. It matches starting with the "b" in banana and ends with the "n" in lemon, matching banana[/p2] grape [p2]lemon . How do I just match banana and

Dynamic Programming Solution for Activity-selection

…衆ロ難τιáo~ 提交于 2019-12-01 01:22:56
In 16.1 An activity-selection problem of Introduction to Algorithm , the dynamic programming solution for this problem was given as c[i, j] = 0 if S(i, j) is empty c[i, j] = max { c[i, k] + c[k, j] + 1 } if S(i, j) is not empty where S(i, j) denotes the set of activities that start after activity a(i) finishes and that finish before activity a(j) starts, and c[i, j] denotes the size of an optimal solution for the set S(i, j) However, I am thinking of another simpler solution c[i] = max { c[i - 1], c[f(i)] + 1 } where f(i) gives the activity that is compatible with a(i) and has the max finish