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' 变量的值。

    Postme
    Postme

    Postme是一款强大的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”变量来跟踪翻转前缀,而不是像第一种方法那样翻转前缀。

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

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

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

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

精品课程

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

共16课时 | 0.9万人学习

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

共33课时 | 2万人学习

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

共100课时 | 9.8万人学习

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

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