Educational Codeforces Round 18 -- C. Divide by Three (贪心)

题意:

给你一个大数,长度最大是10w, 要求你删除最小的数字使得这个数不含前导零 并且能整除3.

思路:

贪心:

我们知道  如果一个数 的各个数字之和sum 能整除3 的话,那么这个数就是3 的倍数。

我们先判断 sum % 3 是否等于0  等于0 直接输出。

不等于0 的话, 假设是x 的话,那么另一个相对的是y (y = 3-x)

那么我们可以找到一个数字 他的mod 3 = x 或者删除两个数它的mod 3 = y。

两种方式选择一个最大的即可。

注意前导0 的问题。

#include <bits/stdc++.h>
#define Siz(x) (int)x.size()
using namespace std;

const int maxn = 1e5 + 7;


string s;
int main(){

    ios::sync_with_stdio(false);


    cin >> s;
    int len = Siz(s);

    int sum = 0;
    for (int i = 0; i < len; ++i){
        sum += s[i] - 48;
    }
    sum %= 3;
    if (sum == 0) {

        cout << s << endl;
        return 0;
    }


    string ans = "-1";
    int MAX = 0;
    string tmp = "",tmp2 = "";
    for (int i = Siz(s); i >= 0; --i){
        int v = s[i] - 48;
        if (v % 3 == sum){
            for (int j = 0; j < i; ++j) tmp += s[j];
            for (int j =i+1; j < len; ++j) tmp += s[j];
            int cur = 0;
            while(tmp[cur] == 48)++cur;

            if (cur != Siz(tmp)){
                while(cur != Siz(tmp)){
                    tmp2 += tmp[cur];
                    ++cur;
                }
                if (Siz(tmp2) > MAX){
                    MAX = Siz(tmp2);
                    ans = tmp2;
                }

            }
            else {
                 if (Siz(tmp) > 0) {
                    if (1 > MAX){
                        MAX = 1;
                        ans = "0";
                    }
                }
            }
            break;
        }
    }
    int p1 = -1;
    tmp = tmp2 = "";
    for (int i = Siz(s); i>= 0; --i){
        int v = s[i] - 48;
        if (v % 3 == 3-sum){
            if (p1 == -1) {
                p1=i;
                continue;
            }


            for (int j = 0; j < i; ++j) tmp += s[j];
            for (int j = i+1; j < p1; ++j) tmp+= s[j];
            for (int j =p1+1; j < len; ++j) tmp+=s[j];

            int cur = 0;
            while(tmp[cur] == 48)++cur;

            if (cur != Siz(tmp)){
                while(cur != Siz(tmp)){
                    tmp2 += tmp[cur];
                    ++cur;
                }
                if (Siz(tmp2) > MAX){
                    MAX = Siz(tmp2);
                    ans = tmp2;
                }
            }
            else {
                if (Siz(tmp) > 0) {
                    if (1 > MAX){
                        MAX = 1;
                        ans = "0";
                    }
                }

            }
            break;

        }


    }

    cout << ans << endl;


    return 0;
}


C. Divide by Three
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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, 09910110 are beautiful numbers, and 0003122 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
input
10
output
0
input
11
output
-1
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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值