Educational Codeforces Round 18 C. Divide by Three DP
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.
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print - 1.
1033
33
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.
题意:
给你一个01串,问你最少删除多少个字符,使得余下的串10进制下%3=0,不得有前导0
题解:
设定dp[i][j][0/1/2]表示前i个字符中,组成%3=j的串需要的最少删除次数;
同时0表示还未填数,
1表示有一个前导0,
2表示开头填了一个非0数
需要记录路径pre
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e3+, mod = 1e9+, inf = 2e9; int dp[N][][],pre[N][][];//前i个数mod3 = j最少需要删除的字母个数 是否有前导0
char s[N];
int n,a[N],ans[N];
int main() {
scanf("%s",s+);
int n = strlen(s+);
for(int i = ; i <= n; ++i) a[i] = s[i] - '';
for(int i = ; i <= n; ++i) {
for(int j = ; j < ; ++j) dp[i][j][] = inf,dp[i][j][] = inf, dp[i][j][] = inf;
}
dp[][][] = ;
for(int i = ; i < n; ++i) {
for(int j = ; j < ; ++j) {
if(dp[i][j][] < dp[i+][(j+a[i+])%][(a[i+])==?:]) {
dp[i+][(j+a[i+])%][(a[i+])==?:] = dp[i][j][];
pre[i+][(j+a[i+])%][(a[i+])==?:] = ;
}
if(dp[i][j][]+ < dp[i+][(j+a[i+])%][(a[i+])==?:]) {
dp[i+][(j+a[i+])%][(a[i+])==?:] = dp[i][j][]+;
pre[i+][(j+a[i+])%][(a[i+])==?:] = ;
}
if(dp[i][j][] < dp[i+][(j+a[i+])%][]) {
dp[i+][(j+a[i+])%][] = dp[i][j][];
pre[i+][(j+a[i+])%][] = ;
} if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
if(dp[i][j][]+ < dp[i+][j][]) {
dp[i+][j][] = dp[i][j][]+;
pre[i+][j][] = -;
}
}
}
if(dp[n][][] >= inf && dp[n][][] >= inf) {
puts("-1");
return ;
}
if(dp[n][][] < dp[n][][]) {
puts("");
return ;
}
int j = ,num = n - dp[n][][];
for(int i = n; i >= ; --i) {
if(pre[i][j][] == ) {
ans[num--] = a[i];
j = ((j - a[i])% + ) % ;
}
if(num == ) break;
}
for(int i = ; i <= n - dp[n][][]; ++i) cout<<ans[i];
return ;
}
Educational Codeforces Round 18 C. Divide by Three DP的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 18
A. New Bus Route 题目大意:给出n个不同的数,问差值最小的数有几对.(n<=200,000) 思路:排序一下,差值最小的一定是相邻的,直接统计即可. #include<cs ...
- Educational Codeforces Round 18 D
Description T is a complete binary tree consisting of n vertices. It means that exactly one vertex i ...
- Educational Codeforces Round 18 B
Description n children are standing in a circle and playing the counting-out game. Children are numb ...
- Educational Codeforces Round 18 A
Description There are n cities situated along the main road of Berland. Cities are represented by th ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- Educational Codeforces Round 19 E. Array Queries(暴力)(DP)
传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
随机推荐
- Ubuntu配置TFTP服务器
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务 ...
- 生成 Let's Encrypt 免费https证书
1.打开 SSL For Free 官网:https://www.sslforfree.com/ 2.在输入框内输入你要申请证书的域名,输入完点击 Create Free SSL Certificat ...
- Java:Md5加密
文章来源:https://www.cnblogs.com/hello-tl/p/9139334.html import java.security.MessageDigest; public clas ...
- Python面向对象之模块和包
模块 模块的概念 模块是Python程序架构的一个核心概念 所有以.py结尾的源文件都是一个模块: 模块名也是标识符,需要遵循标识符的命名规则: 在模块中定义的全局变量,类,函数,都是直接给外界使用的 ...
- python对象销毁(垃圾回收)
Python 使用了引用计数这一简单技术来跟踪和回收垃圾. 在 Python 内部记录着所有使用中的对象各有多少引用. 一个内部跟踪变量,称为一个引用计数器. 当对象被创建时, 就创建了一个引用计数, ...
- C语言学习9
婚礼的谎言 三对情侣参加婚礼,三个新郎为A.B.C,三个新娘为X.Y.Z.有人想知道究竟水域谁结婚2,于是就问新人中的三位,得到结果如下:A说他将和X结婚:X说她的未婚夫是C:C说他将和Z结婚.这人事 ...
- LeetCoce 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- DEV Express中NavBarCointrol的使用
尚未对内容进行删减,内容有偏差和冗余,谨慎阅读. 发现在后面,写在前面: 13,之前在Default模式下,之所以很多Appearance属性都起不到作用,是因为Control的LookAndFeel ...
- php 链接 mongodb
1.下载phpmongodb 驱动:https://s3.amazonaws.com/drivers.mongodb.org/php/index.html 2.Precompiled binaries ...
- zoj 2812
Quicksum Time Limit: 2 Seconds Memory Limit: 65536 KB A checksum is an algorithm that scans a p ...