woj~24. Divide by Six(dp)
来源:互联网 发布:大数据建设开展情况 编辑:程序博客网 时间:2024/06/06 00:16
Input file: standard inputOutput file: standard output Time limit: 1 secondMemory limit: 512 mebibytes
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 mogicalnumber by erasing some of the digits, and you want to erase as few digits as possible.
The number is lucky if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 6. For example, 0, 66,66666 are lucky numbers, and 00, 25, 77 are not.
Write a program which for the given n will find a mogical 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.
Print the length of your answer after the erasing.
If it's impossible to obtain a lucky number, print -1s
.
Input
The first line of input contains n -- a positive integer ( 1≤n≤10100000 ).
Output
Print one number — the number of your lucky number obtained by erasing as few as possible digits. If there is no answer, print -1s
.
Example
Input 1
0010456
Output 1
4
Input 2
11
Output 2
-1s
考虑两种情况,即这点取或不取,保存最优状态
dp[i][j]=x,i代表当前数字的位置,j代表取余6的数(把整个字符串当成一个数,j*10+t进行分段取余),而x代表的是dp[i][j]对应的最大长度。
给dp赋初值的时候,是-100005,这个是防止所有都除去的情况,另外就是状态转移方程有两个,表示当前位置取或不取。
#include <iostream>#include<stdio.h>#include<string>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stdlib.h>#include<stdio.h>#include<map>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define ll long long#define inf 100005;using namespace std;char s[100005];int dp[100005][7];int main(){ scanf("%s",s+1); int len=strlen(s+1); for(int i=0;i<=len;i++) { for(int j=0;j<6;j++) { dp[i][j]=-inf; } } int ans=-inf; for(int i=1;i<=len;i++)if(s[i]=='0')ans=1; for(int i=1;i<=len;i++) { int t=s[i]-'0'; if(t!=0) dp[i][t%6]=1; //不取 for(int j=0;j<=5;j++) dp[i][j]=max(dp[i-1][j],dp[i][j]); //取 for(int j=0;j<=5;j++) dp[i][(j*10+t)%6]=max(dp[i-1][j]+1,dp[i][(j*10+t)%6]); ans=max(ans,dp[i][0]); } if(ans>0) printf("%d\n",ans); else printf("-1s\n");}
- woj~24. Divide by Six(dp)
- WOJ 24. Divide by Six
- woj-Divide by Six (dp)(方法巧妙)
- woj Divide by Six 数位dp
- woj-Divide by Six
- Divide by Six
- dp处理大数整除6的最大位数Divide by Six
- 17年武汉大学网络赛—Divide by Six
- 2017武大校赛网络预选赛C题 Divide by Six
- WOJ 29 Werewolf(树形DP+枚举)
- HDU4301 Divide Chocolate(DP)
- woj 1049 dp
- hdu 2697 WOJ dp
- WOJ 1608 状压dp
- WOJ-29 Werewolf(DP)
- CF 792C Divide by Three DP,记忆化搜索
- fzu 1912 Divisibility by Thirty-six(数论+枚举)
- WOJ 题目1538 B - Stones II (DP)
- Editext
- Once框架使用
- fileoutputstream 抛文件找不到异常
- 【recursive autoencoder】运行str2vec
- Linux TCP Socket四次握手
- woj~24. Divide by Six(dp)
- 一个nginx C++ hello_world http module
- HashSet的判断和删除依据
- 【ssm个人博客项目实战03】左侧导航菜单功能实现
- iOS利用iTunesLookup检查更新
- Java连接数据库入门案例
- 数据结构(线性表):线性表的静态链式表示
- Logstash学习13_Logstash处理连续空格
- 1020. Tree Traversals (25)