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.
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
1033
33
10
0
11
-1
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的倍数
首先就是我们用个数组head来记录每个数字模3之后出现的次数。预处理下各位数字之和sum然后对3取模:
1.如果sum==0。
此时直接输出这个数字就可以了。
2.如果sum==1。
如果"1"出现的次数超过两次那么直接从后面往前删掉一个"1"。注意带引号表示模3之后的数,下面的也是这样。
如果"1"出现的次数为1,并且是以1,0为开头的,那么我们直接看是否能删除掉两个"2"。
剩下的情况我们就看能不能删除掉一个"1"了。
3.sum==2。
类比sum==1的情况。
如果"2"出现的次数超过两次那么直接从后面往前删掉一个"2"。
如果"2"出现的次数为1,并且是以2,0为开头的,那么我们直接看是否能删除掉两个"1"。
剩下的情况我们就看能不能删除掉一个"2"了。
最后查看完之后,要注意:
1.先把前导0去掉,并且要确保剩下的数字位数至少有1位。
2.如果当前没有数字存在了,表明剩下的数字为0,但是又没有0所以无解。
#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e5+7;
const int mod=10007;
char s[MAXN];
vector<int>q;
int head[3];
int main()
{
scanf("%s",s);
int l=strlen(s);
int sum=0;
for(int i=0; i<l; ++i)
{
s[i]-='0';
int t=s[i];
head[t%3]++;
sum+=t;
q.push_back(t);
}
int flag=0;
sum%=3;
//余数为0的情况,直接输出
if(!sum)
{
puts(s);
return 0;
}
//余数为1的情况,只要找到“1”或者两个"2"就表示可以,
else if(sum==1)
{
l=q.size();
if(head[1]>=2)
{
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==1)
{
flag=1;
q.erase(q.begin()+i);
break;
}
}
}
else if((s[0]%3==1&&s[1]==0&&head[1]==1)||!head[1])
{
if(head[2]>=2)
{
int ok=2;
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==2)
{
ok--;
q.erase(q.begin()+i);
if(!ok)
{
flag=1;
break;
}
}
}
}
}
if(!flag)
{
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==1)
{
flag=1;
q.erase(q.begin()+i);
break;
}
}
}
}
//余数为2的情况,先找“2”,再找“1”+“1”.
else
{
l=q.size();
if(head[2]>=2)
{
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==2)
{
flag=1;
q.erase(q.begin()+i);
break;
}
}
}
else if((s[0]%3==2&&s[1]==0&&head[2]==1)||!head[2])
{
if(head[1]>=2)
{
int ok=2;
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==1)
{
ok--;
q.erase(q.begin()+i);
if(!ok)
{
flag=1;
break;
}
}
}
}
}
if(!flag)
{
for(int i=l-1; i>=0; --i)
{
if(q[i]%3==2)
{
flag=1;
q.erase(q.begin()+i);
break;
}
}
}
}
if(!flag)puts("-1");
else
{
while(q[0]==0&&q.size()>1)q.erase(q.begin());
l=q.size();
if(!l)puts("-1");
else
for(int i=0; i<l; ++i)printf("%d",q[i]);
}
return 0;
}