补一下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][补题]的更多相关文章

  1. codeforces 1140D(区间dp/思维题)

    D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. String painter (hdu 2476 DP好题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意: 给出两个等长的串S, T, 要将S变成T, 每次可以把S的连续的一段变成相同的字母 ...

  3. 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 ...

  4. 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 ...

  5. codeforces round 420 div2 补题 CF 821 A-E

    A Okabe and Future Gadget Laboratory 暴力 #include<bits/stdc++.h> using namespace std; typedef l ...

  6. Codeforces round 419 div2 补题 CF 816 A-E

    A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...

  7. Educational Codeforces Round 23 A-F 补题

    A Treasure Hunt 注意负数和0的特殊处理.. 水题.. 然而又被Hack了 吗的智障 #include<bits/stdc++.h> using namespace std; ...

  8. codeforces 447 A-E div2 补题

    A DZY Loves Hash 水题 #include<iostream> #include<cstdio> #include<cstdlib> #include ...

  9. codeforces round 418 div2 补题 CF 814 A-E

    A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...

随机推荐

  1. springboot~yml里的自定义配置~续

    之前写了关于读取自定义配置的文章springboot~yml里的自定义配置,而今天主要说一下对复杂配置信息的读取方法,我们简单的配置用@Value注解就可以了,而结构复杂的一般使用@Configura ...

  2. Parallel.ForEach 使用多线遍历循环

    Parallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程品德操作. static void Main(string[] args) { Console.Write ...

  3. 请确保二进制储存在指定的路径中,或者调试他以检查该二进制或相关的DLL文件

    出现问题原因: 编译socket.dll时,用到了openssl库. 使用libeay32.lib.ssleay32.lib生成socket.dll,就会报这样的错误 解决办法: 使用libeay32 ...

  4. 20190608_浅谈go&java差异(三)

    20190608_浅谈go&java差异(三) 转载请注明出处https://www.cnblogs.com/funnyzpc/p/10990703.html 第三节内容概览 多线程通讯(线程 ...

  5. OpenStack与ZStack深度对比:架构、部署、计算、运维监控等

    摘要 OpenStack从2010年开源至今,已经走过9个年头,其正在进入主流企业市场,但该项目依然面临较难部署和管理的老问题.有一点是毫无疑问的,那就是OpenStack保持着高速增长的态势,超过5 ...

  6. 使用gitolite搭建Git服务器

    使用gitolite搭建Git服务器 运行环境 Ubuntu18.04 gitolite 搭建过程 安装好Ubuntu18.04系统 更新系统 sudo apt update sudo apt upg ...

  7. CentOS7 安装Python3.6.8

    CentOS7 安装Python3.6.8 1. 安装依赖环境 yum -y groupinstall "Development tools" yum -y install zli ...

  8. 连接 sql

    java连接sqlserver 1 创建 Dynamic Web Project项目 在WebContent/WEB-INF/lib中添加sqljdbc42.jar 2 在class文件里连接数据库 ...

  9. 【洛谷5369】[PKUSC2018] 最大前缀和(状压DP)

    点此看题面 大致题意: 对于一个序列,求全排列下最大前缀和之和. 状压\(DP\) 考虑如果单纯按照题目中对于最大前缀和的定义,则一个序列它的最大前缀和是不唯一的. 为了方便统计,我们姑且规定,如果一 ...

  10. [译]Vulkan教程(10)交换链

    [译]Vulkan教程(10)交换链 Vulkan does not have the concept of a "default framebuffer", hence it r ...