algorithm to generate maximum number of character 'A' using keystrokes 'A', CTRL + 'A', CTRL + 'C' and CTRL + 'V'

Posted by munda on Stack Overflow See other posts from Stack Overflow or by munda
Published on 2011-01-05T17:14:38Z Indexed on 2011/01/05 18:54 UTC
Read the original article Hit count: 223

This is an interview question from google. I am not able to solve it by myself. Can somebody throw some light?

The question goes like this.

Write a program to print the sequence of keystrokes such that it generates the maximum number of character 'A's. You are allowed to use only 4 keys: 'A', CTRL + 'A', CTRL + 'C' and CTRL + 'V'. Only N keystrokes are allowed. All CTRL+ characters are considered as one keystroke, so CTRL+A is one keystroke.

e.g.:

A, ctrl+A, ctrl+C, ctrl+V 

generates two As in 4 keystrokes.

Edit:

CTRL + A is Select All

CTRL + C is copy

CTRL + V is paste

I did some mathematics. For any N, using x numbers of A's , one CTRL+A, one CTRL+C and y CTRL+V, we can generate max ((N-1)/2)^2 numbers of A's. But for some N > M, it is better to use as many ^A, ^C and ^V as it doubles the number of As.

Edit 2:

^A, ^V and ^C will not overwrite on the existing selection but it will append the copied selection to selected one.

© Stack Overflow or respective owner

Related posts about c++

Related posts about algorithm