0

0

最大可能的平衡二进制子字符串拆分,最多花费k个

WBOY

WBOY

发布时间:2023-08-29 09:41:07

|

1249人浏览过

|

来源于tutorialspoint

转载

最大可能的平衡二进制子字符串拆分,最多花费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 an equal amount of 0s and 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, 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

  • 第三步 - 定义一个函数来找到最大可能的平衡二进制子字符串分割。

    Copilot
    Copilot

    Copilot是由微软公司开发的一款AI生产力工具,旨在通过先进的人工智能技术,帮助用户快速完成各种任务,提升工作效率。

    下载
  • 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 
#include 
#include 
//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。

相关专题

更多
python中print函数的用法
python中print函数的用法

python中print函数的语法是“print(value1, value2, ..., sep=' ', end=' ', file=sys.stdout, flush=False)”。本专题为大家提供print相关的文章、下载、课程内容,供大家免费下载体验。

183

2023.09.27

数据类型有哪几种
数据类型有哪几种

数据类型有整型、浮点型、字符型、字符串型、布尔型、数组、结构体和枚举等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

296

2023.10.31

php数据类型
php数据类型

本专题整合了php数据类型相关内容,阅读专题下面的文章了解更多详细内容。

216

2025.10.31

typedef和define区别
typedef和define区别

typedef和define区别在类型检查、作用范围、可读性、错误处理和内存占用等。本专题为大家提供typedef和define相关的文章、下载、课程内容,供大家免费下载体验。

100

2023.09.26

define的用法
define的用法

define用法:1、定义常量;2、定义函数宏:3、定义条件编译;4、定义多行宏。更多关于define的用法的内容,大家可以阅读本专题下的文章。

312

2023.10.11

if什么意思
if什么意思

if的意思是“如果”的条件。它是一个用于引导条件语句的关键词,用于根据特定条件的真假情况来执行不同的代码块。本专题提供if什么意思的相关文章,供大家免费阅读。

711

2023.08.22

counta和count的区别
counta和count的区别

Count函数用于计算指定范围内数字的个数,而CountA函数用于计算指定范围内非空单元格的个数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

192

2023.11.20

while的用法
while的用法

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。本专题为大家提供while相关的文章、下载、课程内容,供大家免费下载体验。

81

2023.09.25

桌面文件位置介绍
桌面文件位置介绍

本专题整合了桌面文件相关教程,阅读专题下面的文章了解更多内容。

0

2025.12.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Swoft2.x速学之http api篇课程
Swoft2.x速学之http api篇课程

共16课时 | 0.9万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号