您的位置:首页 > 其它

Max Number of A's given four keys

2016-06-09 06:31 281 查看
/*
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: