Divide by Three

来源:互联网 发布:李强强php 编辑:程序博客网 时间:2024/06/05 04:43

C. Divide by Three
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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, 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
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.

思路:这个题真的是巨恶心,需要考虑的东西十分的多,大概就是对于不同情况分别讨论。

#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <cstdio>#include <queue>#include <stack>#include <vector>#include <set>#include <map>#include <typeinfo>#define MAXN 300000#define INF 1 << 29using namespace std;int main() {    string x;    while (cin >> x) {        int sum = 0;        int pos = -1;        for (int i = 0; i < x.length(); ++i) {            sum += x[i] - '0';            if (pos == -1 && x[i] == '0') {                pos = i;            }        }        if (sum % 3 == 0)  // 如果能整除3不用做处理,直接输出            cout << x << endl;        else if (sum < 3) { // 小于3的情况,有零输出0,没有的话说明不可能达成,输出-1            if (pos == -1)                cout << -1 << endl;            else                cout << 0 << endl;        } else {            int ans1 = 0, ans2 = 0;            string y = x;            if (sum % 3 == 1) {                bool flag = true;                bool F = true;                for (int i = x.length() - 1; i >= 0; --i) { // 这里倒着进行删除的操作,因为我一开始使用正向删除发现会多删除0,导致操作不是最优                    if ((x[i] - '0') % 3 == 1) {                        ans1++;                        x.erase(x.begin() + i);                        flag = false;                        break;                    }                }                int pos_num = -1;                for (int i = 0; i < x.length(); ++i) {                    if (x[i] != '0')                        break;                    ans1++;                    pos_num = i;                }                if (x.length()) {                    if (pos_num == x.length() - 1)                        pos_num -= 1;                    x = x.substr(pos_num + 1);                }                int pos1 = -1;                int pos2 = -1;                int cnt = 0;                for (int i = y.length() - 1; i >= 0; --i) {                    if ((y[i] - '0') % 3 == 2) {                        cnt++;                        if (cnt == 1)                            pos1 = i;                        if (cnt == 2) {                            pos2 = i;                            break;                        }                    }                }                if (cnt == 2) {                    F = false;                    ans2 += 2;                    y.erase(y.begin() + pos1);                    y.erase(y.begin() + pos2);                }                pos_num = -1;                for (int i = 0; i < y.length(); ++i) {                    if (y[i] != '0')                        break;                    ans2++;                    pos_num = i;                }                if (y.length()) { // 如果字符串已经为空的话就不能进行操作,否则会re                    if (pos_num == y.length() - 1)                        pos_num -= 1;                    y = y.substr(pos_num + 1);                }                // 接下来是针对两种方案进行比较                if (!flag && !F) {                    if (ans1 > ans2) {                        if (y.length())                            cout << y << endl;                        else                            cout << -1 << endl;                    }                    else {                        if (x.length())                            cout << x << endl;                        else                            cout << -1 << endl;                    }                }                else if (!F) {                    if (y.length())                        cout << y << endl;                    else                        cout << -1 << endl;                }                else if (!flag) {                    if (x.length())                        cout << x << endl;                    else                        cout << -1 << endl;                }                else {                    cout << -1 << endl;                }            } else {                bool flag = true;                bool F = true;                for (int i = x.length() - 1; i >= 0; --i) {                    if ((x[i] - '0') % 3 == 2) {                        ans1++;                        x.erase(x.begin() + i);                        flag = false;                        break;                    }                }                int pos_num = -1;                for (int i = 0; i < x.length(); ++i) {                    if (x[i] != '0')                        break;                    ans1++;                    pos_num = i;                }                if (x.length()) {                    if (pos_num == x.length() - 1)                        pos_num -= 1;                    x = x.substr(pos_num + 1);                }                int pos1 = -1;                int pos2 = -1;                int cnt = 0;                for (int i = y.length() - 1; i >= 0; --i) {                    if ((y[i] - '0') % 3 == 1) {                        cnt++;                        if (cnt == 1)                            pos1 = i;                        if (cnt == 2) {                            pos2 = i;                            break;                        }                    }                }                if (cnt == 2) {                    F = false;                    ans2 += 2;                    y.erase(y.begin() + pos1);                    y.erase(y.begin() + pos2);                }                pos_num = -1;                for (int i = 0; i < y.length(); ++i) {                    if (y[i] != '0')                        break;                    ans2++;                    pos_num = i;                }                if (y.length()) {                    if (pos_num == y.length() - 1)                        pos_num -= 1;                    y = y.substr(pos_num + 1);                }                if (!flag && !F) {                    if (ans1 > ans2) {                        if (y.length())                            cout << y << endl;                        else                            cout << -1 << endl;                    }                    else {                        if (x.length())                            cout << x << endl;                        else                            cout << -1 << endl;                    }                }                else if (!F) {                    if (y.length())                        cout << y << endl;                    else                        cout << -1 << endl;                }                else if (!flag) {                    if (x.length())                        cout << x << endl;                    else                        cout << -1 << endl;                }                else {                    cout << -1 << endl;                }            }        }    }    return 0;}
1 0
原创粉丝点击