A tiling set such that the Cartesian product obeys the restriction

I read this question which describes the following expression about the problem:

You are given two meanings: Nand K. A moon dog is interested in lines that satisfy the following conditions:

  • The string has exactly Ncharacters, each of which is either "A" or "B".
  • A string shas exactly Kpairs (i, j)( 0 <= i < j <= N-1) such that s[i] = 'A'and s[j] = 'B'.

If there is a string that satisfies the conditions, find and return any such string. Otherwise, return an empty string

It seems to me that this problem is equivalent:

Determine whether 2-partitions exist 0...N-1for which the Cartesian product contains exactly Ktuples (i, j)withi < j

If tuple elements represent string index assignments to Aand B.


This gives a naive (but correct) implementation:

  • Define all 2-partitions of a set 0...N-1
  • For each such partition, a Cartesian product of subsets is produced.
  • For each Cartesian product, count the number of tuples (i, j)for whichi < j
  • Select any 2-section for which this counter K

Here is the implementation in JS:

const test = ([l, r]) =>
  cart(l, r).reduce((p, [li, ri]) => p + (li < ri ? 1 : 0), 0) === k

const indices = _.range(0, n)
const results = partitions(indices).filter(test)

You can check the results in the context of the original problem here . Some examples of outputs for n = 13, are k = 29:

"aababbbbbbbbb", "babaaabbbbbbb", "baabababbbbbb", "abbaababbbbbb", ...

- : S(n, k) k = 2:

enter image description here

, n=13 4095, .

, , ( ), , . , K , i < j.

, - , . . 2- , ?

+4
2

( , , , .)

. , B, . B A , B, .

B , , B N-1 B, BA, BAA... , . N = 13 K = 29, B 3, 10; , . 4 B :

N=13 (number of digits)  
K=29 (number of pairs)  
B= 4 (number of B's)  

(13,29,4) =

(12,20,3) + "B"  
(11,21,3) + "BA"  
(10,22,3) + "BAA"  

, , , (9/2) 2 23. , :

N = N - length of added string  
K = K - number of A still to be added  
B = B - 1  

, B - 1 N - 1, .

, , , B , , , B , , B . . :

function ABstring(N, K, B, str) {
    if ((N - B) * B < K) return;
    str = str || "";
    if (B <= 1 || B >= N - 1) {
        for (var i = N - 1; i >= 0; i--)
            str = (B == 1 && i == K || B == N - 1 && N - 1 - i != K || B == N ? "B" : "A") + str;
        document.write(str + "<br>");
    } else {
        var prefix = "B";
        --B;
        while (--N) {
            if (K - (N - B) >= 0 && B <= N)
                ABstring(N, K - (N - B), B, prefix + str);
            prefix += "A";
        }
    }
}
ABstring(13, 29, 4);
Hide result

B 3 10, 194 (N, K) = (13,29). , B, B 0 N ( , ).


(N, K, B) = (16,24,4):

enter image description here

+1

P - , AB (i, j), s[i] = 'A', s[j] = 'B'.

N, B's , b. , (N-b) A's. S_b. Min P on S_b 0, B's ( O). . P S_b b*(N-b), B's . s S_b .

BAAB. P +1. , O, b B's. , b*(N-b) >= K, s S_b .

b O , N-b . B's, b, b, , b,... B's (m_i) 0 <= m_1 <= m_2 <= ... <= m_b <= N-b.

, AB s N b B's, P(s)=K , K b, <= N-b. , b b*(N-b) >= K.

0

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


All Articles