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

所有子序列的逆序对总和

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

解决方法是通过存储子字符串和以计算确切的下一个子字符串和

  1. Create dp[n][n] to store substring sums;

    创建dp [n] [n]来存储子字符串和;

  2. Initialize sum=0 which will be our final result;

    初始化sum = 0,这将是我们的最终结果;

  3. 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
    
    
  4. 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
    
    
  5. 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表,

Sum of all substrings of a number (1)

After filling the base case,

装完基本外壳后,

Sum of all substrings of a number (2)

Now, I am computing for len=2

现在,我正在计算len = 2

Start=0, end=1

开始= 0,结束= 1

Sum of all substrings of a number (3)

Start=1, end=2

开始= 1,结束= 2

Sum of all substrings of a number (4)

Start=2, end=3

开始= 2,结束= 3

Sum of all substrings of a number (5)

For len =3

对于len = 3

Start=0, end=2

开始= 0,结束= 2

Sum of all substrings of a number (6)

Start=1, end=3

开始= 1,结束= 3

Sum of all substrings of a number (7)

Len=4

Len = 4

Start=0, end=3

开始= 0,结束= 3

Sum of all substrings of a number (8)

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

所有子序列的逆序对总和

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543983.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

synchronized:使用不规范,老板泪两行!

线程安全问题一直是系统亘古不变的痛点。这不&#xff0c;最近在项目中发了一个错误使用线程同步的案例。表面上看已经使用了同步机制&#xff0c;一切岁月静好&#xff0c;但实际上线程同步却毫无作用。关于线程安全的问题&#xff0c;基本上就是在挖坑与填坑之间博弈&#xf…

SQL --运算符

2019独角兽企业重金招聘Python工程师标准>>> 一、<> (安全等于运算符) mysql中的 、<>或!运算符&#xff0c;相信大家已经很清楚了。今天看到了<>这个运算符&#xff0c;记录下来。 1><>和号的相同点 他们都是两个值比较符&#xff0c;相…

linux 文件浏览器_浏览Linux文件系统

linux 文件浏览器你为什么要学习&#xff1f; (Why would you want to learn?) Linux is probably the most used operating system when it comes to development. For a developer, Linux provides all the required tools. Learning how to navigate the Linux file system…

@Autowired 和 @Resource 的 5 点区别!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;Autowired 和 Resource 都是 Spring/Spring Boot 项目中&#xff0c;用来进行依赖注入的注解。它们都提供了将依赖对…

rsync同步数据到内网

最近公司要求将IDC的APP日志备份到公司办公网内部&#xff0c;思前想后&#xff0c;结合以前学过的知识&#xff0c;决定用rsync直接推送&#xff0c;即从APP服务器上直接将日志推送到公司内网。这样避免了在生产服务器上额外安装更多软件而且只需要进行简单的配置&#xff0c;…

SpringBoot 时间格式化的 5 种实现方法!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在我们日常工作中&#xff0c;时间格式化是一件经常遇到的事儿&#xff0c;所以本文我们就来盘点一下 Spring Boot 中时间格…

SpringBoot 解决跨域问题的 5 种方案!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;跨域问题指的是不同站点之间&#xff0c;使用 ajax 无法相互调用的问题。跨域问题本质是浏览器的一种保护机制&#…

Java 中的 Lombok 到底能不能用?

一、摘要Java&#xff0c;作为一款非常热门的编程语言&#xff0c;尽管它有着非常丰富的语言特性&#xff0c;完全面向对象编程&#xff0c;编程高度规范化&#xff0c;但是也有一个最受大家诟病的一个缺点&#xff1a;啰嗦&#xff0c;尤其是当你开发了很多年之后&#xff0c;…

旅行商问题

旅行商问题 (Travelling Salesman problem) This problem can be stated as- "Given n number of cities and a travelling salesman has to visit each city. Then we have to find the shortest tour so that the travelling salesman can visit each and every city on…

分页 + 模糊查询竟然有坑?

不知道你有没有使用过Mysql的like语句&#xff0c;进行模糊查询&#xff1f;不知道你有没有将查询结果&#xff0c;进行分页处理&#xff1f;模糊查询&#xff0c;加上分页处理&#xff0c;会有意想不到的坑&#xff0c;不信我们继续往下看。我之前提供过一个品牌查询接口&…

导致事务@Transactional失效的5种场景!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;一个程序中不可能没有事务&#xff0c;而 Spring 中&#xff0c;事务的实现方式分为两种&#xff1a;编程式事务和声…

操作系统 cpu调度_CPU调度| 操作系统

操作系统 cpu调度调度标准 (Scheduling Criteria) There are many criteria which have been suggested for comparing the CPU scheduling algorithms. The characteristics which are used for comparison and then used to determine the best algorithms, for this some of…

IOS中KVO模式的解析与应用

最近老翁在项目中多处用到了KVO&#xff0c;深感这种模式的好处。现总结如下&#xff1a; 一、概述 KVO,即&#xff1a;Key-Value Observing&#xff0c;它提供一种机制&#xff0c;当指定的对象的属性被修改后&#xff0c;则对象就会接受到通知。简单的说就是每次指定的被观察…

使用 lambda 实现超强的排序功能

我们在系统开发过程中&#xff0c;对数据排序是很常见的场景。一般来说&#xff0c;我们可以采用两种方式&#xff1a;借助存储系统&#xff08;SQL、NoSQL、NewSQL 都支持&#xff09;的排序功能&#xff0c;查询的结果即是排好序的结果查询结果为无序数据&#xff0c;在内存中…

java 的23种设计模式 之单身狗和隔壁老王的故事

2019独角兽企业重金招聘Python工程师标准>>> 觉得代码写的别扭了&#xff0c;回头翻翻java 的23种设计模式。today,额,这么晚了&#xff0c;困了。就弄个最简单的单例模式吧。单例模式&#xff1a;俗称单身狗 package singleton; public class SingleTon { private …

使用python学线性代数_二项式过程| 使用Python的线性代数

使用python学线性代数When we flip a coin, there are two possible outcomes as head or tail. Each outcome has a fixed probability of occurrence. In the case of fair coins, heads and tails each have the same probability of 1/2. In addition, there are cases in …

工作中常见的 6 种设计模式,你用过几种?

前言 哈喽&#xff0c;大家好。平时我们写代码呢&#xff0c;多数情况都是流水线式写代码&#xff0c;基本就可以实现业务逻辑了。如何在写代码中找到乐趣呢&#xff0c;我觉得&#xff0c;最好的方式就是&#xff1a;使用设计模式优化自己的业务代码。今天跟大家聊聊日常工作中…

这12款idea插件,能让你代码飞起来!

前言基本上每个程序员都会写代码&#xff0c;但写代码的速度不尽相同。为什么有些人&#xff0c;一天只能写几百行代码&#xff1f;而有些人&#xff0c;一天可以写几千行代码&#xff1f;有没有办法&#xff0c;可以提升开发效率&#xff0c;在相同的时间内&#xff0c;写出更…

node js 开发网站_使用Node JS开发网站

node js 开发网站You will have your own fully functional website running on "localhost" after going through this article. 阅读完本文后&#xff0c;您将在“ localhost”上运行自己的功能齐全的网站 。 Basic knowledge of JavaScript and HTML is a prereq…