📜  (x,x,x + 1,x + 1)形式的长度为4的子序列数|套装2

📅  最后修改于: 2021-06-26 19:39:17             🧑  作者: Mango

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

前缀和方法:有关前缀和方法,请参阅第1组。

动态编程方法:可以使用动态编程解决此问题。
我们将使用2个数组作为COUNT1 [] [j]COUNT2 [] [10],使得COUNT1 [I] [10]将存储当前索引i从左和COUNT2遍历字符串在数字j的连续相等元件的计[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)