日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

所有子序列的逆序对总和_一个数字的所有子串的总和

發布時間:2025/3/11 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 所有子序列的逆序对总和_一个数字的所有子串的总和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

所有子序列的逆序對總和

Problem statement:

問題陳述:

Given an integer, S represented as a string, get the sum of all possible substrings of this string.

給定一個以字符串形式表示的整數S ,得到該字符串所有可能的子字符串的和

Input:

輸入:

A string S that representing the number.

代表數字的字符串S。

Output:

輸出:

Print sum of all possible substrings as required result.

根據要求的結果打印所有可能的子字符串的總和。

Constraints:

限制條件:

1 <= T <= 100 1 <= S <= 1012

Example:

例:

Input:1234 326Output: 1670 395

Explanation:

說明:

For the first input 1234, All possible substrings are 1, 2, 3, 4, 12, 13, 23, 34, 123, 234, 1234 Total sum = 1 + 2 + 3 + 4 + 12 + 23 + 34 + 123 + 234 + 1234 = 1670 For the second input 326 All possible substrings are 3, 2, 6 32, 26 326 Total sum=3+2+6+32+26+326= 395

Solution Approach:

解決方法:

The solution approach is by storing the substring sums to compute the exact next substring sum

解決方法是通過存儲子字符串和以計算確切的下一個子字符串和

  • Create dp[n][n] to store substring sums;

    創建dp [n] [n]來存儲子字符串和;

  • Initialize sum=0 which will be our final result;

    初始化sum = 0,這將是我們的最終結果;

  • Base case computation (single length substrings),

    基本案例計算(單長度子字符串),

    for i=0 to n-1,n= string lengthdp[i][i]=s[i] -'0'; //s[i]-'0' gives the digit actuallysum+=dp[i][i]; end for
  • Till now we have computed all single digit substrings,

    到現在為止,我們已經計算了所有個位數的子字符串,

    for substring length,len=2 to nfor start=0 to n-len//so basically it's the substring s[start,end]int end=start+len-1; dp[start][end]=dp[start][end-1]*10+s[end]-'0'; sum+=dp[start][end];end for end for
  • Sum is the final result.

    總和是最終結果。

  • All the statements are self-explanatory except the one which is the fundamental idea of the entire storing process. That is the below one,

    所有陳述都是不言自明的,只是整個存儲過程的基本思想。 那是下一個,

    dp[start][end]=dp[start][end-1]*10+s[end]-'0';

    Let's check this with an example,

    我們來看一個例子,

    Say we are computing for string s="1234" At some stage of computing, Start=1, end= 3 So Dp[start][end]=dp[start][end-1]*10+s[end]-'0'So basically we are computing value of substring s[start..end] with help of already computed s[start,end-1]For this particular example s[start..end] ="234" s[start..end-1] ="23"Now, dp[1][3]=dp[1][2]*10+'4'-'0'So, assuming the fact that our algo is correct and thus dp[start][end-1] has the correct value, dp[]1[2] would be 23 then So, dp[1][3]=23*10+'4'-'0=234 and that's true So, here's the main logic Now how dp[1][2] is guaranteed to be correct can be explored if we start filling the Dp table from the base conditions?

    Let's start for the same example

    讓我們開始同樣的例子

    N=4 here

    N = 4這里

    So, we need to fill up a 4X4 DP table,

    因此,我們需要填寫4X4 DP表,

    After filling the base case,

    裝完基本外殼后,

    Now, I am computing for len=2

    現在,我正在計算len = 2

    Start=0, end=1

    開始= 0,結束= 1

    Start=1, end=2

    開始= 1,結束= 2

    Start=2, end=3

    開始= 2,結束= 3

    For len =3

    對于len = 3

    Start=0, end=2

    開始= 0,結束= 2

    Start=1, end=3

    開始= 1,結束= 3

    Len=4

    Len = 4

    Start=0, end=3

    開始= 0,結束= 3

    At each step we have summed up, so result is stored at sum.

    在每一步我們都進行了總結,因此結果被存儲在總和中。

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;void print(vector<int> a, int n) {for (int i = 0; i < n; i++)cout << a[i] << " ";cout << endl; }long long int my(string s, int n) {long long int dp[n][n];long long int sum = 0;for (int i = 0; i < n; i++) {dp[i][i] = s[i] - '0';sum += dp[i][i];}for (int len = 2; len <= n; len++) {for (int start = 0; start <= n - len; start++) {int end = start + len - 1;dp[start][end] = dp[start][end - 1] * 10 + s[end] - '0';sum += dp[start][end];}}return sum; } int main() {int t, n, item;cout << "enter the string: ";string s;cin >> s;cout << "sum of all possible substring is: " << my(s, s.length()) << endl;return 0; }

    Output:

    輸出:

    RUN 1: enter the string: 17678 sum of all possible substring is: 29011RUN 2: enter the string: 326 sum of all possible substring is: 395

    翻譯自: https://www.includehelp.com/icp/sum-of-all-substrings-of-a-number.aspx

    所有子序列的逆序對總和

    總結

    以上是生活随笔為你收集整理的所有子序列的逆序对总和_一个数字的所有子串的总和的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。