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<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
char str[100005];
int dp[100005];
int sum[100005];
int hashh[5];
int rec[100005];
int ans[100005];
int len;
void output()
{
int ss=0;
for (int i=0;i<len;i++)
{
if (rec[i]!=-1) ans[ss++]=str[i]-'0';//printf("%c",str[i]);
}
int pos=-1;
for (int i=0;i<ss;i++)
{
if (ans[i]!=0) {pos=i;break;}
}
if (pos==-1) printf("0");
else
{
for (int i=pos;i<ss;i++) printf("%d",ans[i]);
}
printf("\n");
}
int main()
{
scanf("%s",str);
len=strlen(str);
//dp[0]=(str[0]-'0')%3;
for (int i=0;i<len;i++)
{
dp[i]=(str[i]-'0')%3;
hashh[dp[i]]++;
}
for (int i=0;i<len-1;)
{
if (str[i+1]=='0')
{
int j;
int cnt=0;
for (j=i+1;j<len;j++)
if (str[j]!='0') break;
else cnt++;
sum[i]=cnt;
i=j;
}else
{
i++;
}
}
//cout<<hashh[0]<<" "<<hashh[1]<<" "<<hashh[2]<<endl;
if (!hashh[0])
{
if (hashh[1]<=2&&hashh[2]==0&&hashh[0]==0) {printf("-1\n");return 0;}
if (hashh[2]<3&&hashh[1]==0&&hashh[0]==0) {printf("-1\n");return 0;}
}
if ((hashh[1]+2*hashh[2])%3==0){output();return 0;}
int cntt=0;
if (hashh[1]>hashh[2])
{
int num=hashh[1]-hashh[2];
if (hashh[2]==0)
{
if (hashh[1]<3)
{
//rec[0]=-1;
for (int i=0;i<len;i++)
{
//cout<<cntt<<endl;
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==hashh[1]) {output();return 0;}
}
}
else
{
for (int i=1;i<len;i++)
{
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==hashh[1]%3) {output();return 0;}
}
}
}else
{
if (hashh[1]%3==0)
{
if (hashh[2]<3)
{
if (dp[0]==2)
{
if (sum[0]+hashh[2]<num%3)
{
for (int i=0;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;}
if (cntt==hashh[2]%3) {output();return 0;}
}
}
}
}
else
{
if (hashh[2]%3<num%3)
{
for (int i=1;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;}
if (cntt==hashh[2]%3) {output();return 0;}
}
}
}
}
if(num%3==2)
{
for (int i=1;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;output();return 0;}
}
}
for (int i=1;i<len;i++)
{
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==num%3) {output();return 0;}
}
}
}
if (hashh[1]==hashh[2]) {output();return 0;}
cntt=0;
if (hashh[1]<hashh[2])
{
int num=hashh[2]-hashh[1];
if (hashh[1]==0)
{
if (hashh[2]<3)
{
for (int i=0;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1; cntt++;}
if (cntt==hashh[2]) {output();return 0;}
}
}else
{
for (int i=1;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;}
if (cntt==hashh[2]%3) {output();return 0;}
}
}
}else
{
if (hashh[2]%3==0)
{
if (hashh[1]<3)
{
if (dp[0]==1)
{ //cout<<sum[0]+hashh[1]<<endl;
if (sum[0]+hashh[1]<num%3)
{
//cout<<sum[0]+hashh[1]<<endl;
for (int i=0;i<len;i++)
{
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==hashh[1]%3) {output();return 0;}
}
}else
{
for (int i=1;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;}
if (cntt==num%3) {output();return 0;}
}
}
}
}
else
{
if (hashh[1]%3<num%3)
{
for (int i=1;i<len;i++)
{
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==hashh[1]%3) {output();return 0;}
}
}
}
}
if (num%3==1)
{
for (int i=1;i<len;i++)
{
if (dp[i]==2) {rec[i]=-1;cntt++;}
if (cntt==1){output();return 0;}
}
}
if (num%3==2)
{
for (int i=1;i<len;i++)
{
if (dp[i]==1) {rec[i]=-1;cntt++;}
if (cntt==1){output();return 0;}
}
}
}
}
return 0;
}