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; }
相关文章推荐
- 网络流入门
- Compare Version Numbers
- linux cacti简单配置文档
- Maximum Depth of Binary Tree
- leecode_240 Search a 2D Matrix II
- Servlet & JSP 面试题
- 这些屌炸天的创业者为何对投资人说NO
- 顺为资本CEO许达来:为什么说中国创业者很幸福?(附PPT)
- 星瀚资本杨歌:我七次创业失败的内心感悟(比较真实,可以看看创业的36条军规)
- 晨兴资本刘芹:入行16年我才刚理解创投,有8个最深感悟
- Add Digits
- python 字符编码练习
- 实现统一社会信用代码校验
- 初见微服务之服务注册与发现
- ★【16.6.2】Codeforces Round #356 (Div. 2) C. Bear and Prime 100
- [CSS] Targeting Elements with CSS Attribute Selectors
- python3实现暴力穷举博客园密码
- [TypeScript] Using Exclude and RootDir until File Globs Lands in 2.0.
- [TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS
- ★【16.6.2】Codeforces Round #356 (Div. 2) B. Bear and Finding Criminals