HackerRank Super Six Substrings dp
https://www.hackerrank.com/contests/hourrank-18/challenges/super-six-substrings
能被6整除的数有一个特点,就是能同时被3和被2整除
那么也就是能整除3的偶数。
设dp[i][j]表示以第i位结尾的所有子串中,%3的余数是j的方案数,
dp[i + 1][(j * 10 + nowNumber) % 3] = dp[i][j]
注意那个 * 10是必须的,因为从第i项转移过来,位数应该移动一位。就比如,12
1 % 3 = 1, 然后加入一个2,是1 * 10 + 2 = 12 % 3 = 0
但是这里又不是必须的,因为10 % 3 = 1,所以相当于没加。
现在说说那个0, 0是不能做前缀的,怎么破、
对于0,只算一个贡献,不加进去dp统计那里就行。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL;#include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <bitset> const int maxn = 1e5 + 20; LL dp[2][3]; char str[maxn]; void work() {cin >> str + 1;int lenstr = strlen(str + 1);int now = 0;LL ans = 0;for (int i = 1; i <= lenstr; ++i) {now = !now;memset(dp[now], 0, sizeof dp[now]);int num = str[i] - '0';if (num == 0) {ans += dp[!now][0] + 1;memcpy(dp[now], dp[!now], sizeof dp[!now]);continue;}dp[now][num % 3] = 1;for (int j = 0; j <= 2; ++j) {dp[now][(j * 10 + num) % 3] += dp[!now][j];}if (num % 2 == 0) {ans += dp[now][0]; // if (str[i - 1] == '0') ans--; } // cout << ans << endl; }cout << ans << endl; }int main() { #ifdef localfreopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endifwork();return 0; }
View Code
转载于:https://www.cnblogs.com/liuweimingcprogram/p/6546661.html
HackerRank Super Six Substrings dp相关推荐
- F. Rotating Substrings(dp)
F. Rotating Substrings(dp) 思路: d p dp dp,找到最大匹配数然后用 n n n去减,因为每个字母最多动一次就行了. 设 d p [ i ] [ j ] dp[i][ ...
- Android 13 平板Taskbar加载流程
从 Android 12开始,如果是大屏底下导航栏会变成显示一个任务栏,从 NavigationBar 变成 Taskbar . 注:CentralSurfacesImpl.java 与 原来的 st ...
- DP专题训练之HDU 1087 Super Jumping!
Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is ve ...
- 【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)
题干: Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popul ...
- Super Jumping! Jumping! Jumping! 最长上升子序列+DP
Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular i ...
- HDU1087 Super Jumping! Jumping! Jumping!【最长上升子序列+DP】
问题链接:HDU1087 Super Jumping! Jumping! Jumping!. 问题简述:参见上述问题描述. 问题分析: 这是一个最长上升子序列问题,使用DP算法实现. 定义dp[i]= ...
- Super Jumping! Jumping! Jumping!(初学者也能看懂的dp)
Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular i ...
- Super Jumping! Jumping! Jumping! --简单dp
Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular i ...
- 2014 Super Training #8 C An Easy Game --DP
原题:ZOJ 3791 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3791 题意:给定两个0-1序列s1, s2,操作t ...
最新文章
- java.io包和杯子测楼
- matlab产生一列相同的数据,读取excel中的数据把第一列相同的所有行数据输出成一个excel...
- 什么是CommonJS?
- 特斯拉超级充电桩亮相:充电5分钟能跑百公里
- JS获取元素的offsetTop,offsetLeft等相关属性
- 【ArcObject开发】实验:ArcGIS Desktop开发方式入门基础教程
- 推荐算法--总结(08)
- simulink和c语言开发,Simulink之嵌入式C代码生成-应用层和底层的接口
- Android开发笔记(一百三十五)应用栏布局AppBarLayout
- 解决问题:UserWarning: Matplotlib is currently using agg, which is a non-GUI backend.
- 严版快速排序Partion方法
- YUV420查表法高效、无失真的转换为RGB32格式
- lisp用entmake生产圆柱体_蚌埠踏步板钢盖板沟盖板生产加工厂家材质齐全-老友网...
- ( 科来网络分析系统 )简单的抓包步骤,具体的后面文章说
- 中华石杉Java面试突击第一季笔记三(分布式缓存)
- IB书单推荐:没有所谓的“闲书”
- 20155317 2016-2017-2 蓝墨云班课考题第2周
- 怎样使用轻快pdf阅读器
- 倒计时1天,六位专家周末带你解锁前端研发新姿势
- php正则匹配是否为url地址,php正则匹配网址-正则php-php正则匹配url地址
热门文章
- 一些常见的Find 命令
- MySQL查询语种关键字_SQL——SQL语言全部关键字详解
- 面向任务的 Git 学习法
- android信息中字符个数,在android中指定编辑文本中的字符数
- android 蓝牙 setscanmode,蓝牙LE扫描在后台无法在Android M上运行
- mysql with ties_MySQL令人咋舌的隐式转换
- css3边框线倾斜,使用CSS3倾斜边框的Div?
- 想成为全栈工程师,要做到哪几点?
- python的for循环语句_干货丨Python的循环语句基础讲解!
- 99用c语言怎么写出来的,c语言的书写格式是什么