键盘特殊_特殊键盘

键盘特殊

Problem statement:

问题陈述:

Imagine you have a special keyboard with four types of keys:

想象一下,您有一个特殊的键盘,其中包含四种类型的键:

Key 1: Prints 'I' on screen

按键1:在屏幕上打印“ I”

Key 2: Select whatever printed in screen

键2:选择屏幕上打印的任何内容

Key 3: Copy selection to buffer

关键3:将选择复制到缓冲区

Key 4: Append buffer on-screen 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 I's possible to be printed on the screen.

关键4:在已打印的内容之后在屏幕上追加缓冲区。 如果您只能按键盘N次(使用上述四个键),请编写一个程序以产生最大数量的I(可以在屏幕上打印)。

Input:

输入:

Input is N, number of times keys can be pressed in total.

输入为N ,总共可以按键的次数。

Output:

输出:

Print maximum number of I's possible to print

打印我可以打印的最大数量

Constraints:

限制条件:

1 ≤ N ≤ 75

Example:

例:

Input:
2
Output:
2
Explanation:
We can at most get 2 I's on screen by pressing 
following key sequence Key1, key1.
Pressing other keys have no effect. 
Like key 1, key2 will produce only one I on screen. 
Input:
7
Output:
9
Explanation:
We can at most get 9 I's on screen by pressing 
following key sequence.
Key1, Key1, Key1, Key2, Key3, key4, Key4
I //after pressing key1
I I  //again pressing key 1
I I I //again pressing key1
I I I //pressing key2 selects three I's
I I I // pressing key3 copies these three I's to buffer
I I I I I I // pressing key4 appends these three I's 
I I I I I I I I I // pressing key4 again appends these three I's 

Solution Approach:

解决方法:

Basically,

基本上,

Two things need to be understood to solve this problem

解决此问题需要了解两点

Key4 appends whatever is printed already on screen before 3 key pressing

按下3键之前, Key4会附加屏幕上已经打印的内容

That means at moment 4,

这意味着在第四时刻

You can append whatever was printed while moment 1 as to print in moment 4, you need to press key2 at moment 2 and key3 at moment 3.

您可以在第1时刻添加要在第4时刻打印的内容,然后在第2时刻按key2 ,在第3时刻按key3

So, the recursive function can be written as

因此,递归函数可以写成

Let,

让,

F(n) = max number of I’s printed on screen

F(n) =我在屏幕上打印的最大数量

So, for n>3

因此,对于n> 3

F(n) = max(f(j)*(n-j-1)) for 1<=j<=n-3
Where, 
F(j) = already printed characters up to moment j 
and (n-j-1) is number of appending possible   

So, now we need to convert the recursion into DP.

因此,现在我们需要将递归转换为DP。

1)  Initialize dp[n+1] like following base value;
2)  for i=0 to n
dp[i]=i;
3)  Fill the DP table
for i=4 to n
for j=i-3 to 1,j--
dp[i]=max(dp[i],dp[j]*(i-j-1));
end for
End for
4)  Return dp[n]

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int specialKeyboard(int n)
{
int dp[n + 1];
for (int i = 0; i <= n; i++)
dp[i] = i;
for (int i = 6; i <= n; i++) {
for (int j = i - 3; j >= 1; j--) {
dp[i] = std::max(dp[i], dp[j] * (i - j - 1));
}
}
return dp[n];
}
int main()
{
int t, n, item;
cout << "Input n, number of times keys to be pressed: ";
scanf("%d", &n);
cout << "max no of i's got printed: " << specialKeyboard(n) << endl;
return 0;
}

Output:

输出:

Input n, number of times keys to be pressed: 7
max no of i's got printed: 9

翻译自: https://www.includehelp.com/icp/special-keyboard.aspx

键盘特殊

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543733.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称&#xff1a;日期的简单操作******类函数&#xff1a;构造函数&#xff0c;拷贝构造函数&#xff0c;析构函数&#xff0c;操作符重载函数****日期类操作函数&#xff1a; 1&#xff1a;…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵&#xff0c;安全第一,一个高明的入侵者&#xff0c;不会冒然实行动. 他们在入侵时前会做足功课&#xff0c;入侵时会通过各种技术手段保护自己&#xff0c;以防被对方发现&#xff0c;引火烧身. 其中&#xff0c;跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …

科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

UTF-8&#xff1a;Unicode TransformationFormat-8bit&#xff0c;允许含BOM&#xff0c;但通常不含BOM。是用以解决国际上字符的一种多字节编码&#xff0c;它对英文使用8位&#xff08;即一个字节&#xff09;&#xff0c;中文使用24为&#xff08;三个字节&#xff09;来编码…

vue3实现本地开发使用的px转换成vw,px转换成rem方法整理

前言&#xff1a; 项目中如果想本地开发使用px&#xff0c;但是界面上线以后界面是自适应的效果,可以有多种方式来实现效果。 一、px转成vw 1、安装&#xff0c;安装成功后&#xff0c;node_modules 会新增这两个插件包 npm i postcss-px-to-viewport-8-plugin 2、新增 post…

airplay2协议是什么_什么是AirPlay?

airplay2协议是什么AirPlay (AirPlay) AirPlay is released by Apple in the year 2004. It allows the easy exchange of audios without the use of any wired technique between the two devices. It was previously termed as AirTunes and later got its name changed to …

微信支付开发(5) 订单查询

本文介绍微信支付中订单查询功能的实现。 作者&#xff1a;方倍工作室 地址&#xff1a;http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一、订单查询 因为某一方技术的原因&#xff0c;可能导致商户在预期时间内都收不到最终支付通知&#xff0c;此时商户可以通过该…

ruby 执行函数_Ruby at()函数

ruby 执行函数Ruby中的at()函数 (at() function in Ruby) If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Rubys lib…

python饼形图_Python | 饼形图

python饼形图A pie plot or a pie chart is a circular statistical graphic technique, in which a circle is divided into slices with respect to numerical proportion. In a pie chart, the arc length, central angle, and area of each slice, is proportional to the …

Linux巡检

# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算机名 # lspci -tv # 列出所有PCI设备 # lsusb -tv # 列出所有USB设备 # lsmod # 列出加载的内核模块 # env # 查看环境变量 # fre…

appweb ejs_EJS部分

appweb ejsHi! Welcome. Today, we are going to look at EJS partials. EJS Partials help us avoid repetition of the same code on several web pages. 嗨&#xff01; 欢迎。 今天&#xff0c;我们将看EJS局​​部函数 。 EJS Partials帮助我们避免在多个网页上重复相同的…

Struts2配置

1. 设定server a) window– preferences – myeclipse – servers – tomcat – 6.x b) 选择tomcat homedirectory c) 选择enable d) finish 2. 设定jdk环境 a) window– preferences – java – installed jres b) 如果没有对应的JDK…

ruby继承_Ruby继承

ruby继承Ruby中的继承 (Inheritance in Ruby) Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child cla…

Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题

近期在知乎看到一句话&#xff0c;保持学习的有一种是你看到了很多其它的牛人&#xff0c;不甘心&#xff0c;真的不甘心。Spring和hibernate整合的时候&#xff0c;jsp页面做展现&#xff0c;发现展现属性出现&#xff1a; org.apache.jasper.JasperException: could not init…

sql判断数据库类型数据_SQL数据类型

sql判断数据库类型数据SQL | 资料类型 (SQL | Data Types) Just like other programming languages, facilities of defining data of various types are available in SQL also. SQL supports the following data types for the specification of various data-items or field…

同事反馈环:如何实现持续改进的文化

“魔镜魔镜告诉我&#xff0c;谁才是最美丽的人&#xff1f;”&#xff0c;邪恶的皇后如此问道。似乎在精益和敏捷企业中也会有很多与《白雪公主》中类似的问题&#xff0c;如果我们没有一面可以看到我们正在做什么的镜子&#xff0c;我们就很难搞清楚我们有多么美丽&#xff0…

Scala懒惰瓦尔

Scala | 懒惰的瓦尔 (Scala | lazy val) Scala programming language allows the user to initialize a variable as a lazy val. A lazy variable is used when we need to save memory overheads while object creation. Using the lazy keyword, you can halt the initializ…

经典功率谱估计及Matlab仿真

原文出自&#xff1a;http://www.cnblogs.com/jacklu/p/5140913.html 功率谱估计在分析平稳各态遍历随机信号频率成分领域被广泛使用&#xff0c;并且已被成功应用到雷达信号处理、故障诊断等实际工程中。本文给出了经典功率谱估计的几类方法&#xff0c;并通过Matlab的实验仿真…

ruby 三目运算符_Ruby运算符

ruby 三目运算符Ruby运算符 (Ruby operators) Operators are the symbols which assist compiler or interpreter to carry out certain mathematical, logical and relational tasks and produce the results. Operators are method calls with parameters. 运算符是帮助编译器…