📌  相关文章
📜  A 1s、B 10s 和 C 0s字符串中每个 1 的“10”子序列的总和计数

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

A 1s、B 10s 和 C 0s字符串中每个 1 的“10”子序列的总和计数

给定一个A“ 1”、 B “10”和C “0”,任务是计算字符串中每个 1 的“10”个子序列的总和,其中A 1、 B 10 和C 0

例子:

朴素方法:这个问题最简单的解决方案是生成字符串,然后对于每个 1,找到可能的“10”子序列的计数。最后返回所有此类子序列的计数总和。

时间复杂度: O(N*countOf1s)
辅助空间: O(1)

有效方法:使用数学和组合的一些基本概念有效解决问题的想法。

  • 要获得最大子序列,请在最终字符串的开头附加所有“1” ,在最终字符串的末尾附加“0”
  • 声明 ans 变量并通过乘以(A*B)+((B*(B+1))/2)将可能的“10”子序列形式的计数乘以 A 乘以“1”和 B 乘以“10”
  • 通过乘以(A*B)*C将可能的“10”子序列形式的计数乘以 C 乘以“0”和 A 和 B 乘以“1”
  • 返回答案的模数。

下面是上述方法的实现。

C++
// C++ Code for the above approach:
 
#include 
using namespace std;
int maxsubsequence(int A, int B, int C)
{
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long long mod = 1e9 + 7;
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long long ans
        = (A * 1l * B) % mod
          + ((B * 1l * (B + 1)) / 2) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * 1l * C) % mod;
    if (ans >= mod) {
        ans -= mod;
    }
    return ans;
}
 
// Driver code
int main()
{
    int A = 1, B = 2, C = 3;
    cout << maxsubsequence(A, B, C) << endl;
    return 0;
}


Java
// JAVA Code for the above approach:
 
import java.util.*;
class GFG {
  public static int maxsubsequence(int A, int B, int C)
  {
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long mod = (long)(1e9 + 7);
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long ans = (long)(A * B) % mod
      + ((B * (B + 1)) / 2) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * C) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
    return (int)ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int A = 1, B = 2, C = 3;
    System.out.println(maxsubsequence(A, B, C));
  }
}
 
// This code is contributed by Taranpreet


Python3
# python3 Code for the above approach:
def maxsubsequence(A, B, C):
 
    # As the answer may be very large,
    # Find it modulo 109 + 7
    mod = int(1e9 + 7)
 
    # Count possible subsequence by
    # A times"1" and B times"10"
    ans = (A * 1 * B) % mod + ((B * 1 * (B + 1)) // 2) % mod
    if (ans >= mod):
        ans -= mod
 
    # Count possible subsequence
    # By C times "0" and A & B time "1"
    ans += ((A + B) * 1 * C) % mod
    if (ans >= mod):
        ans -= mod
 
    return ans
 
# Driver code
if __name__ == "__main__":
 
    A, B, C = 1, 2, 3
    print(maxsubsequence(A, B, C))
 
# This code is contributed by rakeshsahni


C#
// C# Code for the above approach:
using System;
class GFG{
 
  static int maxsubsequence(int A, int B, int C)
  {
 
    // As the answer may be very large,
    // Find it modulo 109 + 7
    long mod = (long)(1e9 + 7);
 
    // Count possible subsequence by
    // A times"1" and B times"10"
    long ans = (long)(A * B) % mod
      + ((B * (B + 1)) / 2) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
 
    // Count possible subsequence
    // By C times "0" and A & B time  "1"
    ans += ((A + B) * C) % mod;
    if (ans >= mod) {
      ans -= mod;
    }
    return (int)ans;
  }
 
  // Driver code
  static public void Main (){
 
    int A = 1, B = 2, C = 3;
    Console.Write(maxsubsequence(A, B, C));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript



输出
14

时间复杂度: O(1)
辅助空间: O(1)