您的位置:首页 > 其它

Codeforces-792C:Divide by Three(DP)

2018-01-11 23:44 309 查看
C. Divide by Three

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard 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 beautifulnumber 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.

这个题好像是2017武大校赛-网络选拔赛的一道题,当时用贪心错了很多遍做出来的。。。。

思路:d[i][j][k]表示前i位数取完数后余数为j时,且前导数为k(k=1表示前导不为0)时删除的最少次数。
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
char s[MAX];
int d[MAX][3][2];
void DFS(int k,int m,int tag)
{
//printf("d[%d][%d][%d]=%d\n",k,m,tag,d[k][m][tag]);
if(tag==0)return;
for(int i=0;i<3;i++)
{
if((s[k]-'0'+i)%3==m)
{
if(d[k-1][i][0]!=-1&&d[k-1][i][0]==d[k][m][tag])
{
DFS(k-1,i,0);
printf("%c",s[k]);
return;
}
if(d[k-1][i][1]!=-1&&d[k-1][i][1]==d[k][m][tag])
{
DFS(k-1,i,1);
printf("%c",s[k]);
return;
}
}
if(i==m)
{
if(d[k-1][i][0]!=-1&&d[k-1][i][0]+1==d[k][m][tag])
{
DFS(k-1,i,0);
return;
}
if(d[k-1][i][1]!=-1&&d[k-1][i][1]+1==d[k][m][tag])
{
DFS(k-1,i,1);
return;
}
}
}
}
int main()
{
scanf("%s",s);
int n=strlen(s);
memset(d,-1,sizeof d);
for(int i=0;i<n;i++)
{
if(i==0)
{
d[i][0][0]=1;
d[i][(s[i]-'0')%3][1]=0;
continue;
}
for(int j=0;j<3;j++)
{
if(d[i-1][j][0]!=-1)
{
if(s[i]!='0')
{
if(d[i][(j+s[i]-'0')%3][1]==-1||d[i][(j+s[i]-'0')%3][1]>d[i-1][j][0])d[i][(j+s[i]-'0')%3][1]=d[i-1][j][0];
}
if(d[i][j][0]==-1||d[i][j][0]>d[i-1][j][0]+1)d[i][j][0]=d[i-1][j][0]+1;
}
if(d[i-1][j][1]!=-1)
{
if(d[i][(j+s[i]-'0')%3][1]==-1||d[i][(j+s[i]-'0')%3][1]>d[i-1][j][1])d[i][(j+s[i]-'0')%3][1]=d[i-1][j][1];
if(d[i][j][1]==-1||d[i][j][1]>d[i-1][j][1]+1)d[i][j][1]=d[i-1][j][1]+1;
}
}
}
if(d[n-1][0][1]==-1)
{
for(int i=0;i<n;i++)
{
if(s[i]=='0')
{
puts("0");
return 0;
}
}
puts("-1");
}
else DFS(n-1,0,1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: