CF792C Divide by Three
思路:
dp。
实现:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <algorithm>
- using namespace std;
- const int INF = 0x3f3f3f3f;
- string s;
- int n, cnt = ;
- int dp[][][];
- int path[][][];
- int ans[];
- int trans(int index)
- {
- return s[index] - '';
- }
- int dfs(int now, bool lz, int mod)
- {
- if (dp[now][lz][mod] != -)
- return dp[now][lz][mod];
- if (now == n)
- {
- if (lz && !mod)
- return ;
- return -INF;
- }
- int x, y = -INF;
- x = dfs(now + , lz, mod);
- path[now][lz][mod] = ;
- if (s[now] != '' || (s[now] == '' && lz))
- {
- y = dfs(now + , true, (mod + trans(now)) % ) + ;
- if (y > x)
- {
- path[now][lz][mod] = ;
- return dp[now][lz][mod] = y;
- }
- }
- return dp[now][lz][mod] = x;
- }
- void get_path(int now, bool lz, int mod)
- {
- if (now == n)
- return;
- if (path[now][lz][mod])
- {
- ans[cnt++] = now;
- get_path(now + , true, (mod + trans(now)) % );
- }
- else
- {
- get_path(now + , lz, mod);
- }
- }
- int main()
- {
- cin >> s;
- n = s.length();
- memset(dp, -, sizeof(dp));
- int tmp = dfs(, false, );
- if (tmp <= )
- {
- if (s.find("") != string::npos)
- puts("");
- else
- puts("-1");
- }
- else
- {
- get_path(, false, );
- for (int i = ; i < cnt; i++)
- {
- putchar(s[ans[i]]);
- }
- puts("");
- }
- return ;
- }
CF792C Divide by Three的更多相关文章
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Pairwise Sum and Divide 51nod
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样 ...
- Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘
在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- [leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- uva10375 Choose and Divide(唯一分解定理)
uva10375 Choose and Divide(唯一分解定理) 题意: 已知C(m,n)=m! / (n!*(m-n!)),输入整数p,q,r,s(p>=q,r>=s,p,q,r,s ...
- 51nod1305 Pairwise Sum and Divide
题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
随机推荐
- 升级iOS 9之前的注意事项
iOS 9 beta刚刚公布.就下载了官网的升级包, 使用itunes的更新功能,升级 眼看安装过程一番顺利, 升级完開始进入设置操作步骤上, 结果傻眼了 进入了输入手机password的界面, 不 ...
- 多媒体开发之---h264 server rtsp
(1)live555 (2)gstreamer http://code.openhub.net/search?s=rtsp%20server (3)srs (4)ffmpeg
- 阿里云cenos 6.5 模板上安装 docker
本章将介绍在阿里云的 Centos6.5 模板上安装 Docker 以及在 Ubuntu 14.04 模板上安装 Docker 的过程 Centos 6.5 模板上使用Docker 首先,通过 ssh ...
- 可以声明接口,但不可以new接口
接口是一种特殊的抽象类,它包含常量和方法的声明,但没有方法的实现:可以把接口看成是一种特殊的抽象类: 接口实质上是一种规范,它关心的是"做什么",不关心"怎样做" ...
- Django's CSRF mechanism
Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this ...
- 原生ajax请求和jsonp
1.原生ajax请求 var obj = new XMLHttpRequest(); obj.open("POST", url, true); obj.setRequestHead ...
- assert的作用是什么
assert()宏是用于保证满足某个特定条件,用法是: assert(表达式); 如果表达式的值为假,整个程序将退出,并输出一条错误信息.如果表达式的值为真则继续执行后面的语句. 使用这个宏前需要包含 ...
- UVALive3415 Guardian of Decency —— 最大独立集
题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...
- YTU 2586: 填空题B-字画鉴别
2586: 填空题B-字画鉴别 时间限制: 1 Sec 内存限制: 128 MB 提交: 509 解决: 131 题目描述 注:本题只需要提交填写部分的代码,请按照C语言方式提交. 古玩店老板小勇 ...
- YTU 2559: “心脏出血”
2559: "心脏出血" 时间限制: 1 Sec 内存限制: 128 MB 提交: 5 解决: 2 题目描述 2014年4月,一个开源加密库OpenSSL的严重漏洞" ...