zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 18 C. Divide by Three DP

    C. Divide by Three
     

    A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

    The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

    Write a program which for the given n will find a beautiful number such that n can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number n.

    If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

    Input

    The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

    Output

    Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

    Examples
    input
    1033
    output
    33
    Note

    In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.

     题意:

      给你一个01串,问你最少删除多少个字符,使得余下的串10进制下%3=0,不得有前导0

    题解:

      设定dp[i][j][0/1/2]表示前i个字符中,组成%3=j的串需要的最少删除次数;

      同时0表示还未填数,

         1表示有一个前导0,

       2表示开头填了一个非0数

      需要记录路径pre

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double Pi = acos(-1.0);
    const int N = 1e5+10, M = 1e3+20, mod = 1e9+7, inf = 2e9;
    
    
    int dp[N][5][3],pre[N][5][3];//前i个数mod3 = j最少需要删除的字母个数 是否有前导0
    char s[N];
    int n,a[N],ans[N];
    int main() {
        scanf("%s",s+1);
        int n = strlen(s+1);
        for(int i = 1; i <= n; ++i) a[i] = s[i] - '0';
        for(int i = 0; i <= n; ++i) {
            for(int j = 0; j < 3; ++j) dp[i][j][0] = inf,dp[i][j][1] = inf, dp[i][j][2] = inf;
        }
        dp[0][0][0] = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < 3; ++j) {
                if(dp[i][j][0] < dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2]) {
                    dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = dp[i][j][0];
                    pre[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = 1;
                }
                if(dp[i][j][1]+1 < dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2]) {
                    dp[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = dp[i][j][1]+1;
                    pre[i+1][(j+a[i+1])%3][(a[i+1])==0?1:2] = 1;
                }
                if(dp[i][j][2] < dp[i+1][(j+a[i+1])%3][2]) {
                    dp[i+1][(j+a[i+1])%3][2] = dp[i][j][2];
                    pre[i+1][(j+a[i+1])%3][2] = 1;
                }
    
                if(dp[i][j][1]+1 < dp[i+1][j][1]) {
                   dp[i+1][j][1] = dp[i][j][1]+1;
                   pre[i+1][j][1] = -1;
                }
                if(dp[i][j][0]+1 < dp[i+1][j][0]) {
                   dp[i+1][j][0] = dp[i][j][0]+1;
                   pre[i+1][j][0] = -1;
                }
                if(dp[i][j][2]+1 < dp[i+1][j][2]) {
                   dp[i+1][j][2] = dp[i][j][2]+1;
                   pre[i+1][j][2] = -1;
                }
            }
        }
        if(dp[n][0][2] >= inf && dp[n][0][1] >= inf) {
            puts("-1");
            return 0;
        }
        if(dp[n][0][1] < dp[n][0][2]) {
            puts("0");
            return 0;
        }
        int j = 0,num = n - dp[n][0][2];
        for(int i = n; i >= 1; --i) {
            if(pre[i][j][2] == 1) {
                ans[num--] = a[i];
                j = ((j - a[i])%3 + 3) % 3;
            }
            if(num == 0) break;
        }
        for(int i = 1; i <= n - dp[n][0][2]; ++i) cout<<ans[i];
        return 0;
    }

      

  • 相关阅读:
    UniEAP V4 开发实践说明文档
    SI_WorkShop_V4安装手册
    unieap platform eclipse.ini vm设置
    asp.net 配置 web.config 禁用VS2013自带的Browser Link功能
    unieap 建库
    onserverclick
    工作中记录的命令和知识点(不断更新)
    CentOS 下做端口映射/端口转发
    DELL服务器硬件信息采集SHELL脚本
    Linux中变量#,@,0,1,2,*,$$,$?的意思
  • 原文地址:https://www.cnblogs.com/zxhl/p/6638762.html
Copyright © 2011-2022 走看看