所有子序列的逆序对总和_一个数字的所有子串的总和
所有子序列的逆序对总和
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
326
Output:
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 length dp[i][i]=s[i] -'0'; //s[i]-'0' gives the digit actually sum+=dp[i][i]; end for
Till now we have computed all single digit substrings,
到现在为止,我们已经计算了所有个位数的子字符串,
for substring length,len=2 to n for 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: 29011
RUN 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
所有子序列的逆序对总和
所有子序列的逆序对总和_一个数字的所有子串的总和相关推荐
- mysql 查询多个总和_使用MySQL查询选择多个总和,并在单独的列中显示?
要使用MySQL查询选择多个总和列并将它们显示在单独的列中,您需要使用CASE语句.语法如下:SELECT SUM( CASE WHEN yourColumnName1='yourValue1' TH ...
- vb.net中递归退到最外层_数组中的逆序对
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- 树状数组求逆序对_算法系列之-数组中的逆序对
题目来源 剑指offer 01 题目描述 在数组中如果前一个数字大于后一个数字,则称为这个数字组合组成一个逆序对.输入一个数组,求所有的逆序对的总数. 如 数组 {7,5,6,4} 则它的逆序对是 ( ...
- 输入一个数组,求出这个数组中的逆序对的总数
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- 牛客(35)数组中的逆序对
// 题目描述 // 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对. // 输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. ...
- c语言数组求逆序对,LeetCode 面试题51. 数组中的逆序对
面试题51. 数组中的逆序对 题目来源:https://leetcode-cn.com/problems/shu-zu-zhong-de-ni-xu-dui-lcof/ 题目 在数组中的两个数字,如果 ...
- java逆序对距离之和,七天刷完剑指offer-【第27道-第37道】
27.字符串的排列 1. 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列. 2. 示例 例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac, ...
- 分治法 逆序对计数 O(nlgn)
一.逆序对 1. 问题背景 假如有一组电影集合,包括n部电影.某个人对这n部电影的喜欢程度各有高低,根据其喜欢程度对这n部电影进行排名,按照从1到n的方式进行标记,这就形成了一个关于电影的排名表.假设 ...
- 树状数组(求逆序对)
一.树状数组是什么 树状数组,又称二进制索引树,英文名Binary Indexed Tree 之前遇到一个求逆序对的题,看了很多题解都只说了这个树状数组,关于怎么实现的全都避而不谈,我研究了一下午,总 ...
最新文章
- utf-7 xss paper
- 系统相机裁剪比例_真皮、皮革自动裁剪机,拒绝材料浪费,一年可以节省十几万!...
- testng的报告自定义笔记
- web项目通过ajax提交数据太大报错
- LL-verilog语法-generate语句
- php超强后门在任意位置创建文件,php大马:.user.ini文件构成的超强PHP后门
- 对未来计算机的畅想初中英语,初中英语期中考试,作文停电一小时,在北京……在上海……在威海…….doc...
- linux分配端口未抢占端口,Linux命令之awk:基础知识(一)
- 今日恐慌与贪婪指数为66 贪婪程度有所缓解
- 手机访问www如何自动跳转到m js代码实现
- 显示纯服务器_BBT三行代码搭建服务器,让Dynamo跳出IronPython的封锁
- chat后缀域名_.chat域名简介
- 2011考研数学核心题型-陈文灯
- MC74HC595驱动
- 黑马SQL入门到精通笔记 —— 进阶篇
- Go语言的流程结构简单介绍
- HERCULE:通过在相关日志图上进行社区发现来重建攻击故事
- 哈夫曼树 (100分)哈夫曼树
- 【游戏开发实战】Unity UGUI Text图文混排(聊天文字混表情),支持动态表情,出招吧表情帝
- 简易商品展示HTML+CSS
热门文章
- origin设置不同区域的颜色_[测试狗]Origin入门教程(二十四):效率翻倍小技巧——修改默认字体...
- python图像识别代码_用Python进行简单图像识别(验证码)
- php+mysql记事本_一个简单记事本php操作mysql辅助类创建
- button设置disabled属性不生效_jQuery属性节点
- 防火墙firewalld
- 【技术干货+限时活动】openstack原理及在华为云中的应用
- 一次面试总结(记录)
- 51CTO会员开通成功!开森!
- 自己动手实现一个html2canvas
- 直播的学习与使用-----采集