Max Number of A's given four keys

来源:互联网 发布:浪潮软件论坛 编辑:程序博客网 时间:2024/06/05 10:36
/*  Imagine you have a special keyboard with following four keys: Key 1: Prints 'A' on screenKey 2: (Ctrl-A): Select screen Key 3: (Ctrl-C): Copy selection to bufferKey 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed. If you can only press the keyboard for N times (with the above four keys), write a program to produce maximum numbers of A's.*/// attention, Ctrl-A and Ctrl-C can copy the text into buffer but not print out.// Ctrl-V is to print out.// Ctrl-A + Ctrl-C + Ctrl-V can be sperate and coherant.#include <iostream>#include <vector>using namespace std;int findMax(int n, vector<int>& maxSolution) {  if(n < 0) return -1;  if(n <= 6) return 6;  int max_so_far = 0;  int max_with_this_i = 0;  int multiplier = 2;  // why start at n - 3?  // Suppose we have input n./*  Suppose we can the following numbes. n - 9 n - 8 n - 7 n - 6            f(n-6), ctrA, ctrlc, ctrlV, ctrlV, ctrlV, ctrV n - 5                    f(n-5) ctrA, ctrlC, ctrlV, ctrlV, ctrV n - 4                          f(n-4) ctrlA, ctrlC, ctrlV, ctrV n - 3                                f(n-3), ctrlA, ctrlC, ctrV n - 2                                        f(n-2) n - 1                                               f(n-1) n                                                           f(n)*/  for(int i = n - 3; i >= 0; --i) {    if(maxSolution[i] == -1) maxSolution[i] = findMax(i, maxSolution);      max_as_with_this_i = multiplier * maxSolution[i];    if(max_as_with_this_i > max_so_far) {      max_so_far = max_as_with_this_i;    }    multiplier += 1;  }  return max_so_far;}

0 0
原创粉丝点击