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], ...
随机推荐
- MySQL 重置Mysql root用户账号密码
重置Mysql root用户账号密码 By:授客 QQ:1033553122 问题描述: 使用mysqladmin.exe执行命令时出现以下错误提示: mysqladmin: connect to ...
- 学习之Redis(二)
Redis的对象和数据结构 一.字符串对象(请参考学习之Redis(一):https://www.cnblogs.com/wbq1994/p/12029516.html) 二.列表对象 列表对象的编码 ...
- node_modules/.bin/babel : 无法加载文件 D:\node\node_project\es6\node_modules\.bin\babel.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.co m/fwlink/?LinkID=135170 中的 about_Execution_Policies。
刚入门es6,遇到上面问题,然后 解决方案: 以管理员身份运行vs code执行:get-ExecutionPolicy,显示Restricted,表示状态是禁止的执行:set-ExecutionPo ...
- 第十六届浙江大学宁波理工学院程序设计大赛 D 雷顿女士与分队hard version(dp)
题意 链接:https://ac.nowcoder.com/acm/contest/2995/D来源:牛客网 卡特莉接到来自某程序设计竞赛集训队的邀请,来为他们进行分队规划. 现在集训队共有n名选手, ...
- Vant-Weap通过事件获取值
van-field框的使用 和通过事件获取值 <van-cell-group> <van-field value="{{username}}" label=&qu ...
- post请求四种传送正文的方式
一.简介 HTTP协议规定post提交的数据必须放在消息主体(entity-body)中,但协议没有规定数据必须使用什么编码方式.HTTP协议是以ASCII码传输,建立再TCP/IP协议之上的应用层规 ...
- 邹传伟:对人民银行DC/EP的初步分析
http://opinion.caixin.com/2019-11-01/101477903.html [财新网](专栏作家 邹传伟)2019年10月24日,习总书记在中央政治局第十八次集体学习中指出 ...
- Git实用指南
个人整理的一些Git概念和命令,可以速查或者快速解决某些方面的问题 一.精简入门 1.克隆仓库 克隆仓库会下载仓库完整的文件.分支和历史记录 git clone [<options>] [ ...
- go语言的错误处理
1.系统自己抛异常 //go语言抛异常 func test3_1() { l := [5] int {0,1,2,3,4} var index int = 6 fmt.Println(l) l[ind ...
- 动态SQL与变量绑定
有时候动态sql需要进行变量的赋值,这个时候就需要调用系统的存储过程sp_executesql了.使用中还是有些注意事项,代码如下: --字符型字段需声明为NVARCHAR类型 ),) --动态SQL ...