CodeForces - 792C Divide by Three (DP做法)
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 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;
char num[100010];
int dp[100010][3],f[100010][3];
void dfs(int x,int temp){
if (x==0){
return;
}
else{
if (f[x][temp]==3){
dfs(x-1,temp);
}
else{
dfs(x-1,f[x][temp]);
printf("%c",num[x]);
}
}
}
int main(){
scanf("%s",num+1);
int ll=strlen(num+1);
for (int i=0; i<=ll; i++)
for (int j=0; j<3; j++)
dp[i][j]=1e6;
dp[0][0]=0;
int find0=0;
for (int i=1; i<=ll; i++){
int t=num[i]-'0';
if (t==0) find0=1;
for (int j=0; j<3; j++){
if ( dp[i-1][j]+1 < dp[i][j] ){
dp[i][j]=dp[i-1][j]+1;
f[i][j]=3;
}
if (dp[i-1][j]!=i-1 || t!=0)
if ( dp[i-1][j] < dp[i][(j+t)%3] ){
dp[i][(j+t)%3]=dp[i-1][j];
f[i][(j+t)%3]=j;
}
}
}
if (dp[ll][0]==ll)
if (find0) printf("0");
else
printf("-1");
else
dfs(ll,0);
return 0;
}
CodeForces - 792C Divide by Three (DP做法)的更多相关文章
- codeforces 792C. Divide by Three
题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...
- CodeForces 792C - Divide by Three [ 分类讨论 ]
删除最少的数位和前缀0,使得剩下的数能被3整除 等价于各数位数字之和能被3整除. 当前数位和可能是 0, 1, 2(mod 3) 0: 直接处理 1: 删除一个a[i]%3 == 1 或者 两个a[i ...
- codeforces 1288C. Two Arrays(dp)
链接:https://codeforces.com/contest/1288/problem/C C. Two Arrays 题意:给定一个数n和一个数m,让构建两个数组a和b满足条件,1.数组中所有 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- Educational Codeforces Round 18 C. Divide by Three DP
C. Divide by Three A positive integer number n is written on a blackboard. It consists of not more ...
- 【codeforces 792C】Divide by Three
[题目链接]:http://codeforces.com/contest/792/problem/C [题意] 让你删掉最少的数字使得剩下的数字%3==0 [题解] 看代码..内置题解了现在. [完整 ...
- Divide by Three CodeForces - 792C
A positive integer number n is written on a blackboard. It consists of not more than 105 digits. You ...
- codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。
限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...
- [CodeForces - 712D]Memory and Scores (DP 或者 生成函数)
题目大意: 两个人玩取数游戏,第一个人分数一开始是a,第二个分数一开始是b,接下来t轮,每轮两人都选择一个[-k,k]范围内的整数,加到自己的分数里,求有多少种情况使得t轮结束后a的分数比b高. ( ...
随机推荐
- yii学习笔记(7),数据库操作,联表查询
在实际开发中,联表查询是很常见的,yii提供联表查询的方式 关系型数据表:一对一关系,一对多关系 实例: 文章表和文章分类表 一个文章对应一个分类 一个分类可以对应多个文章 文章表:article 文 ...
- webpack4的react打包错误
因为之前一直用的是脚手架创建项目,第一次自己学习创建webpack打包.loader我是复制别人的. module: { loaders: [ { test: /\.js?$/, exclude: / ...
- python网络编程之进程
一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...
- Java虚拟机(JVM)内存区域
Java虚拟机内存区域分为五部分:程序计数器.Java虚拟机栈.本地方法栈.堆.方法区.其中程序计数器.Java虚拟机栈.本地方法栈属于线程私有内存区,其生命周期与线程相同,随线程的产 ...
- 4245: [ONTAK2015]OR-XOR
4245: [ONTAK2015]OR-XOR https://www.lydsy.com/JudgeOnline/problem.php?id=4245 /* 要求分成m份,总价值为a1|a2|a3 ...
- 一 Hive安装及初体验
一 .Hive安装及初体验 1 .hive简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能. 1.1直接使用hadoop面临的问题 ...
- 『Golang』Go简介以及环境搭建
简介 go语言是由Google进行维护的一个编程语言,发布自2009年.其以良好的编程风格.优秀的并发机制被广大的技术人员所接受. 使用go语言开发的优秀的产品: Docker gocode lime ...
- JS中String对象常用的方法
1. stringObject.charAt(index) 参数:index 必需,即字符在字符串中的下标. 返回值: 返回在指定位置的字符.返回的字符是长度为 1的字符串.(length属性 ...
- PS 给天空添加蓝天白云<转载>
https://jingyan.baidu.com/article/b2c186c8e83b1cc46ef6ffee.html 给图片添加蓝天白云的步骤: 1.打开要加蓝天白云的照片.(如图一) [图 ...
- jieba结巴分词
pip install jieba安装jieba模块 如果网速比较慢,可以使用豆瓣的Python源:pip install -i https://pypi.douban.com/simple/ jie ...