思路:

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的更多相关文章

  1. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  2. Pairwise Sum and Divide 51nod

      1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有这样 ...

  3. Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘

    在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...

  4. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  5. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

  6. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

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

  8. 51nod1305 Pairwise Sum and Divide

    题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...

  9. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

随机推荐

  1. JS日历控件 灵活设置: 精确的时分秒.

     在今年7月份时候 写了一篇关于 "JS日历控件" 的文章 , 当时仅仅支持 年月日 的日历控件,如今优化例如以下:      1. 在原基础上 支持 yyyy-mm-dd 的年月 ...

  2. 嵌入式开发之davinci--- 8148/8168/8127 中的图像缩放sclr、swms之后出现图像视频卡顿、屏幕跳跃的问题

    ()问题原因 这边的case链路是这样的camera->sclr(yuv420sp cif)->dup->ipcframeoutm3<->ipcframerocess&l ...

  3. seajs载入流程图

    近期读seajs源代码,整理出了主要逻辑的流程图(注意:是逻辑示意图).感兴趣的朋友能够看看,欢迎批评指正~ http://www.gliffy.com/go/publish/image/607216 ...

  4. debug找到source lookup path以及,debug跑到另外的解决办法

    在我们使用eclipse调试的时候,有时候会出一些奇葩的问题,比如找不到Source  lookup path, 这时我们可以点击Edit Source Lookup Path.接着回弹出一个 我们只 ...

  5. 关于axis2.1.6与websphere7的包冲突问题的解决方式

    1,复制axis2.1.6内的module目录内的全部文件到lib 并改动扩展名为.jar 2,删除module目录(可选,不删除也能够) 3,部署到was 4,设置was相应应用程序的类载入方案为父 ...

  6. <label>标签for属性

    label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控 ...

  7. servlet,RMI,webservice之间的区别

    最近项目中有提供或者调用别的接口,在纠结中到底是用servlet还是用webservice,所以上网查看了下他们以及RMI之间的区别,方便加深了解. 首先比较下servlet和webservice下  ...

  8. Jackson 框架的高阶应用

    Jackson 是当前用的比较广泛的,用来序列化和反序列化 json 的 Java 的开源框架.Jackson 社 区相对比较活跃,更新速度也比较快, 从 Github 中的统计来看,Jackson ...

  9. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  10. 输出两个MAC地址之间的地址

    /******************************************************************************* * 输出两个MAC地址之间的地址 * ...