CF792C Divide by Three

思路:

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 = 0;
12 int dp[100005][2][3];
13 int path[100005][2][3];
14 int ans[100005];
15
16 int trans(int index)
17 {
18     return s[index] - ‘0‘;
19 }
20
21 int dfs(int now, bool lz, int mod)
22 {
23     if (dp[now][lz][mod] != -1)
24         return dp[now][lz][mod];
25     if (now == n)
26     {
27         if (lz && !mod)
28             return 0;
29         return -INF;
30     }
31     int x, y = -INF;
32     x = dfs(now + 1, lz, mod);
33     path[now][lz][mod] = 0;
34     if (s[now] != ‘0‘ || (s[now] == ‘0‘ && lz))
35     {
36         y = dfs(now + 1, true, (mod + trans(now)) % 3) + 1;
37         if (y > x)
38         {
39             path[now][lz][mod] = 1;
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 + 1, true, (mod + trans(now)) % 3);
54     }
55     else
56     {
57         get_path(now + 1, lz, mod);
58     }
59 }
60
61 int main()
62 {
63     cin >> s;
64     n = s.length();
65     memset(dp, -1, sizeof(dp));
66     int tmp = dfs(0, false, 0);
67     if (tmp <= 0)
68     {
69         if (s.find("0") != string::npos)
70             puts("0");
71         else
72             puts("-1");
73     }
74     else
75     {
76         get_path(0, false, 0);
77         for (int i = 0; i < cnt; i++)
78         {
79             putchar(s[ans[i]]);
80         }
81         puts("");
82     }
83     return 0;
84 }
时间: 2024-09-29 09:27:39

CF792C Divide by Three的相关文章

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. (1) log int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return INT_MAX; double t1 = log(fabs(dividend

codeforces 792C. Divide by Three

题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧.删除一个数字模3为M的或删除两个模3为3-M的(还有一些要删零),具体看代码. #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<strin

Divide Groups 二分图的判定

Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1835    Accepted Submission(s): 657 Problem Description   This year is the 60th anniversary of NJUST, and to make the celebration mo

lintcode 中等题:Divide Two Integers 两个数的除法

题目 两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11 解题  15%的通过率,减法,位运算?表示不知道如何下手. 法一:利用减法,超时,人工直接去除的一些情况太流氓. public class Solution { /** * @param dividend the dividend * @param divisor the divisor * @return the re

Divide Two Integers

不能使用乘法,除法和mod operator,实现除法功能. Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 用减法可以实现,但是太慢了.算法里所做的优化都是为了节省时间. 不能忽视溢出的问题.例如,被除数是Integer.MIN_VALUE,除以-1,得到的结果对于Int型来说就溢出了,因此返回Integer.MAX_V

分治算法(Divide and Conquer)

分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并. 分治法所能解决的问题一般具有以下几个特征: 问题的规模缩小到一定的程度就可以容易地解决 问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质 利用该问题分解出的子问题的解可以合并为该问题的解 该问题所分解出的各个子问题是相互独立的,即子问题之间不包含公共的子问

[LeetCode] Divide Two Integers

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can be use? Well, bit manipulations. Let's look at an example and see how bit manipulation might help.

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢