1. 程式人生 > >Divide by Three CodeForces - 792C

Divide by Three CodeForces - 792C

stack have cto while program other numbers multiple 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.

給定一個串,要求該數去掉盡可能少的位,使得剩下的數可以被3整除

就是所有位的和都可以被3整除

其實最多減兩個 如果 sum%3==1 那麽可以減去兩個 %3==2的 和一個 %3=1的

反之亦然

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-6
14 #define  fi first
15 #define  se second
16 #define  lson l,m,rt<<1
17 #define  rson m+1,r,rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  fuck(x)     cout<<"["<<"x="<<x<<"]"<<endl
21 #define  f(a)        a*a
22 #define  sf(n)       scanf("%d", &n)
23 #define  sff(a,b)    scanf("%d %d", &a, &b)
24 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
26 #define  FIN         freopen("DATA.txt","r",stdin)
27 #define  gcd(a,b)    __gcd(a,b)
28 #define  lowbit(x)   x&-x
29 #pragma  comment (linker,"/STACK:102400000,102400000")
30 using namespace std;
31 typedef long long  LL;
32 typedef unsigned long long ULL;
33 const int INF = 0x7fffffff;
34 const LL  INFLL = 0x3f3f3f3f3f3f3f3fLL;
35 const int mod = 1e9 + 7;
36 const int maxn = 1e4 + 10;
37 string s1, s2, s3;
38 int cal(string s) {
39     int sum = 0;
40     for (int i = 0 ; i < s.size() ; i++) sum = (sum + s[i] - 0) % 3;
41     if (!s.size()) return 1;
42     return sum;
43 }
44 void del(string &s) {
45     while(s[0] == 0 && s.size() > 1) s.erase(0, 1);
46 }
47 int main() {
48     cin >> s1;
49     int tot = cal(s1), num1 = 1, num2 = 2, p = 3 - tot;
50     s2 = s3 = s1;
51     if (!tot) {
52         cout << s1 << endl;
53         return 0;
54     }
55     for (int i = s1.size() ; i >=0 ; i--) {
56         if ((s2[i] - 0)%3 == tot && num1 ) {
57             s2.erase(i, 1);
58             num1--;
59         }
60         if ((s3[i] - 0)%3 == p  && num2) {
61             s3.erase(i, 1);
62             num2--;
63         }
64     }
65     num1 = cal(s2), num2 = cal(s3);
66     del(s2), del(s3);
67     if (num1 && num2) return 0 * printf("-1\n");
68     if (!num1 && !num2) {
69         if (s2.size() > s3.size()) cout << s2 << endl;
70         else cout << s3 << endl;
71     } else if (!num1) cout << s2 << endl;
72     else cout << s3 << endl;
73     return 0;
74 }

Divide by Three CodeForces - 792C