最大可能的平衡二進位子字串拆分,最多花費k個

The array in the C programming language has a fixed size, which means that once the size is specified, it cannot be changed; you can neither shrink it or extend it.
如我們所知,陣列是一組相同資料類型的元素,它們儲存在連續的記憶體區域中。
Given an array of values v[] and a binary array a[]. The objective is to use as many k coins to divide the binary array as much as is possible while ensuring that each segment has 0 equal 1s. i and j are the neighboring indices of the split segment, and the cost of each split is (v[i] - v[j])2.
Problem Statement
實作一個程序,找到最大可能的平衡二進位子字串分割,最多花費k。
範例範例1
Let the Input array be: a: [1,0,0, 1, 0, 0, 1, 1] The given values be: v: [7, 8, 9, 10, 11, 12,13,14] K: 1
Output obtained is: 1
Explanation
由於K的值為1,我們可以在第一個和第二個索引之間進行一次切割。
在這種情況下,[0, 1] 和 [0, 0, 1, 1] 是最終結果的平衡二進位子字串。
進行這個剪切將花費(8 - 9)^ 2 = 1,以及1 = 1。
範例範例2
Let the Input array be: a: [1,0 1, 0, 1, 1, 0,0] The given values be: v: [2, 4, 7, 10, 11, 12, 13, 14] K: 14
Output obtained is: 2
Explanation
The first cut will be made between the first and second index that is 4 and 7, costing us (4 - 7)^2 = 9 and the second cut will be made between the third and fourth index that is 7 and 10 , costing us (7 - 10)^ 2 = 9. No more cuts are possible at this time. The balanced binary substrings in this case that would arise are [1, 0], [1, 0], and [1, 1 would arise are [1, 0], [1, 0], and [1, 1 , 0, 0].
Approach
In order to find maximum possible balanced binary substring splits with at most cost k, we take the following methodology.
Here we take a top-down approach to solve this problem and to find maximum possible balanced binary substring splits with at most cost k.
採用自頂向下的方法,或更廣為人知的動態規劃方法。動態規劃的主要優點是改善了簡單遞歸的效率。動態規劃可以用來最佳化任何包含對相同輸入進行重複呼叫的遞歸解決方案。為了避免以後重新計算子問題的結果,我們的想法是將它們儲存起來。透過這個簡單的最佳化,時間複雜度從多項式降低到指數級。
Algorithm
The algorithm to find maximum possible balanced binary substring splits with at most cost K is given below.
第一步 - 開始
第二步 - 定義一個二維矩陣 m
第三步 - 定義一個函數來找出最大可能的平衡二進位子字串分割。
Step 4 − Define integer variables zeroCount to count the number of zeros and oneCount to count the number of ones respectively
第5步 − 定義一個整數變數cntSplits來計算拆分的數量
第6步 - 遍歷給定的陣列 a
#Step 7 − check whether the number of zeros is equal to the number of ones, then store the maximum feasible one
第8步 - 假設索引位於位置0,然後找出它是1還是0,然後增加計數
#Step 9 − set the cntSplits to zero, if count of one and count of zero is unequal.
第10步 - 將結果值儲存在矩陣中
#Step 11 − Print the desired result obtained
Step 12 − Stop
範例:C程式
這是上述演算法的C程式實現,用於找到最大可能的平衡二進位子字串分割,最多花費k。
#include <stdio.h>
#include <limits.h>
#include <string.h>
//Define a two-dimensional matrix m
int m[1001][1001];
//Define a function to find maximum possible //balanced binary substring splits
int maxSplits(int a[], int v[], int k, int s) {
if (k < 0) {
return INT_MIN;
}
if (m[k][s] != -1) {
return m[k][s];
}
//Define integer variables to count the number of zeros and ones
// Define an integer variable to count the //number of splits
int zeroCount = 0, oneCount = 0;
int cntSplits = 0;
int i;
//Iterating through the given array a
for (i = s - 1; i > 0; i--) {
a[i] == 0 ? zeroCount++ : oneCount++;
// check whether the number of zeros is equal to the number of ones, then store the maximum feasible one
if (zeroCount == oneCount) {
cntSplits = cntSplits > (1 + maxSplits(a, v, k - (v[i] - v[i - 1]) * (v[i] - v[i - 1]), i)) ? cntSplits : (1 + maxSplits(a, v, k - (v[i] - v[i - 1]) * (v[i] - v[i - 1]), i));
}
}
//Suppose the index is at the position 0, then find whether it is a one or a zero. then increment the count
if (i == 0) {
a[0] == 0 ? zeroCount++ : oneCount++;
// set the cntSplits to zero , if count of one and count of zero is unequal.
if (zeroCount != oneCount) {
cntSplits = 0;
}
}
// store the resultant value in the matrix
return m[k][s] = cntSplits;
}
int main() {
int a[] = { 1, 0, 0, 1, 0, 0, 1, 1 };
int v[] = { 7, 8, 9, 10, 11, 12, 13, 14 };
int k = 1;
int s = sizeof(a) / sizeof(a[0]);
//To assign a specific value to a block of memory, we use the memset() function.
memset(m, -1, sizeof(m));
printf("%d\n", maxSplits(a, v, k, s));
return 0;
}
輸出
1
結論
同樣地,我們可以找到最多花費K的可能的平衡二進位子字串分割。
在本文中,解決了獲取程式以最多花費K的條件下找到最大可能的平衡二進位子字串分割的挑戰。
在這裡提供了C程式碼以及找到最大可能的平衡二進位子字串拆分的演算法,最多花費K。
以上是最大可能的平衡二進位子字串拆分,最多花費k個的詳細內容。更多資訊請關注PHP中文網其他相關文章!
熱AI工具
Undress AI Tool
免費脫衣圖片
Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片
AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。
Clothoff.io
AI脫衣器
Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!
熱門文章
熱工具
記事本++7.3.1
好用且免費的程式碼編輯器
SublimeText3漢化版
中文版,非常好用
禪工作室 13.0.1
強大的PHP整合開發環境
Dreamweaver CS6
視覺化網頁開發工具
SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)
在Java中遞歸地計算子字串出現的次數
Sep 17, 2023 pm 07:49 PM
給定兩個字串str_1和str_2。目標是使用遞歸過程計算字串str1中子字串str2的出現次數。遞歸函數是在其定義中呼叫自身的函數。如果str1是"Iknowthatyouknowthatiknow",str2是"know"出現次數為-3讓我們透過範例來理解。例如輸入str1="TPisTPareTPamTP",str2="TP";輸出Countofoccurrencesofasubstringrecursi
快速上手:Java中的JSON數組合併和分割技巧。
Sep 06, 2023 am 10:21 AM
快速上手:Java中的JSON數組合併和分割技巧在現代的軟體開發中,資料的格式和傳輸變得愈發重要。其中,JSON(JavaScriptObjectNotation)是一種常用的資料格式,特別適用於前後端互動和資料儲存。在Java開發中,我們經常需要處理JSON物件和JSON數組。本文將介紹如何在Java中合併和拆分JSON數組,以及實現這些操作的技巧和示
strtok_r()函數是C語言中的一個函數,它的作用是將字串分割成一系列子字串
Aug 26, 2023 am 09:45 AM
該函數與strtok()函數類似。唯一的關鍵區別是_r,它被稱為可重入函數。可重入函數是執行過程中可以被中斷的函數。這種類型的函數可用於恢復執行。因此,可重入函數是線程安全的,這意味著它們可以安全地被線程中斷,而不會造成任何損害。 strtok_r()函數有一個稱為上下文的額外參數。這樣函數就可以在正確的位置恢復。 strtok_r()函數的語法如下:#include<string.h>char*strtok_r(char*string,constchar*limiter,char**
MySQL中如何使用LOCATE函數來尋找子字串在字串中的位置
Jul 25, 2023 am 09:45 AM
MySQL中如何使用LOCATE函數來尋找子字串在字串中的位置在MySQL中,有許多函數可以用來處理字串。其中,LOCATE函數是一種非常有用的函數,可以用來尋找子字串在字串中的位置。 LOCATE函數的語法如下:LOCATE(substring,string,[position])其中,substring為要找的子字串,string為要在其中
如何使用PHP ZipArchive實現多個壓縮包的合併和拆分?
Jul 21, 2023 am 10:17 AM
如何使用PHPZipArchive實現多個壓縮包的合併和拆分?概述:在開發過程中,有時我們需要將多個壓縮包合併成一個,或將一個壓縮包拆分成多個。 PHP提供了ZipArchive擴展,可以輕鬆完成這些操作。本文將介紹如何使用PHPZipArchive實現多個壓縮包的合併與拆分。合併多個壓縮包首先,我們需要建立一個新的壓縮包,並打開它。然後,循環遍歷要合
PHP傳回一個字串在另一個字串中開始位置到結束位置的字串
Mar 21, 2024 am 10:31 AM
這篇文章將為大家詳細講解有關PHP返回一個字符串在另一個字符串中開始位置到結束位置的字符串,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP中使用substr()函數從字串中擷取子字串substr()函數可從字串中擷取指定範圍內的字元。其語法如下:substr(string,start,length)其中:string:要從中提取子字串的原始字串。 start:子字串開始位置的索引(從0開始)。 length(可選):子字串的長度。如果未指定,則提
PHP8.1新增的str_contains函數:快速判斷子字串是否存在
Jul 07, 2023 pm 01:18 PM
PHP8.1新增的str_contains函數:快速判斷子字串是否存在在最新的PHP8.1版本中,新增了一個非常方便的函數str_contains,它的作用是用來快速判斷一個字串是否包含另一個子字串。相較於先前的strpos函數,str_contains函數更加簡潔、易用,且能大幅提升開發效率。本文將向大家介紹str_contains函數的使用方法,並
回文子字串查詢在C++中
Sep 22, 2023 am 09:05 AM
在本教程中,我們需要解決給定字串的回文子串查詢。解決回文子字串查詢比解決C++中的常規查詢複雜得多。它需要更複雜的程式碼和邏輯。在本教程中,我們提供了字串str和Q個子字串[L...R]查詢,每個查詢都有兩個值L和R。我們的目標編寫一個程式來解決查詢以確定substring[L...R]是否是回文。我們必須確定在L到R範圍內形成的子字串是否是回文來解決每個查詢。例如-Let'sinput"abbbabaaaba"asourinputstring.Thequer


