码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 18 C. Divide by Three DP

时间:2017-03-29 11:36:10      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:cti   link   路径   minimum   graph   删除   nbsp   output   ons   

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;
}

 

  

Educational Codeforces Round 18 C. Divide by Three DP

标签:cti   link   路径   minimum   graph   删除   nbsp   output   ons   

原文地址:http://www.cnblogs.com/zxhl/p/6638762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!