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
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得到三种情况,余0时直接输出,1时删除一个模3余1的数字或删除两个模3余2的数字,余2时删除一个模3余2的数字或删除两个模3余1的数字,分开讨论即可。
AC代码
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e6 + 5;
const ll mod = 1e9+7;
char str[maxn];
bool vis[10][maxn];
char ans[10][maxn];
int num[maxn];
int len;
void getans(int x)
{
int k=0;
bool judge=false;
for(int i=0;i<len;i++) {
if(vis[x][i]==false) {
if(str[i]=='0') {
if(judge==true) {
ans[x][k]=str[i];
k++;
}
else
continue;
}
else
{
judge=true;
ans[x][k]=str[i];
k++;
}
}
}
if(k==0) {
for(int i=0;i<len;i++) {
if(str[i]=='0')
{
ans[x][0]='0';
break;
}
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ios::sync_with_stdio(0),cin.tie(0);
scanf("%s",str);
len=strlen(str);
int sum=0;
for(int i=0;i<len;i++) {
num[(str[i]-'0')%3]++;
sum+=(str[i]-'0')%3;
}
if(sum%3==0)
printf("%s\n",str);
else if(sum%3==1)
{
for(int i=len-1;i>=0;i--) {
if((str[i]-'0')%3==1)
{
vis[1][i]=true;
break;
}
}
int t1=0;
for(int i=len-1;i>=0;i--) {
if((str[i]-'0')%3==2)
{
vis[2][i]=true;
t1++;
}
if(t1==2) {
break;
}
}
getans(1);
getans(2);
int n1=strlen(ans[1]);
int n2=strlen(ans[2]);
if(num[1]>=1&&num[2]>=2)
{
if(n1==0&&n2==0)
printf("-1\n");
else
{
if(n1<=n2)
printf("%s\n",ans[2]);
else
printf("%s\n",ans[1]);
}
}
else if(num[1]>=1)
{
if(n1==0)
printf("-1\n");
else
printf("%s\n",ans[1]);
}
else if(num[2]>=2)
{
if(n2==0)
printf("-1\n");
else
printf("%s\n",ans[2]);
}
else
printf("-1\n");
}
else
{
for(int i=len-1;i>=0;i--) {
if((str[i]-'0')%3==2)
{
vis[3][i]=true;
break;
}
}
int t2=0;
for(int i=len-1;i>=0;i--) {
if((str[i]-'0')%3==1)
{
vis[4][i]=true;
t2++;
}
if(t2==2) {
break;
}
}
getans(3);
getans(4);
int n3=strlen(ans[3]);
int n4=strlen(ans[4]);
if(num[1]>=2&&num[2]>=1)
{
if(n3==0&&n4==0)
printf("-1\n");
else
{
if(n3<=n4)
printf("%s\n",ans[4]);
else
printf("%s\n",ans[3]);
}
}
else if(num[2]>=1)
{
if(n3==0)
printf("-1\n");
else
printf("%s\n",ans[3]);
}
else if(num[1]>=2)
{
if(n4==0)
printf("-1\n");
else
printf("%s\n",ans[4]);
}
else
printf("-1\n");
}
return 0;
}