String Concatenation Requests

I have a list of characters, e.g. x, denoted by b[1], b[2], b[3] ... b[x]. after x,

  • b[x+1]is concatenation b[1],b[2].... b[x]in that order. Similarly

  • b[x+2]is concatenation b[2],b[3]....b[x],b[x+1].

  • So basically it b[n]will be a concatenation of the last xterms b[i]taken from left to right.

  • Given parameters as pwell qas as requests, how can I find out which character from b[1], b[2], b[3]..... b[x]matches q thb[p] matches

Note. xand b[1], b[2], b[3]..... b[x]fixed for all queries.

I tried force formatting, but the length of the string increases exponentially for large x. (x <= 100).


Example:

  • when x=3,

    b[] = a, b, c, a b c, b c abc, c abc bcabc, abc bcabc cabcbcabc, //....  
    //Spaces for clarity, only commas separate array elements
    
  • , , p=7, q=5, , , 3 ( 'c').

.

+4
1

, , , , .

, , b[p][q] x, b[p] p. , , b[p][q], p , 1 x q, 1.

x=3, , :

p  N(p)  b[p]
-  ----  ----
1  1     a
2  1     b
3  1     c
4  3     a b c
5  5     b c abc
6  9     c abc bcabc
7  17    abc bcabc cabcbcabc
8  31    bcabc cabcbcabc abcbcabccabcbcabc
9  57    cabcbcabc abcbcabccabcbcabc bcabccabcbcabcabcbcabccabcbcabc

: N(p) = N(p-1) + N(p-2) + N(p-3), N(p) - pth- b. p x, N [1, p]. , b b[p][q] .

, x=3, p=9 q=45.

  • N(6)=9, N(7)=17 N(8)=31. 45>9+17, , b[9][45] b[8][45-(9+17)] = b[8][19].
  • /, 19>9+5, b[8][19] = b[7][19-(9+5)] = b[7][5].
  • 5>N(4), 5<N(4)+N(5), b[7][5] = b[5][5-3] = b[5][2].
  • b[5][2] = b[3][2-1] = b[3][1]
  • 3 <= x, , b[9][45] - c b[3].

, , p, q, x b x. p N(p) . , .

Python ( , numpy, , ):

def so38509640(b, p, q):
    """
    p, q are integers. b is a char sequence of length x.
    list, string, or tuple are all valid choices for b.
    """
    x = len(b)

    # Trivial case
    if p <= x:
        if q != 1:
            raise ValueError('q={} out of bounds for p={}'.format(q, p))
        return p, b[p - 1]

    # Construct list of counts
    N = [1] * p
    for i in range(x, p):
        N[i] = sum(N[i - x:i])
    print('N =', N)

    # Error check
    if q > N[-1]:
        raise ValueError('q={} out of bounds for p={}'.format(q, p))

    print('b[{}][{}]'.format(p, q), end='')

    # Reduce p, q until it is p < x
    while p > x:
        # Find which previous element character q comes from
        offset = 0
        for i in range(p - x - 1, p):
            if i == p - 1:
                raise ValueError('q={} out of bounds for p={}'.format(q, p))
            if offset + N[i] >= q:
                q -= offset
                p = i + 1
                print(' = b[{}][{}]'.format(p, q), end='')
                break
            offset += N[i]
    print()
    return p, b[p - 1]

so38509640('abc', 9, 45)

N = [1, 1, 1, 3, 5, 9, 17, 31, 57]
b[9][45] = b[8][19] = b[7][5] = b[5][2] = b[3][1]
(3, 'c') # <-- Final answer

, so38509640('abc', 7, 5) :

N = [1, 1, 1, 3, 5, 9, 17]
b[7][5] = b[5][2] = b[3][1]
(3, 'c') # <-- Final answer

, :) , Py2 3, / range.

, . , , - .

+1

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


All Articles