您的位置:首页 > 其它

CodeForces - 792C Divide by Three(思路)(分类讨论)

2017-04-12 12:33 197 查看

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.

思路:在满足情况下的条件下要么删一个,要么删两个,所以分类讨论就行了。当输入的长度为1或2时特殊判断,因为怕全被删除没了。然后贪心的时候要逆着来,因为可能有前导0(具体详见代码)

代码:

#include<stdio.h>
#include<string.h>

#define maxn 100010
const int inf=0x3f3f3f3f;
char s[maxn];
int len;

void print(int x,int y)//输出
{
if(x!=-1)
s[x]='0';
if(y!=-1)
s[y]='0';
int k=0;
while(s[k]=='0'&&k<len)
++k;
if(k==len)
{
printf("0\n");
return ;
}
for(int i=k; i<len; ++i)
if(i!=x&&i!=y)
printf("%c",s[i]);
printf("\n");
}

int cal(int x,int y)//要删除的长度
{
if(y==-1)
{
int k=1;
while(s[k]=='0')
++k;
return k;
}
else
{
char a=s[x],b=s[y];
s[x]=s[y]='0';
int k=0,cnt=2;
while(s[k]=='0')
{
if(k==x||k==y)
--cnt;
++k;
}
s[x]=a,s[y]=b;
return k+cnt;
}
}

int main()
{
scanf("%s",s);
len=strlen(s);
int sum=0;
for(int i=0; i<len; ++i)
sum=(sum+(s[i]-'0'))%3;
if(!sum)//能被三整除
{
print(-1,-1);
return 0;
}
if(len==1)//长度为1
{
if((s[0]-'0')%3==0)
printf("%s\n",s);
else printf("-1\n");
}
else if(len==2)//长度为2
{
if((s[0]-'0'+s[1]-'0')%3==0)
printf("%s\n",s);
else if((s[1]-'0')%3==0)
printf("%c\n",s[1]);
else if((s[0]-'0')%3==0)
printf("%c\n",s[0]);
else printf("-1\n");
}
else
{
for(int i=len-1; i>=1; --i)//删除一个
{
if((s[i]-'0')%3==sum)
{
print(i,-1);
return 0;
}
}
int cnt1=inf;
if((s[0]-'0')%3==sum)//如果第一个删去符合条件,(后面可能有0)先别删,和删除两个的比较一下
cnt1=cal(0,-1);
int x=3-sum,r1=0,r2=0,cnt2=inf;
for(int i=len-1; ~i; --i)
{
if((s[i]-'0')%3==x)
if(r1)
{
r2=i;
cnt2=cal(r1,r2);
break;
}
else r1=i;
}
if(cnt1==inf&&cnt2==inf)
printf("-1\n");
else if(cnt1<cnt2)
print(0,-1);
else
print(r1,r2);
}
return 0;
}


看了一个大神的代码,发现string对于删除的这种操作真是好用,

这种思路就是另加两个string类,对两种情况进行删除操作,最后比较哪一个更长,输出就好了

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int cal(string s)//判断是否是能被3整除的数列
{
int sum=0;
for(int i=0; i<s.length(); ++i)
sum=(sum+s[i]-'0')%3;
return sum|!s.length();//当长度为0时说明不能被3整除
}

void deal(string &s)//去除前缀0
{
while(s[0]=='0'&&s.length()>1)
s.erase(0,1);
}

int main()
{
string str,s1,s2;
cin>>str;
int tot=cal(str),q=3-tot,w=1,e=2;
s1=s2=str;
for(int i=str.length()-1; ~i; --i)
{
if((str[i]-'0')%3==tot&&w)//删除一个
s1.erase(i,1),--w;
if((str[i]-'0')%3==q&&e)//删除两个
s2.erase(i,1),--e;
}
q=cal(s1),w=cal(s2),deal(s1),deal(s2);
if(q&&w)
{
cout<<"-1"<<endl;
return 0;
}
if(s1.length()>s2.length()&&!q||w)
s2=s1;
cout<<s2<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: