You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]
补一下codeforces前天教育场的题。当时只A了一道题。
大致题意:
定义一个x - y - counter :是一个加法计数器。初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后一位生成一个数列。
例如:4 - 2 - counter 进行+4 +4 +4 +4 +2 +4操作会生成数列 04824。每步要加上x或y是任意的。
给你一个数列(由0~9组成的字符串),问你0~9中全部两个数字生成这个包含这个子串的数列中间至少要插入多少数字。以10 * 10矩阵格式输出。
例如:给定字符串:0840 用 4 - 3 - counter 生成 0(4)8(1)4(7)0是插入数字最少的数列,那么第四行第三列就是3。
样例输入:
0840
样例输出:
-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7
思路:
照着涛哥的代码和思路自己理解了一下。然后敲了一遍。
首先用dp思想初始以x,y为加数加一个个位数为mod的数的的最小步数,因为每步都会在中间插入一个数所以最小步数即在两个
数中间出现的最少的字符+1。生成这个数组i,j循环到9就可以了。然后在打印矩阵中就是算出字符串中每两个数的差值,直接代
入dp[x][y][s[i] - s[i - 1]] - 1即可。转移方程:dp[x][y][mod] = min{i + j} ((i * x + j * y) % 10 == mod)
代码:

1 /*
2 2019年8月10日16点43分
3 Time
4 936ms
5 Memory
6 5312kB
7 Length
8 1497*/
9 #include <cstdio>
10 #include <cstring>
11 #include <string>
12 #include <iostream>
13 using namespace std;
14
15 typedef long long ll;
16 const int MAXN = 50;
17 const int INF = 0x7f7f7f7f;
18
19 ll dp[MAXN][MAXN][MAXN];
20
21 void fill()
22 {
23 for(int i = 0; i < 10; i ++)
24 for(int j = 0; j < 10; j ++)
25 for(int k = 0; k < 10; k ++)
26 dp[i][j][k] = INF;
27 return;
28 }
29 void init()
30 {
31 fill();
32 for(int x = 0; x < 10; x ++)
33 for(int y = 0; y < 10; y ++){
34 for(int i = 0; i < 10; i ++) // i, j只从0循环到9就可以找到x - y - counter生成mod的最少步数。
35 for(int j = 0; j < 10; j ++){
36 if(i + j == 0) continue; // 0 - 0 - counter因为无法生成任何数所以直接跳过。
37 int mod = (i * x + j * y) % 10;
38 dp[x][y][mod] = min(dp[x][y][mod], 1ll * (i + j)); // 进行状态转移。
39 }
40 }
41 return ;
42 }
43
44 ll cal(int x, int y, int a, int b)
45 {
46 a -= b; // 计算的时候因为是从上一位数要加到这一位所以先减去b,是对10取模,+10不影响。
47 if(a < 0) a += 10;
48 return dp[x][y][a];
49 }
50
51 int main()
52 {
53 string s;
54 cin >> s;
55
56 init();
57
58 int n = s.length();
59 for(int x = 0; x < 10; x ++){
60 for(int y = 0; y < 10; y ++){
61 ll ans = 0;
62 for(int i = 1; i < n; i ++){
63 ll cnt = cal(x, y, s[i] - '0', s[i - 1] - '0');
64 ans += cnt; // 把每一位数都算上,然后输出这个位置的答案。
65 if(ans >= INF) break;
66 ans --; // 要减一是因为最后一个数就是我们要的,但不算到中间插入的数。
67 }
68
69 if(ans >= INF) printf("-1 ");
70 else printf("%lld ", ans);
71 }
72 printf("\n");
73 }
74 return 0;
75 }

感受:
涛哥tql!
You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]的更多相关文章
- codeforces 1140D(区间dp/思维题)
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- String painter (hdu 2476 DP好题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意: 给出两个等长的串S, T, 要将S变成T, 每次可以把S的连续的一段变成相同的字母 ...
- codeforces round 422 div2 补题 CF 822 A-F
A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL ...
- codeforces round 421 div2 补题 CF 820 A-E
A Mister B and Book Reading O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...
- codeforces round 420 div2 补题 CF 821 A-E
A Okabe and Future Gadget Laboratory 暴力 #include<bits/stdc++.h> using namespace std; typedef l ...
- Codeforces round 419 div2 补题 CF 816 A-E
A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...
- Educational Codeforces Round 23 A-F 补题
A Treasure Hunt 注意负数和0的特殊处理.. 水题.. 然而又被Hack了 吗的智障 #include<bits/stdc++.h> using namespace std; ...
- codeforces 447 A-E div2 补题
A DZY Loves Hash 水题 #include<iostream> #include<cstdio> #include<cstdlib> #include ...
- codeforces round 418 div2 补题 CF 814 A-E
A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...
随机推荐
- vue-router精简demo
cnpm install vue-router --save-dev 或者 cnpm install vue-router --save 全局引用VueRouter import VueRouter ...
- Oracle 常用函数积累
①length 函数说明:计算字符串长度的函数 返回结果:数字 使用图解: ②lengthb 函数说明:计算字符串字节长度.在学习过程中,了解到还有一个 lengthb 函数.字节和字符的区别 返回结 ...
- mysql创建用户后无法进入
说明 在mysql中添加用户: #mysql -u root -p >use mysql: >update user set password="" where use ...
- November 24th, Week 48th, Sunday, 2019
Once you replace negative thoughts with positive ones, you will start having positive results. 淘汰消极思 ...
- javabean的内省(Introspector)
内省是 Java 语言对 Bean 类属性.事件的一种缺省处理方法.例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值. 通过 getName/ ...
- Django中的sql注入
Django中防止SQL注入的方法 方案一总是使用Django自带的数据库API.它会根据你所使用的数据库服务器(例如PostSQL或者MySQL)的转换规则,自动转义特殊的SQL参数.这被运用到了整 ...
- 2019-2020-1 20199305《Linux内核原理与分析》第九周作业
进程的切换和一般执行过程 (一)进程调度的时机 (1)关键问题 何为进程切换?就是进程调度时机到来时从就绪进程队列中能够挑选一个进程执行,占用CPU时间,那么就有两个关键问题:一是什么时间去挑选一个就 ...
- 安装PS
1:下载溜云库 2:查找PS软件,下载 3:按照教程安装
- java自学-数组
1.数组是什么 前边说过java的基本数据类型,数组,就是装这些基本类型的容器.每个基本类型的变量都是单个的,数组就是这些单个元素的组合. 2.创建数组 方式一 格式: 数组存储的数据类型[] 数组名 ...
- eclipse的一些常用快捷键
掌握了eclipse快捷键功能,能够大大提高开发效率. 这里总结一些eclipse的常用快捷键. 编辑相关快捷键 1. [ALT+/]:此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不 ...