📌  相关文章
📜  计算所有子串的字符权重不超过K

📅  最后修改于: 2021-04-29 06:23:15             🧑  作者: Mango

给定一个包含小英文字母的字符串P和一个包含英文字母所有字符权重的字符串Q ,使得对于所有’i’,0≤Q [i]≤9。任务是找到唯一子字符串的总数权重之和不超过K。

例子:

方法:想法是使用无序集合来存储唯一值。遵循以下步骤来计算答案:

  • 使用嵌套循环遍历所有子字符串,并保持到目前为止遇到的所有字符的权重之和。
  • 如果字符总和不大于K,则将其插入哈希图中。
  • 最后,输出哈希图的大小。

下面是上述方法的实现:

C++
// C++ program to find the count of
// all the sub-strings with weight of
// characters atmost K
#include 
using namespace std;
  
// Function to find the count of
// all the substrings with weight
// of characters atmost K
int distinctSubstring(string& P, string& Q,
                      int K, int N)
{
  
    // Hashmap to store all substrings
    unordered_set S;
  
    // Iterate over all substrings
    for (int i = 0; i < N; ++i) {
  
        // Maintain the sum of all characters
        // encountered so far
        int sum = 0;
  
        // Maintain the substring till the
        // current position
        string s;
  
        for (int j = i; j < N; ++j) {
  
            // Get the position of the
            // character in string Q
            int pos = P[j] - 'a';
  
            // Add weight to current sum
            sum += Q[pos] - '0';
  
            // Add current character to substring
            s += P[j];
  
            // If sum of characters is <=K
            // then insert in into the set
            if (sum <= K) {
                S.insert(s);
            }
  
            else {
                break;
            }
        }
    }
  
    // Finding the size of the set
    return S.size();
}
  
// Driver code
int main()
{
    string P = "abcde";
    string Q = "12345678912345678912345678";
    int K = 5;
    int N = P.length();
  
    cout << distinctSubstring(P, Q, K, N);
  
    return 0;
}


Java
// Java program to find the count of
// all the sub-Strings with weight of
// characters atmost K
import java.util.*;
  
class GFG{
   
// Function to find the count of
// all the subStrings with weight
// of characters atmost K
static int distinctSubString(String P, String Q,
                      int K, int N)
{
   
    // Hashmap to store all subStrings
    HashSet S = new HashSet();
   
    // Iterate over all subStrings
    for (int i = 0; i < N; ++i) {
   
        // Maintain the sum of all characters
        // encountered so far
        int sum = 0;
   
        // Maintain the subString till the
        // current position
        String s = "";
   
        for (int j = i; j < N; ++j) {
   
            // Get the position of the
            // character in String Q
            int pos = P.charAt(j) - 'a';
   
            // Add weight to current sum
            sum += Q.charAt(pos) - '0';
   
            // Add current character to subString
            s += P.charAt(j);
   
            // If sum of characters is <=K
            // then insert in into the set
            if (sum <= K) {
                S.add(s);
            }
   
            else {
                break;
            }
        }
    }
   
    // Finding the size of the set
    return S.size();
}
   
// Driver code
public static void main(String[] args)
{
    String P = "abcde";
    String Q = "12345678912345678912345678";
    int K = 5;
    int N = P.length();
   
    System.out.print(distinctSubString(P, Q, K, N));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python program to find the count of 
# all the sub-strings with weight of 
# characters atmost K
  
# Function to find the count of 
# all the substrings with weight 
# of characters atmost K 
def distinctSubstring(P, Q, K, N): 
  
    # Hashmap to store all substrings 
    S = set()
  
    # Iterate over all substrings 
    for i in range(0,N): 
  
        # Maintain the sum of all characters 
        # encountered so far 
        sum = 0; 
  
        # Maintain the substring till the 
        # current position 
        s = ''
  
        for j in range(i,N):
  
            # Get the position of the 
            # character in string Q 
            pos = ord(P[j]) - 97 
  
            # Add weight to current sum 
            sum = sum + ord(Q[pos]) - 48 
  
            # Add current character to substring 
            s += P[j]
  
            # If sum of characters is <=K 
            # then insert in into the set 
            if (sum <= K):
                S.add(s) 
            else:
                break 
              
    # Finding the size of the set 
    return len(S)
  
# Driver code 
P = "abcde"
Q = "12345678912345678912345678"
K = 5
N = len(P)
  
print(distinctSubstring(P, Q, K, N))
  
# This code is contributed by Sanjit_Prasad


C#
// C# program to find the count of
// all the sub-Strings with weight of
// characters atmost K
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function to find the count of
// all the subStrings with weight
// of characters atmost K
static int distinctSubString(String P, String Q,
                      int K, int N)
{
    
    // Hashmap to store all subStrings
    HashSet S = new HashSet();
    
    // Iterate over all subStrings
    for (int i = 0; i < N; ++i) {
    
        // Maintain the sum of all characters
        // encountered so far
        int sum = 0;
    
        // Maintain the subString till the
        // current position
        String s = "";
    
        for (int j = i; j < N; ++j) {
    
            // Get the position of the
            // character in String Q
            int pos = P[j] - 'a';
    
            // Add weight to current sum
            sum += Q[pos] - '0';
    
            // Add current character to subString
            s += P[j];
    
            // If sum of characters is <=K
            // then insert in into the set
            if (sum <= K) {
                S.Add(s);
            }
    
            else {
                break;
            }
        }
    }
    
    // Finding the size of the set
    return S.Count;
}
    
// Driver code
public static void Main(String[] args)
{
    String P = "abcde";
    String Q = "12345678912345678912345678";
    int K = 5;
    int N = P.Length;
    
    Console.Write(distinctSubString(P, Q, K, N));
}
}
  
// This code is contributed by 29AjayKumar


输出:
7

时间复杂度: O(N 2 )