Creating a binary string with K-pairs

I am doing AB problem on TopCoder, and my code passes all the test cases of the system except one. This is the statement of 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

My algorithm should have started with a string of length Nconsisting of all As. Initially, the number of pairs is 0. When moving the line, if the number of pairs is less than K, I replace the rightmost one with Bs, starting at the end of the line. If the number of pairs becomes larger than K, I replace As at the beginning of the line Bs. The number of pairs at any given time countOfAs * countOfBs.

string createString(int n, int k) {
  string result(n, 'A'); // "AAAA....A"
  int i = 0, j = n - 1; // indexes to modify the string
  int numPairs = 0; // number of pairs
  int countA = n; // count of As in the string
  int countB = 0; // count of Bs in the string
  do {
    if (numPairs > k) {
      result[i++] = 'B';
    }
    else if (numPairs < k) {
      result[j--] = 'B';
      countB++;
    }
    else {
      return result;
    }
    countA--;
    numPairs = countA * countB;
  } while (numPairs != 0); // numPairs will eventually go to 0 as more Bs are added
  return "";
}

A test that is not suitable for me is this N=13, K=29. Kbeing a prime does not exist countOfAs * countOfBs, which is equal K.

The sample answer gave "AAAABBBBBBABA"as an answer (because you can create pairs from the first 4 As, the first 6 Bs, the second for the last A and last B, ie 4*6 + 4*1 + 1*1=29)

+4
source share
3 answers

, B:

A , B K ; :

N=13, K=29  

0123456789ABC  
aaaaaaaaaaaab  <-  position 12 creates 12 pairs  

N = , K = K - + #B = 18 #B = 1, #B - B, . B X X, , B #B; K #B .

N=12, K=18, #B=1  

0123456789AB  
aaaaaaaaaaab  <-  position 11 adds 11 pairs  

N = 11, K = K - 11 + #B = 9, #B = 2:

N=11, K=9, #B=2  

0123456789A  
aaaaaaaaaba  <-  position 9 creates 9 pairs  

, , :

aaaaaaaaababb

, : K & ge; N a B , K < N a B K, .

N/2 B, K , ; , , (N/2) 2 K.

function ABstring(N, K, B) {
    if (B == undefined) {                     // top-level recursion
        if ((N / 2) * (N / 2) < K) return ""; // return if impossible
        B = 0;
    }
    if (K >= N) return ABstring(N - 1, K - (N - 1) + B + 1, B + 1) + 'B';
    var str = "";
    for (var i = 0; i < N; i++) str += (K && i == K) ? 'B' : 'A';
    return str;
}
document.write(ABstring(13, 29));
Hide result

, , , . B , :

aaaabaaaabbbb  

, , , B , B :

aaaabaaaabbbb  
aaaaabaababbb  
aaaaaabbaabbb  

, , .

+2

, K [0, N-1] , :

B B B ... A B ... B
          ^
          index: N-1-K

K, A:

A B B ... A B ... B
          ^
          index: (N-1)-(K-(N-2)) = 2N-3-K

K [N, 2N-4].

p As, K [(p-1)*(N-p), p*(N-p)], (p-1) As , A .

, N=19 K=23, :

A B B ... A B B B B B B
          ^
          index: 2N-3-K = 38-3-23 = 12
+1

:

for a given pair of positive integers, (a,b),
the highest product achievable when
sum(a,b) is fixed is when a ≈ b since
a^2 is necessarily greater than (a+1)*(a-1)

:

a*b + a_0*b_0 + a_1*b_1 ... = K,
where a + b + a_0 + a_1 ... <= N
(meaning only the first b term is included in the second sum)

, K,

K = p*p' + 1*m, where p ≈ p' and m <= max(p, p')

,

p + p' + 1
(remember the 1*m represents our a_0*b_0 above, 
where only the a_0 is summed and b_0, which is m, is ignored)

, , :

let A = round(sqrt(K))
let B = floor(K / A)
let M = remainder(K / A)

min(A,B) 'a's
followed by max(A, B) 'b's
with an additional 'a' inserted M 'b back
for a total of A + B + 1 characters
(the rest can be filled with 'a's)

:

N = 13
K = 29

A = round(sqrt(29)) = 5
B = floor(29 / 5) = 5
M = remainder(29 / 5) = 4

aaaaa
aaaaabbbbb
aaaaababbbb (5 + 5 + 1 = 11 characters)
       <--M 'b back 

Solution: aaaaababbbbaa
+1

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


All Articles