Given a set of non-negative integer sets, can some pre-processing quickly convince that some tuple in the set dominates the given tuple of the request?

Thus, this is motivated by a recent question that asked how to quickly determine if a query word can be rebuilt to match a specific word in a given dictionary of words. The basic idea for quickly solving the query was simple: firstly, for the preprocessing for each dictionary of the word, the hash code consists of how many times each letter in the alphabet occurs, and then after the preprocessing for the query word, all you need to do is this is a hash code of the same type of tuples and see if there will be a match in the hash table.

Basically, this problem boiled down to finding out that the tuple of non-negative integers (the number of letters of each letter in the alphabet) exactly matches the tuple in the hash table, where the hash table can be constructed quickly first, and not take up too much memory, compared with the size of the original dictionary of words.

So now I want to expand the problem, and in terms of strings, the expanded problem is whether the query string can be rearranged to match the SUB-sequence of one of the dictionary strings (i.e. not necessarily a continuous subsequence, although an interesting case is also interesting). As for character sets, this is equivalent to determining if the dictionary has a dictionary that dominates the query tuple, i.e. Each account in the dictionary of the dictionary word is greater than or equal to the corresponding account in the court of the query words.

Hoping to get a quick solution, let's say the problem is simply a yes / no answer to the answer to the request, and if so, then only one possible dictionary word (tuple counter) is returned that satisfies the request.

- , / , , , set, , , , , ?

, , , - ( - ), . , - , , d n O (n (log n) ^ ( d-1)) , O ((log n) ^ (d-1)). d , O (nw) n w, .

+4
1

, ( ) . / , .

Python.

itertools Python .

import itertools

, , :

# Keep the values ordered
DATA = (
    (1, 2, 2, 4),
    (1, 3, 5, 5),
    (2, 4),
    (2, 4, 6, 6, 6, 7, 8, 9, 10, 11, 12, 13, 15),
)

:

ALLOWED = {c for d in DATA for l in range(len(d)) for c in itertools.combinations(d, l+1)}

:

assert (1,) in ALLOWED
assert (1, 2) in ALLOWED
assert (1, 3) in ALLOWED
assert (1, 4) in ALLOWED
assert (1, 5) in ALLOWED
assert (1, 6) not in ALLOWED
assert (1, 2, 2, 4) in ALLOWED
assert (1, 2, 2, 5) not in ALLOWED
0

Source: https://habr.com/ru/post/1598427/


All Articles