What is the minimum number of operations required to copy a string n times?

Recently, I had to duplicate a row a certain number of times. Say that I should have had 5 copies of the string " bacon", and that I had 1 string of " bacon" in my editor. So I would start with this:

bacon

And finish with this:

bacon
bacon
bacon
bacon
bacon

Let's also define three β€œatomic” operations: copy, paste and delete. β€œCopy” allows you to copy any number of lines, β€œpaste” paste everything that was recently copied, and β€œdelete” deletes the line. Therefore, if I have 3 lines of " bacon":

bacon
bacon
bacon

And I want 10 lines of " bacon", I could do something like this:

copy 2  | lines of bacon: 3
paste 2 | lines of bacon: 5
copy 5  | lines of bacon: 5
paste 5 | lines of bacon: 10

"" j , ≀ j? , ? ?

+3
3

, . , , , . , .

, " N". , , "copy N", , ( ).

, , , . , .

, - , :

XXXXXXXXDDDD

( X C, P)

, :

XXXXXXPDDDD

, paste + delete (PD) copy + paste (CP), . , . :

XXXXXXCPDDD

, :

XXXXXXCCCCP

, , :

XXXXXXCP

, .

, + + + . ( , , 4 13).

. Dijkstra ...

[]

, . :

(N) , N . , Copy (N) + Paste + Paste + Paste Copy (N) + Paste + Copy (2N) + Paste.

, , . , .

+3

, + , . 2: 2. , 4 4 .

, + + , . 3: 3 , . , 9x 6 , .

, + + + , . , 4: 4. , 16x 8 .

, + 4 * , 5. 6 . , , 2, 3 4 .

4 , 4x , 3 2 , .

+2

, . .

  • D * (C [DP] *) *. D C (n) β†’ C (n) D D P (n) β†’ P (n) D.

  • (CP *) * D *. C (n) D β†’ D P (n) D β†’ C (n-1) P (n-1). , .

  • (CP *) * | D *.

& le; j, , (CP *) *.

  • (CP *) *. C (n) P (n) P (n) P (n) P (n) P (n) β†’ C (n) P (n) C (2n) P (2n) P (2n) C (n).

  • (CP {0,4}) *. C (n) C (n ') β†’ ( ').

  • (CP {1,4}) *. C (n) P (n) P (n) P (n) P (n) β†’ C (n) P (n) P (n) C (2n) P (2n). , , , .

  • (CP {1,3}) *. C (n) P (n) P (n) P (n) β†’ C (n) P (n) C (2n) P (2n). .

, (CP {1,2}) *. 2 (n) = C (n) P (n) 3 (n) = C (n) P (n) P (n). , partial, . , , . w p a .

. , 2p (n) & hellip; 2? ( '). 2w (n + & delta;) & hellip; 2p (n '- & delta;) 2w (n + n') & hellip;, . 3p (n) & hellip; 3? ( '). , 3p (n) & hellip; 2? ( '). 3p (n + & delta;) & hellip; 2p (1), 3? (N + & delta; +1) & hellip; D . do 2p (n) & hellip; 3? (N '), 2 .

, . 2w (n) 3w (2n) β†’ 3w (n) 2w (3n) 2w (n) 2w (2n) 2w (4n) 2w (8n) β†’ 2w (n) 3w (2n) 3p (5n), 2w {0,3} 3w * (| 2p | 2a? 3w * 3p).

log (n) -

, O (log (n)). 3w , ; . all , O (log (n)). ; , , .

+2

All Articles