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
output
input
output
input
output
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;
}
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;
}
相关文章推荐
- Codeforces 792C Divide by Three【Dp+记录路径】
- (2017/4、8、/ B - Divide by Three) CodeForces - 792C (dp)(暴力破解)
- CodeForces 792C - Divide by Three [ 分类讨论 ]
- codeforces 792C —— Divide by Three(分类讨论)
- codeforce 792C - Divide by Three(思维)
- [CF792C] Divide by Three(dp,记录路径)
- 【codeforces 792C】Divide by Three
- 792C - Divide by Three
- Codeforces 729 C. Divide by Three
- codeforeces 792 C. Divide by Three (dp || 贪心)
- CodeForces 149D Coloring Brackets(区间DP)
- HDU 4301 Divide Chocolate(打表+dp)
- CodeForces 15 E.Triangles(组合数学+dp)
- Codeforces 766 C Mahmoud and a Message 详解(DP)
- Codeforces 407B Long Path【dp】好题
- 【DP】 codeforces 464C Substitutes in Number
- CodeForces - 835C Star sky(dp)
- Codeforces 696B 树形dp,概率
- CodeForces 123 C.Brackets(组合数学+dp)
- codeforces 294B Shaass and Bookshelf (暴力dp)