思路:

dp。

实现:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int INF = 0x3f3f3f3f;
  9.  
  10. string s;
  11. int n, cnt = ;
  12. int dp[][][];
  13. int path[][][];
  14. int ans[];
  15.  
  16. int trans(int index)
  17. {
  18. return s[index] - '';
  19. }
  20.  
  21. int dfs(int now, bool lz, int mod)
  22. {
  23. if (dp[now][lz][mod] != -)
  24. return dp[now][lz][mod];
  25. if (now == n)
  26. {
  27. if (lz && !mod)
  28. return ;
  29. return -INF;
  30. }
  31. int x, y = -INF;
  32. x = dfs(now + , lz, mod);
  33. path[now][lz][mod] = ;
  34. if (s[now] != '' || (s[now] == '' && lz))
  35. {
  36. y = dfs(now + , true, (mod + trans(now)) % ) + ;
  37. if (y > x)
  38. {
  39. path[now][lz][mod] = ;
  40. return dp[now][lz][mod] = y;
  41. }
  42. }
  43. return dp[now][lz][mod] = x;
  44. }
  45.  
  46. void get_path(int now, bool lz, int mod)
  47. {
  48. if (now == n)
  49. return;
  50. if (path[now][lz][mod])
  51. {
  52. ans[cnt++] = now;
  53. get_path(now + , true, (mod + trans(now)) % );
  54. }
  55. else
  56. {
  57. get_path(now + , lz, mod);
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. cin >> s;
  64. n = s.length();
  65. memset(dp, -, sizeof(dp));
  66. int tmp = dfs(, false, );
  67. if (tmp <= )
  68. {
  69. if (s.find("") != string::npos)
  70. puts("");
  71. else
  72. puts("-1");
  73. }
  74. else
  75. {
  76. get_path(, false, );
  77. for (int i = ; i < cnt; i++)
  78. {
  79. putchar(s[ans[i]]);
  80. }
  81. puts("");
  82. }
  83. return ;
  84. }

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. 升级iOS 9之前的注意事项

    iOS 9 beta刚刚公布.就下载了官网的升级包, 使用itunes的更新功能,升级 眼看安装过程一番顺利, 升级完開始进入设置操作步骤上, 结果傻眼了 进入了输入手机password的界面,  不 ...

  2. 多媒体开发之---h264 server rtsp

    (1)live555 (2)gstreamer http://code.openhub.net/search?s=rtsp%20server (3)srs (4)ffmpeg

  3. 阿里云cenos 6.5 模板上安装 docker

    本章将介绍在阿里云的 Centos6.5 模板上安装 Docker 以及在 Ubuntu 14.04 模板上安装 Docker 的过程 Centos 6.5 模板上使用Docker 首先,通过 ssh ...

  4. 可以声明接口,但不可以new接口

    接口是一种特殊的抽象类,它包含常量和方法的声明,但没有方法的实现:可以把接口看成是一种特殊的抽象类: 接口实质上是一种规范,它关心的是"做什么",不关心"怎样做" ...

  5. Django's CSRF mechanism

    Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this ...

  6. 原生ajax请求和jsonp

    1.原生ajax请求 var obj = new XMLHttpRequest(); obj.open("POST", url, true); obj.setRequestHead ...

  7. assert的作用是什么

    assert()宏是用于保证满足某个特定条件,用法是: assert(表达式); 如果表达式的值为假,整个程序将退出,并输出一条错误信息.如果表达式的值为真则继续执行后面的语句. 使用这个宏前需要包含 ...

  8. UVALive3415 Guardian of Decency —— 最大独立集

    题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...

  9. YTU 2586: 填空题B-字画鉴别

    2586: 填空题B-字画鉴别 时间限制: 1 Sec  内存限制: 128 MB 提交: 509  解决: 131 题目描述 注:本题只需要提交填写部分的代码,请按照C语言方式提交. 古玩店老板小勇 ...

  10. YTU 2559: “心脏出血”

    2559: "心脏出血" 时间限制: 1 Sec  内存限制: 128 MB 提交: 5  解决: 2 题目描述 2014年4月,一个开源加密库OpenSSL的严重漏洞" ...