您的位置:首页 > 其它

codeforces 792 C. 删除尽量少的位数后是3的倍数 (取余问题+分类讨论+模拟吧)

2017-04-16 17:11 483 查看
题意:给出一个正整数n,问用最少的删除操作是它变成美丽数。 
美丽数的定义:没有前导0,且是3的倍数。输出任意美丽数,无解输出-1。 
题解:是三的倍数,和也是三的倍数。求和mod3之后,有三种情况, 
1:和为0,即本身就是美丽数,直接输出。 
2:和为1,此时应该删除一个1(mod3为1,下同)或者两个2。 
3:和为2,此时应该删除一个2或者两个1。 

这里总结了一下,以后有取模的问题,我们要取模后去考虑问题,

这样可以把数据范围降低,方便思考,方便解答!!!!ORZ

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<bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int inf = 0x3f3f3f3f;
char s
;
int a
,vis
,n;
int work(int num,int cnt)
{
int i,j,ans;
ans=cnt;
for(i=n-1;i>=0;i--) {
if(cnt==0) break;
if(a[i]%3==num) {
cnt--;
vis[i]=1;
}
}
for(i=0;i<n;i++) {
if(vis[i]) continue;
if(a[i]==0) {
ans++;
vis[i]=1;
}
else break;
}
if(cnt) ans=inf;
return ans;
}
int main()
{
int i,j,sum,zero=0;
int op1,op2;
scanf("%s",s);
n=strlen(s);
sum=0;
for(i=0;i<n;i++) {
a[i]=s[i]-'0';
if(a[i]==0) zero=1;
sum+=a[i];
sum=sum%3;
}
if(sum==0) {
printf("%s",s);
}
else if(sum==1) {
memset(vis,0,sizeof(vis));
op1 = work(1,1);
memset(vis,0,sizeof(vis));
op2 = work(2,2);
memset(vis,0,sizeof(vis));
if(min(op1,op2)==n) {
if(zero) printf("0\n");
else printf("-1\n");
}
else if(op1==op2 && op1==inf) printf("-1\n");
else if(op1<op2) {
work(1,1);
for(i=0;i<n;i++)
if(!vis[i]) printf("%d",a[i]);
printf("\n");
}
else {
work(2,2);
for(i=0;i<n;i++)
if(!vis[i]) printf("%d",a[i]);
printf("\n");
}
}
else {
memset(vis,0,sizeof(vis));
op1 = work(1,2);
memset(vis,0,sizeof(vis));
op2 = work(2,1);
memset(vis,0,sizeof(vis));
if(min(op1,op2)==n) {
if(zero) printf("0\n");
else printf("-1\n");
}
else if(op1==op2 && op1==inf) printf("-1\n");
else if(op1<op2) {
work(1,2);
for(i=0;i<n;i++)
if(!vis[i]) printf("%d",a[i]);
printf("\n");
}
else {
work(2,1);
for(i=0;i<n;i++)
if(!vis[i]) printf("%d",a[i]);
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: