📜  使二进制字符串“ab”免费的操作计数

📅  最后修改于: 2022-05-13 01:57:07.437000             🧑  作者: Mango

使二进制字符串“ab”免费的操作计数

给定一个仅包含字符'a' 和 'b' 的字符串。将给定的字符串转换为没有“ab”子字符串的字符串。为了使字符串“ab”自由,我们可以执行一个操作,在该操作中我们选择一个“ab”子字符串并将其替换为“bba”。
找出转换给定字符串所需的操作总数。
例子:

Input : s = 'abbaa'
Output : 2
Explanation:
Here, ['ab'baa] is replaced s = [bbabaa]
[bb'ab'aa] is replaced s = [bbbbaaa]
which is ab free. Hence, 2 operations required.

Input : s = 'aab'
Output : 3
Explanation:
Here, [a'ab'] is replaced s = [abba]
['ab'ba] is replaced s = [bbaba]
[bb'ab'a] is replaced s = [bbbbaa]
which is ab free. Hence, 3 operations required.

方法:
最终状态将是 'b' 之后的某个字符'a': “bbb...baaa...a”
很明显,证明所有 'b' 是彼此不同的(即每个 'b' 在初始状态,将添加一些 'b' 到与其他 'b' 不相交的最终状态)。对于初始状态的字符“b”,在看到字符“a”后它会加倍。对于每个第i个字符“b”,考虑 t i在它之前的 a 的数量。因此,'b' 的最终数量可以定义为 2^t i的总和。
下面是上述方法的实现。

C++
// C++ program to find all subsets of given set. Any
// repeated subset is considered only once in the output
#include
using namespace std;
 
// code to make 'ab' free string
int abFree(string s)
{
    int n = s.length();
    char char_array[n + 1];
     
    // convert string into char array
    strcpy(char_array, s.c_str());
     
    // Traverse from end. Keep track of count
    // b's. For every 'a' encountered, add b_count
    // to result and double b_count.
    int b_count = 0;
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        if (char_array[n - i - 1] == 'a')
        {
            res = (res + b_count);
            b_count = (b_count * 2);
        } else {
            b_count += 1;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    string s = "abbaa";
    cout<


Java
// Java program to find all subsets of given set. Any
// repeated subset is considered only once in the output
import java.util.*;
 
class GFG
{
 
    // code to make 'ab' free string
    static int abFree(char[] s)
    {
 
        // Traverse from end. Keep track of count
        // b's. For every 'a' encountered, add b_count
        // to result and double b_count.
        int b_count = 0;
        int res = 0;
        for (int i = 0; i < s.length; i++)
        {
            if (s[s.length - i - 1] == 'a')
            {
                res = (res + b_count);
                b_count = (b_count * 2);
            } else {
                b_count += 1;
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "abbaa";
        System.out.println(abFree(s.toCharArray()));
 
        s = "aab";
        System.out.println(abFree(s.toCharArray()));
 
        s = "ababab";
        System.out.println(abFree(s.toCharArray()));
    }
}
 
// This code is contributed by Princi Singh


Python3
# code to make 'ab' free string
def abFree(s):
    
    # Traverse from end. Keep track of count
    # b's. For every 'a' encountered, add b_count
    # to result and double b_count.
    b_count = 0
    res = 0
    for i in range(len(s)):
        if s[~i] == 'a':
            res = (res + b_count)
            b_count  = (b_count  * 2)
        else:
            b_count  += 1
    return res
 
# driver code
s = 'abbaa'
print(abFree(s))
 
s = 'aab'
print(abFree(s))
 
s ='ababab'
print(abFree(s))


C#
// C# program to find all subsets of given set.
// Any repeated subset is considered
// only once in the output
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // code to make 'ab' free string
    static int abFree(char[] s)
    {
 
        // Traverse from end. Keep track of count
        // b's. For every 'a' encountered, add b_count
        // to result and double b_count.
        int b_count = 0;
        int res = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (s[s.Length - i - 1] == 'a')
            {
                res = (res + b_count);
                b_count = (b_count * 2);
            } else
            {
                b_count += 1;
            }
        }
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "abbaa";
        Console.WriteLine(abFree(s.ToCharArray()));
 
        s = "aab";
        Console.WriteLine(abFree(s.ToCharArray()));
 
        s = "ababab";
        Console.WriteLine(abFree(s.ToCharArray()));
    }
}
 
// This code contributed by Rajput-Ji


Javascript


输出:

2
3
11