0

0

将二进制字符串转换为另一个所需的最小前缀翻转次数

WBOY

WBOY

发布时间:2023-08-27 12:13:06

|

952人浏览过

|

来源于tutorialspoint

转载

将二进制字符串转换为另一个所需的最小前缀翻转次数

在这个问题中,我们需要通过翻转第一个二进制字符串的前缀来将其转换为第二个二进制字符串。为了获得最小的前缀翻转次数,我们需要遍历字符串的末尾,如果在两个字符串中找到不匹配的字符,我们需要翻转第一个字符串的前缀。

问题陈述 − 我们给出了两个不同的二进制字符串,分别称为first和second。两个二进制字符串的长度相等,为N。我们需要通过翻转第一个字符串的前缀将其转换为第二个字符串。在这里,我们需要计算将一个字符串转换为另一个字符串所需的总前缀翻转次数。翻转意味着将‘0’转换为‘1’,将‘1’转换为‘0’。

Sample Examples

Input –  first = "001", second = "000"
Output – 2

Explanation

  • First, we need to flip the prefix of length 3 for the first string, and the resultant string will be 110.

  • 在此之后,我们需要翻转长度为2的前缀,结果字符串将为000,与第二个字符串相等。

Input –  first = "10", second = "01"
Output – 1

Explanation

We require to flip the prefix of length 2, and the resultant string will be ‘01’, which is equal to the second string.

Input –  first = "11", second = "11"
Output – 0

Explanation

由于两个字符串相等,我们不需要进行任何翻转。

Approach 1

在这种方法中,我们从字符串的最后开始迭代,如果找到不匹配的字符,我们会翻转长度为‘I + 1’的前缀中的所有字符。这是解决问题的一种简单方法。

算法

  • 步骤 1 - 定义变量 'cnt' 并将其初始化为 0。

  • Step 2 − Start traversing the string from the end using the loop.

  • Step 3 − If first[i] and second[i] are not equal, we need to flip all the characters of the prefix whose length is equal to the I + 1.

  • Step 4 − Use the nest for loop to iterate through the prefix of length equal to I + 1, and flip each character of it by executing the flipBits() function.

  • Step 4.1 − We return ‘0’ if the current character is ‘1’ and ‘1’ if the current character is ‘0’ in the flipBits() function.

  • Step 5 − Increase the value of the ‘cnt’ variable by 1 when we flip the prefix.

  • 步骤 6 - 返回 'cnt' 变量的值。

    Copysmith
    Copysmith

    Copysmith是一款面向企业的 AI 内容创建解决方案

    下载

Example

#include 
using namespace std;
char flipBits(char ch){
   // if the bit is '0', flip it to '1', else flip it to '0'.
   return (ch == '0') ? '1' : '0';
}
int minimumFlips(string first, string second, int n){
   // to store the count of flips
   int cnt = 0;
   
   // Traverse the string
   for (int i = n - 1; i >= 0; i--){
   
      // If first[i] is not equal to second[i]
      if (first[i] != second[i]){
      
         // flip the bits
         for (int j = 0; j <= i; j++){
            first[j] = flipBits(first[j]);
         }
         cnt++;
      }
   }
   return cnt;
}
int main(){
   string first = "001", second = "000";
   int N = first.size();
   cout << "The minimum number of flips of prefixes required to convert the first binary string to the second is - " << minimumFlips(first, second, N);
   return 0;
}

Output

The minimum number of flips of prefixes required to convert the first binary string to the second is - 2

Approach 2

In this approach, we will use the ‘inverted’ variable to track whether the current bit is flipped or not, rather than flipping each prefix bit every time. We also optimized the time complexity of the code in this approach.

算法

  • Step 1 − Define the ‘cnt’ variable and initialize it with ‘0’. Also, define the ‘inverted’ variable and initialize it with a false value.

  • 步骤 2 - 从末尾开始遍历字符串。如果第一个[i]和第二个[i]字符不匹配,请按照以下步骤操作。

  • Step 2.1 − If the value of the ‘inverted’ is false, increase the value of the ‘cnt’ variable by 1, and change the value of the ‘inverted’ variable to true.

  • Step 3 − If both characters match, and the value of the ‘inverted’ is true, we need to flip the bit again. So, increase the value of ‘cnt’ by 1, and change the value of ‘inverted’ to false.

Example

Let’s debug the above algorithm by taking the example.

Input – first = ‘001’, second = ‘000’
  • 在第一步中,第一个[2]和第二个[2]不匹配,'inverted'的值为false。因此,'cnt'的值将变为1,'inverted'将变为true。在这里,通过将'inverted'的值更改为true,我们假设我们已经虚拟地翻转了前缀。

  • After that, the first[1] and second[1] match, but the value of the ‘inverted’ is true. So, the ‘cnt’ value will become 2, and the ‘inverted’ is false.

  • Next, the first[0] and second[0] match, and the value of ‘inverted’ is false. So, we don’t need to perform any operation.

  • 最后,它返回值为‘cnt’,其值为2。

#include 
using namespace std;
// Function to find the minimum number of flips of prefixes required to convert the first binary string to the second
int minimumFlips(string first, string second, int N){

   // number of flips
   int cnt = 0;
   
   // to track whether the current bit is already flipped.
   // When we flip a bit 2 times, it becomes the same as the original bit.
   bool inverted = false;
   
   // Traverse the given string
   for (int i = N - 1; i >= 0; i--){
   
      // If A[i] is not equal to B[i]
      if (first[i] != second[i]){
      
         // If the current bit is not already flipped
         if (!inverted){
            cnt++; // Increase the count of flips
            inverted = true; // Invert all prefix bits
         }
      }
      else{
      
         // If A[i] is equal to B[i], but a current bit is flipped, we need to flip it again
         if (inverted){
         
            // Increase the count of flips
            cnt++;
            
            // Update inverted to false
            inverted = false;
         }
      }
   }
   return cnt;
}
int main(){
   string first = "001", second = "000";
   int N = first.size();
   cout << "The minimum number of flips of prefixes required to convert the first binary string to the second is - " << minimumFlips(first, second, N);
   return 0;
}

Output

The minimum number of flips of prefixes required to convert the first binary string to the second is - 2

Conclusion

我们学习了两种方法来找到将一个字符串转换为另一个字符串所需的最小前缀翻转次数。逻辑是从末尾遍历字符串,如果我们发现不匹配的字符,则翻转前缀。

我们在时间复杂度方面优化了第二段代码,通过使用“inverted”变量来跟踪翻转前缀,而不是像第一种方法那样翻转前缀。

相关专题

更多
excel制作动态图表教程
excel制作动态图表教程

本专题整合了excel制作动态图表相关教程,阅读专题下面的文章了解更多详细教程。

20

2025.12.29

freeok看剧入口合集
freeok看剧入口合集

本专题整合了freeok看剧入口网址,阅读下面的文章了解更多网址。

65

2025.12.29

俄罗斯搜索引擎Yandex最新官方入口网址
俄罗斯搜索引擎Yandex最新官方入口网址

Yandex官方入口网址是https://yandex.com;用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

197

2025.12.29

python中def的用法大全
python中def的用法大全

def关键字用于在Python中定义函数。其基本语法包括函数名、参数列表、文档字符串和返回值。使用def可以定义无参数、单参数、多参数、默认参数和可变参数的函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

python改成中文版教程大全
python改成中文版教程大全

Python界面可通过以下方法改为中文版:修改系统语言环境:更改系统语言为“中文(简体)”。使用 IDE 修改:在 PyCharm 等 IDE 中更改语言设置为“中文”。使用 IDLE 修改:在 IDLE 中修改语言为“Chinese”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

C++的Top K问题怎么解决
C++的Top K问题怎么解决

TopK问题可通过优先队列、partial_sort和nth_element解决:优先队列维护大小为K的堆,适合流式数据;partial_sort对前K个元素排序,适用于需有序结果且K较小的场景;nth_element基于快速选择,平均时间复杂度O(n),效率最高但不保证前K内部有序。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

12

2025.12.29

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

134

2025.12.29

抖音网页版入口在哪(最新版)
抖音网页版入口在哪(最新版)

抖音网页版可通过官网https://www.douyin.com进入,打开浏览器输入网址后,可选择扫码或账号登录,登录后同步移动端数据,未登录仅可浏览部分推荐内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

63

2025.12.29

快手直播回放在哪看教程
快手直播回放在哪看教程

快手直播回放需主播开启功能才可观看,主要通过三种路径查看:一是从“我”主页进入“关注”标签再进主播主页的“直播”分类;二是通过“历史记录”中的“直播”标签页找回;三是进入“个人信息查阅与下载”里的“直播回放”选项。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

18

2025.12.29

热门下载

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

精品课程

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

共16课时 | 0.9万人学习

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

共33课时 | 1.9万人学习

Go语言教程-全程干货无废话
Go语言教程-全程干货无废话

共100课时 | 9.4万人学习

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

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