📌  相关文章
📜  形式为 (x, x, x+1, x+1) 的长度为 4 的子序列的计数 | 2套

📅  最后修改于: 2021-09-06 05:10:00             🧑  作者: Mango

给定一个大小为N的字符串str形式的大量数字,任务是计算长度为 4 的子序列,其数字形式为(x, x, x+1, x+1)
例子:

前缀和方法:请参阅第 1 集,了解前缀和方法。

动态规划方法:这个问题可以使用动态规划解决。
我们将使用 2 个数组作为count1[][j]count2[][10],这样count1[i][10]将存储数字 j在当前索引处的连续相等元素的计数i从左遍历字符串和count2 [i][j]将在当前索引 i 处存储数字 j的连续相等元素的计数,从右侧遍历字符串。以下是步骤:

  • 初始化两个计数数组count1[][10]用于从左到右填充表, count2[][10]用于从输入字符串 的右到左填充表。
  • 遍历输入字符串并填充 count1 和 count2 数组。
  • count1[][] 的循环关系由下式给出:
  • count2[][] 的循环关系由下式给出:
  • 将变量ans初始化为 0 ,以存储稳定数字的结果计数。
  • 遍历输入字符串并从COUNT1 [] []COUNT2 [] []阵列得到的数字的计数,使得从COUNT1数之间的差[] []COUNT2 [] []数组为1并将它存储在变量C1c2。
  • 最后用(c1 * ((c2 * (c2 – 1) / 2)))更新结果(比如ans )
  • 打印上面计算的答案

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the numbers
int countStableNum(string str, int N)
{
    // Array that stores the
    // digits from left to right
    int count1[N][10];
 
    // Array that stores the
    // digits from right to left
    int count2[N][10];
 
    // Initially both array store zero
    for (int i = 0; i < N; i++)
        for (int j = 0; j < 10; j++)
            count1[i][j] = count2[i][j] = 0;
 
    // Fill the table for count1 array
    for (int i = 0; i < N; i++) {
        if (i != 0) {
            for (int j = 0; j < 10; j++) {
                count1[i][j] += count1[i - 1][j];
            }
        }
 
        // Update the count of current character
        count1[i][str[i] - '0']++;
    }
 
    // Fill the table for count2 array
    for (int i = N - 1; i >= 0; i--) {
        if (i != N - 1) {
            for (int j = 0; j < 10; j++) {
                count2[i][j] += count2[i + 1][j];
            }
        }
 
        // Update the count of cuuent character
        count2[i][str[i] - '0']++;
    }
 
    // Variable that stores the
    // count of the numbers
    int ans = 0;
 
    // Traverse Input string and get the
    // count of digits from count1 and
    // count2 array such that difference
    // b/w digit is 1 & store it int c1 &c2.
    // And store it in variable c1 and c2
    for (int i = 1; i < N - 1; i++) {
 
        if (str[i] == '9')
            continue;
 
        int c1 = count1[i - 1][str[i] - '0'];
        int c2 = count2[i + 1][str[i] - '0' + 1];
 
        if (c2 == 0)
            continue;
 
        // Update the ans
        ans = (ans
               + (c1 * ((c2 * (c2 - 1) / 2))));
    }
 
    // Return the final count
    return ans;
}
 
// Driver Code
int main()
{
    // Given String
    string str = "224353";
    int N = str.length();
 
    // Function Call
    cout << countStableNum(str, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the numbers
static int countStableNum(String str, int N)
{
     
    // Array that stores the
    // digits from left to right
    int count1[][] = new int[N][10];
     
    // Array that stores the
    // digits from right to left
    int count2[][] = new int[N][10];
     
    // Initially both array store zero
    for(int i = 0; i < N; i++)
        for(int j = 0; j < 10; j++)
            count1[i][j] = count2[i][j] = 0;
     
    // Fill the table for count1 array
    for(int i = 0; i < N; i++)
    {
        if (i != 0)
        {
            for(int j = 0; j < 10; j++)
            {
                count1[i][j] += count1[i - 1][j];
            }
        }
   
        // Update the count of current character
        count1[i][str.charAt(i) - '0']++;
    }
   
    // Fill the table for count2 array
    for(int i = N - 1; i >= 0; i--)
    {
        if (i != N - 1)
        {
            for(int j = 0; j < 10; j++)
            {
                count2[i][j] += count2[i + 1][j];
            }
        }
         
        // Update the count of cuuent character
        count2[i][str.charAt(i) - '0']++;
    }
     
    // Variable that stores the
    // count of the numbers
    int ans = 0;
     
    // Traverse Input string and get the
    // count of digits from count1 and
    // count2 array such that difference
    // b/w digit is 1 & store it int c1 &c2.
    // And store it in variable c1 and c2
    for(int i = 1; i < N - 1; i++)
    {
        if (str.charAt(i) == '9')
        continue;
         
        int c1 = count1[i - 1][str.charAt(i) - '0'];
        int c2 = count2[i + 1][str.charAt(i) - '0' + 1];
         
        if (c2 == 0)
        continue;
         
        // Update the ans
        ans = (ans + (c1 * ((c2 * (c2 - 1) / 2))));
    }
     
    // Return the final count
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given String
    String str = "224353";
    int N = str.length();
     
    // Function call
    System.out.println(countStableNum(str, N));
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach
 
# Function to count the numbers
def countStableNum(Str, N):
 
    # Array that stores the
    # digits from left to right
    count1 =  [[0 for j in range(10)]
                  for i in range(N)]
   
    # Array that stores the
    # digits from right to left
    count2 =  [[0 for j in range(10)]
                  for i in range(N)]
   
    # Initially both array store zero
    for i in range(N):
        for j in range(10):
            count1[i][j], count2[i][j] = 0, 0
   
    # Fill the table for count1 array
    for i in range(N):
        if (i != 0):
            for j in range(10):
                count1[i][j] = (count1[i][j] +
                                count1[i - 1][j])
               
        # Update the count of current character
        count1[i][ord(Str[i]) - ord('0')] += 1
     
    # Fill the table for count2 array
    for i in range(N - 1, -1, -1):
        if (i != N - 1):
            for j in range(10): 
                count2[i][j] += count2[i + 1][j]
   
        # Update the count of cuuent character
        count2[i][ord(Str[i]) -
                  ord('0')] = count2[i][ord(Str[i]) -
                                        ord('0')] + 1
   
    # Variable that stores the
    # count of the numbers
    ans = 0
   
    # Traverse Input string and get the
    # count of digits from count1 and
    # count2 array such that difference
    # b/w digit is 1 & store it int c1 &c2.
    # And store it in variable c1 and c2
    for i in range(1, N - 1):
        if (Str[i] == '9'):
            continue
   
        c1 = count1[i - 1][ord(Str[i]) - ord('0')]
        c2 = count2[i + 1][ord(Str[i]) - ord('0') + 1]
   
        if (c2 == 0):
            continue
   
        # Update the ans
        ans = (ans + (c1 * ((c2 * (c2 - 1) // 2))))
   
    # Return the final count
    return ans
     
# Driver code
 
# Given String
Str = "224353"
N = len(Str)
 
# Function call
print(countStableNum(Str, N))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count the numbers
static int countStableNum(String str, int N)
{
     
    // Array that stores the
    // digits from left to right
    int [,]count1 = new int[N, 10];
     
    // Array that stores the
    // digits from right to left
    int [,]count2 = new int[N, 10];
     
    // Initially both array store zero
    for(int i = 0; i < N; i++)
        for(int j = 0; j < 10; j++)
            count1[i, j] = count2[i, j] = 0;
     
    // Fill the table for count1 array
    for(int i = 0; i < N; i++)
    {
        if (i != 0)
        {
            for(int j = 0; j < 10; j++)
            {
                count1[i, j] += count1[i - 1, j];
            }
        }
 
        // Update the count of current character
        count1[i, str[i] - '0']++;
    }
 
    // Fill the table for count2 array
    for(int i = N - 1; i >= 0; i--)
    {
        if (i != N - 1)
        {
            for(int j = 0; j < 10; j++)
            {
                count2[i, j] += count2[i + 1, j];
            }
        }
         
        // Update the count of cuuent character
        count2[i, str[i] - '0']++;
    }
     
    // Variable that stores the
    // count of the numbers
    int ans = 0;
     
    // Traverse Input string and get the
    // count of digits from count1 and
    // count2 array such that difference
    // b/w digit is 1 & store it int c1 &c2.
    // And store it in variable c1 and c2
    for(int i = 1; i < N - 1; i++)
    {
        if (str[i] == '9')
        continue;
         
        int c1 = count1[i - 1, str[i] - '0'];
        int c2 = count2[i + 1, str[i] - '0' + 1];
         
        if (c2 == 0)
        continue;
         
        // Update the ans
        ans = (ans + (c1 * ((c2 * (c2 - 1) / 2))));
    }
     
    // Return the readonly count
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given String
    String str = "224353";
    int N = str.Length;
     
    // Function call
    Console.WriteLine(countStableNum(str, N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1

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