📌  相关文章
📜  通过除去非空子字符串获得的所有可能字符串的总和

📅  最后修改于: 2021-04-27 05:23:31             🧑  作者: Mango

给定由N个整数组成的数字字符串str ,任务是在除去非空子字符串之后找到所有可能的结果字符串的总和。

例子:

方法:要解决此问题,需要进行以下观察:

请按照以下步骤解决问题:

  • 预计算10的幂并存储在一个数组中powers []
  • 将给定数字字符串的数字的前缀和存储在数组pref []中
  • 应用上面获得的公式,对于从0N – 1的每个x ,计算总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
const int N = 10;
 
int pref[N], power[N];
 
// Function to convert a character
// to its equivalent digit
int toDigit(char ch)
{
    return (ch - '0');
}
 
// Function to precompute powers of 10
void powerOf10()
{
    power[0] = 1;
    for (int i = 1; i < N; i++)
        power[i] = power[i - 1] * 10;
}
 
// Function to precompute prefix sum
// of numerical strings
void precomputePrefix(string str, int n)
{
    pref[0] = str[0] - '0';
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1]
                  + toDigit(str[i]);
}
 
// Function to return the i-th
// term of Triangular Number
int triangularNumber(int i)
{
    int res = i * (i + 1) / 2;
    return res;
}
 
// Function to return the sum
// of all resulting strings
int sumOfSubstrings(string str)
{
    int n = str.size();
 
    // Precompute powers of 10
    powerOf10();
 
    // Precompute prefix sum
    precomputePrefix(str, n);
 
    // Initialize result
    int ans = 0;
 
    for (int i = 0; i < n - 1; i++) {
 
        // Apply the above general
        // formula for every i
        ans += (pref[n - i - 2] * (i + 1)
                + toDigit(str[n - i - 1])
                      * triangularNumber(
                            n - i - 1))
               * power[i];
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    string str = "1234";
 
    // Function Call
    cout << sumOfSubstrings(str);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
static int N = 10;
static int []pref = new int[N];
static int[] power = new int[N];
 
// Function to convert a
// character to its equivalent
// digit
static int toDigit(char ch)
{
  return (ch - '0');
}
 
// Function to precompute
// powers of 10
static void powerOf10()
{
  power[0] = 1;
   
  for (int i = 1; i < N; i++)
    power[i] = power[i - 1] * 10;
}
 
// Function to precompute prefix sum
// of numerical Strings
static void precomputePrefix(char[] str,
                             int n)
{
  pref[0] = str[0] - '0';
   
  for (int i = 1; i < n; i++)
    pref[i] = pref[i - 1] +
              toDigit(str[i]);
}
 
// Function to return the i-th
// term of Triangular Number
static int triangularNumber(int i)
{
  int res = i * (i + 1) / 2;
  return res;
}
 
// Function to return the sum
// of all resulting Strings
static int sumOfSubStrings(String str)
{
  int n = str.length();
 
  // Precompute powers of 10
  powerOf10();
 
  // Precompute prefix sum
  precomputePrefix(
  str.toCharArray(), n);
 
  // Initialize result
  int ans = 0;
 
  for (int i = 0; i < n - 1; i++)
  {
    // Apply the above general
    // formula for every i
    ans += (pref[n - i - 2] * (i + 1) +
            toDigit(str.charAt(n - i - 1)) *
            triangularNumber(n - i - 1)) *
            power[i];
  }
 
  // Return the answer
  return ans;
}
 
// Driver Code
public static void main(String[] args)
{
  String str = "1234";
 
  // Function Call
  System.out.print(sumOfSubStrings(str));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for the
# above approach
N = 10
pref = [0] * N
power = [0] * N
 
# Function to convert a
# character to its equivalent
# digit
def toDigit(ch):
   
    return (ord(ch) -
            ord('0'))
 
# Function to precompute
# powers of 10
def powerOf10():
 
    power[0] = 1
    for i in range(1, N):
        power[i] = power[i - 1] * 10
 
# Function to precompute prefix sum
# of numerical strings
def precomputePrefix(st, n):
 
    pref[0] = (ord(st[0]) -
               ord('0'))
    for i in range(1, n):
        pref[i] = (pref[i - 1] +
                   toDigit(st[i]))
 
# Function to return the i-th
# term of Triangular Number
def triangularNumber(i):
 
    res = i * (i + 1) // 2
    return res
 
# Function to return the sum
# of all resulting strings
def sumOfSubstrings(st):
 
    n = len(st)
 
    # Precompute powers
    # of 10
    powerOf10()
 
    # Precompute prefix
    # sum
    precomputePrefix(st, n)
 
    # Initialize result
    ans = 0
 
    for i in range(n - 1):
 
        # Apply the above general
        # formula for every i
        ans += ((pref[n - i - 2] * (i + 1) +
                 toDigit(st[n - i - 1]) *
                 triangularNumber(n - i - 1)) *
                 power[i])
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    st = "1234"
 
    # Function Call
    print(sumOfSubstrings(st))
 
# This code is contributed by Chitranayal


C#
// C# program for the
// above approach
using System;
class GFG{
 
static int N = 10;
static int []pref = new int[N];
static int[] power = new int[N];
 
// Function to convert a
// character to its equivalent
// digit
static int toDigit(char ch)
{
  return (ch - '0');
}
 
// Function to precompute
// powers of 10
static void powerOf10()
{
  power[0] = 1;
   
  for (int i = 1; i < N; i++)
    power[i] = power[i - 1] * 10;
}
 
// Function to precompute prefix sum
// of numerical Strings
static void precomputePrefix(char[] str,
                             int n)
{
  pref[0] = str[0] - '0';
   
  for (int i = 1; i < n; i++)
    pref[i] = pref[i - 1] +
              toDigit(str[i]);
}
 
// Function to return the i-th
// term of Triangular Number
static int triangularNumber(int i)
{
  int res = i * (i + 1) / 2;
  return res;
}
 
// Function to return the sum
// of all resulting Strings
static int sumOfSubStrings(String str)
{
  int n = str.Length;
 
  // Precompute powers of 10
  powerOf10();
 
  // Precompute prefix sum
  precomputePrefix(str.ToCharArray(), n);
 
  // Initialize result
  int ans = 0;
 
  for (int i = 0; i < n - 1; i++)
  {
    // Apply the above general
    // formula for every i
    ans += (pref[n - i - 2] * (i + 1) +
            toDigit(str[n - i - 1]) *
            triangularNumber(n - i - 1)) *
            power[i];
  }
 
  // Return the answer
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
  String str = "1234";
 
  // Function Call
  Console.Write(sumOfSubStrings(str));
}
}
 
// This code is contributed by 29AjayKumar


输出:
680









时间复杂度: O(N),其中N是字符串的长度。
辅助空间: O(1)