js \n直接顯示字符串_顯示N個字符的最短時間

js \n直接顯示字符串

Problem statement:

問題陳述:

You need to display N similar characters on a screen. You are allowed to do three types of operation each time.

您需要在屏幕上顯示N個相似的字符。 每次允許您執行三種類型的操作。

  1. You can insert a character,

    您可以插入一個字符,

  2. You can delete the last character

    您可以刪除最后一個字符

  3. You can copy and paste all displayed characters. After copy operation count of total written character will become twice.

    您可以復制并粘貼所有顯示的字符。 復制操作后,總書寫字符數將變為兩倍。

All the operations are assigned different times.

所有操作均分配有不同的時間。

  • Time for insertion is X

    插入時間為X

  • Time for deletion is Y

    刪除時間為Y

  • Time for copy, paste is Z

    復制時間,粘貼為Z

You need to output minimum time to display N characters on the screen using these operations.

使用這些操作,您需要輸出最短時間以在屏幕上顯示N個字符

Input:

輸入:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N denoting the number of same characters to write on the screen. The next line contains time for insertion, deletion, and copying respectively.

輸入的第一行包含一個整數T,表示測試用例的數量。 然后是T測試用例。 每個測試用例都包含一個整數N,該整數N表示要在屏幕上寫入的相同字符的數量。 下一行分別包含插入,刪除和復制的時間。

Output:

輸出:

Print the minimum time to display N characters on the screen.

打印最短時間以在屏幕上顯示N個字符。

Constraints:

限制條件:

 1 <= T <= 100
1 <= N <= 100
1 <= X, Y, Z <= N

Example:

例:

Input:
Test case: 2
First test case,
N, number of characters to be displayed = 9
Value of X, Y, Z respectively,
1 2 1
N, number of characters to be displayed = 10 
Value of X, Y, Z respectively,
2 5 4
Output:
minimum time for first test case: 5
minimum time for second test case: 14

Explanation:

說明:

For the first test case no of character to be displayed is: 9
Time for insertion is 1
Time for deletion is 2
Time for copy paste is 1
Say the similar character is 'a'
So the best way is
Insert two character
Time is 2
Copy paste
Total character so far is 4
Total time 3
Copy paste again
Total character so far is 8
Total time 4
Insert gain
Total character so far is 9
Total time 5
This is the most optimum way we can solve this

Solution Approach:

解決方法:

This is a recursive problem

這是一個遞歸問題

And we have a few possibilities,

我們有幾種可能性,

  1. Simply insert

    只需插入

  2. Copy-paste

    復制粘貼

  3. Delete

    刪除

Now we need to understand the optimal option based on the situation

現在我們需要根據情況了解最佳選擇

Say a situation where we need to reach character 13 and we are at character 7 displayed already

假設有一種情況,我們需要到達第13個字符,并且已經顯示了第7個字符

Also, Cost of Inserting 6 digits is much higher than one-time copy paste and deleting ( just consider this case, it may not happen always if copy paste option is costly)?

另外,插入6位數字的成本比一次性復制粘貼和刪除要高得多(僅考慮這種情況,如果復制粘貼選項成本很高,則可能不會總是發生)

So, the question is when the "delete" options is useful

因此,問題在于何時使用“刪除”選項

It's useful for such case what I mentioned. So if we formulate the recursion

我提到的這種情況對這種情況很有用。 所以如果我們制定遞歸

Say,

說,

Minimum Time to Display N Character

N = number of characters to be displayed.

N =要顯示的字符數。

So, if n is odd only then we need deletion if n is we even don't need that.

所以,如果n是奇數只有這樣,我們需要刪除,如果n是我們甚至不需要這樣。

Now, we have our recursion, so we can write the recursive function. Since there will be many overlapping sub-problems, we can wither use top-down DP or bottom-up DP. I have used memoization in my implementation. As an exercise, you should be able to convert the above recursion to tabulation DP.

現在,我們有了遞歸,因此我們可以編寫遞歸函數。 由于將存在許多重疊的子問題,因此我們可以使用自頂向下的DP或自底向上的DP。 我在實現中使用了記憶。 作為練習,您應該能夠將上述遞歸轉換為表格DP。

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int dp[101];
int minimum(int a, int b)
{
return a > b ? b : a;
}
int my(int cur, int x, int y, int z)
{
if (cur == 1) {
return x;
}
// meoization, dont compute what's already computed
if (dp[cur] != -1)
return dp[cur];
if (cur % 2 == 0) { //for even n
dp[cur] = minimum(x + my(cur - 1, x, y, z), z + my(cur / 2, x, y, z));
}
else { //for odd n
dp[cur] = minimum(x + my(cur - 1, x, y, z), z + y + my((cur + 1) / 2, x, y, z));
}
return dp[cur];
}
int main()
{
int t, n, item;
cout << "enter number of testcase\n";
cin >> t;
for (int i = 0; i < t; i++) {
cout << "Enter number of characters\n";
cin >> n;
int x, y, z;
cout << "Insert time for insertion, deletion and copy respectively\n";
cin >> x >> y >> z;
for (int i = 0; i <= n; i++)
dp[i] = -1;
cout << "Minimum time is: " << my(n, x, y, z) << endl;
}
return 0;
}

Output:

輸出:

enter number of testcase
3 
Enter number of characters
8 
Insert time for insertion, deletion and copy respectively 
3 2 5 
Minimum time is: 16 
Enter number of characters
8 
Insert time for insertion, deletion and copy respectively 
1 1 3 
Minimum time is: 7
Enter number of characters
3 
Insert time for insertion, deletion and copy respectively 
1 4 6 
Minimum time is: 3

翻譯自: https://www.includehelp.com/icp/minimum-time-to-display-n-character.aspx

js \n直接顯示字符串

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377857.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377857.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377857.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

示例 Demo 工程和 API 參考鏈接

Camera Explorer&#xff1a;有關 Windows Phone8 中有關增強 Camera API 的使用。文章鏈接 Filter Effects&#xff1a;對拍攝的照片或者圖片庫中的照片應用 Nokia Imaging SDK 中的濾鏡。文章鏈接 Filter Explorer&#xff1a;演示了對新拍攝圖片或者現有圖片的編輯功能&…

三、標簽準備

所有操作均在anaconda中的自己配置的環境下進行 一、安裝labelimg 因為YOLO模型所需要的樣本標簽必須是txt類型&#xff0c;本人使用labelimg軟件進行對圖像進行打標簽操作。 pip install pycocotools-windows pip install pyqt5 pip install labelimg 通過labelimg命令打…

ubuntu 8.04安裝應用軟件Can't find X includes錯誤解決辦法

系統很小。應用軟件都的自己裝。 首先把 APT’s database is not updated. # apt-get update    # apt-get upgrade 再裝其它軟件。 make xconfigure 無法運行時&#xff1a; apt-get install qt3-dev-tools 編譯QVFB  是出現&#xff1a; 出現&#xff1a;C preproces…

leetcode 39. 組合總和 思考分析

目錄1、題目2、思考分析3、未經優化代碼4、剪枝優化1、題目 給定一個無重復元素的數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的數字可以無限制重復被選取。 2、思考分析 解空間樹寬度部分即數…

java uuid靜態方法_Java UUID equals()方法與示例

java uuid靜態方法UUID類equals()方法 (UUID Class equals() method) equals() method is available in java.util package. equals()方法在java.util包中可用。 equals() method is used to check whether this object equals to the given object or not. equals()方法用于檢…

一、機器學習概念

一、何為機器學習(Mechine Learning)&#xff1f; 答&#xff1a;利用已有數據(經驗)&#xff0c;來訓練某種模型&#xff0c;利用此模型來預測未來。機器學習是人工智能的核心Mechine Learning。 例如&#xff1a;你和狗蛋兒7點在老槐樹下集合&#xff0c;如何一塊約去開黑&a…

Java線程新特征——Java并發庫

一、線程池 Sun在Java5中&#xff0c;對Java線程的類庫做了大量的擴展&#xff0c;其中線程池就是Java5的新特征之一&#xff0c;除了線程池之外&#xff0c;還有很多多線程相關的內容&#xff0c;為多線程的編程帶來了極大便利。為了編寫高效穩定可靠的多線程程序&#xff0c;…

第一篇博文

剛剛申請博客&#xff0c;開通了&#xff0c;很高興。但是由于這幾天考試比較多&#xff0c;等考完之后&#xff0c;再開始正式寫博客&#xff0c;與諸君共進步&#xff01; 2012/1/1 18:20 轉載于:https://www.cnblogs.com/zhenglichina/archive/2012/01/01/2309561.html

leetcode 40. 組合總和 II 思考分析

題目 給定一個數組 candidates 和一個目標數 target &#xff0c;找出 candidates 中所有可以使數字和為 target 的組合。 candidates 中的每個數字在每個組合中只能使用一次。 思考以及代碼 如果我們直接套用39題的思路&#xff0c;那么就會出現重復的組合。 重復組合的…

java vector_Java Vector size()方法與示例

java vector矢量類size()方法 (Vector Class size() method) size() method is available in java.util package. size()方法在java.util包中可用。 size() method is used to return the size (i.e. the number of the element exists) of this Vector. size()方法用于返回此V…

二、線性回歸

一、回歸 可以拿正態分布為例&#xff0c;比如身高&#xff0c;若平均身高為1.78m&#xff0c;絕大多數人都是1.78m左右&#xff0c;超過2m的很少&#xff0c;低于1m的也不多。 很多事情都會回歸到一定的區間之內&#xff0c;即回歸到平均值。 機器學習沒有完美解&#xff0c…

【轉】HMM學習最佳范例五:前向算法1 .

五、前向算法&#xff08;Forward Algorithm&#xff09; 計算觀察序列的概率&#xff08;Finding the probability of an observed sequence&#xff09; 1.窮舉搜索&#xff08; Exhaustive search for solution&#xff09;  給定隱馬爾科夫模型&#xff0c;也就是在模型參…

vs 字體

看代碼看得眼疼不能不說是程序員的惡夢&#xff0c;那么&#xff0c;選擇適當的字體也算是對自己的救贖吧。周末閑得無聊&#xff0c;在網上亂逛&#xff0c;搜索了一些資料整理一下給大家分享&#xff0c;僅作記錄而已&#xff0c;參考使用&#xff1a; 1.一個編程人員痛苦的選…

leetcode 349. 兩個數組的交集 思考分析

題目 給定兩個數組&#xff0c;編寫一個函數來計算它們的交集。 1、暴力雙for循環 class Solution { public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {vector<int> result;vector<int> res;if(nums1.siz…

random.next_Java Random next()方法與示例

random.next隨機類的next()方法 (Random Class next() method) next() method is available in java.util package. next()方法在java.util包中可用。 next() method is used to return the pseudo-random number in bits. next()方法用于返回以位為單位的偽隨機數。 next() me…

VS2008下QT開發環境搭建

http://blog.csdn.net/sunnyboycao/article/details/6364444 轉載于:https://www.cnblogs.com/bjfuyumu/p/3321180.html

三、梯度下降法求解最優θ值

一、梯度下降法(GD&#xff0c;Gradient Descent) Ⅰ、得到目標函數J(θ)&#xff0c;求解使得J(θ)最小時的θ值 當然&#xff0c;這里只是取了倆特征而已&#xff0c;實際上會有m個特征維度 通過最小二乘法求目標函數最小值 令偏導為0即可求解出最小的θ值&#xff0c;即…

Delphi中Messagedlg用法

if MessageDlg(Welcome to my Delphi application. Exit now?, mtConfirmation, [mbYes, mbNo], 0) mrYes then begin Close; end;MessageDlg用法 對話框類型&#xff1a;mtwarning——含有感嘆號的警告對話框mterror——含有紅色叉符號的錯誤對話框mtinformation——含有藍…

leetcode 131. 分割回文串 思考分析

題目 給定一個字符串 s&#xff0c;將 s 分割成一些子串&#xff0c;使每個子串都是回文串。 返回 s 所有可能的分割方案。 思考 問題可以分為兩個子問題&#xff1a;1、判斷回文串2、分割數組 判斷回文串 bool isPalindrome_string(string s,int startindex,int endinde…

android淡入淡出動畫_在Android中淡入動畫示例

android淡入淡出動畫1) XML File: activity_main 1)XML文件&#xff1a;activity_main <?xml version"1.0" encoding"utf-8"?><android.support.constraint.ConstraintLayout xmlns:android"http://schemas.android.com/apk/res/android&…