补一下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. 内存取证工具-volatility、foremost

    内存取证 1. 内存取证工具volatility 猜测dump文件的profile值 root@kali:~/CTF# volatility -f mem.vmem imageinfo Volatil ...

  2. 微店APP协议简要分析

    1.通过抓包软件charles进行抓包,点击微信收款后,抓包内容都是加密处理过  2.加载分析定位这些字段的加密函数. WDTNThorParameterProcessor HTTPBody:task ...

  3. 【Cocos谁学谁会】制作会跑动的地板

    版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...

  4. CF#603 Div2

    差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...

  5. poi创建excel文件

    package com.mozq.sb.file01.test; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf. ...

  6. 《工作笔记:移动web页面前端开发总结》

    工作笔记:移动web页面前端开发总结 移动web在当今的发展速度是一日千里,作为移动领域的门外汉,在这段时间的接触后,发现前端开发这一块做一个小小的总结. 1.四大浏览器内核 1.Trident (I ...

  7. python-paramiko对远程服务器终端的操作

    1.with open写文件到本地 2.paramiko SFTPClient将文件推到salt服务端 3.paramiko SSHClient通过salt-cp将文件分发给目标服务器 1. with ...

  8. filter,map,reduce三个数组高阶函数的使用

    filter ,map ,reduce三个高阶函数的使用 普通方法解决数据问题 const nums1= [10,20,111,222,444,40,50] // 需求1.取出小于100的数字 // ...

  9. IOS系统input输入框为readonly时, 隐藏键盘上的上下箭头

    业务中在一定场景中会将input 设置为只读状态,在IOS safari上当input 输入框focus 时,仍会出现键盘上的上下箭头,这种用户体验非常不好,如何干掉呢? <input read ...

  10. Python菜鸟文本处理4种方法

    自从认识了python这门语言,所有的事情好像变得容易了,作为小白,逗汁儿今天就为大家总结一下python的文本处理的一些小方法. 话不多说,代码撸起来. python大小写字符互换 在进行大小写互换 ...