woj Divide by Six 数位dp
来源:互联网 发布:捕鱼达人 h5源码 编辑:程序博客网 时间:2024/05/21 11:14
数据可能有前导零
Input file: standard input Output file: standard output Time limit: 1
second Memory limit: 512 mebibytesA positive integer number n is written on a blackboard. It consists of
not more than 10510^5105 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 nnn will find a mogical number
such that nnn 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 nnn.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 nnn – a positive integer (
1≤n≤10100000 1\le n \le 10^{100000} 1≤n≤10100000 ). OutputPrint 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 10010456
Output 1
4
Input 2
11
Output 2
-1s
数位dp题
题意: 给出一个最大长度为十万位的大数,问最少删去多少个数字后,能使之变为幸运数。弱无法变为幸运数 这输出“-1s”;
幸运数的定义: 无导前零,%6=0;
定义一个二位dp数组 dp[i][j] 表示为 前i个数 %6=j 所需要删去的最小的数。
#include<iostream>#include<cstdio>#include<string.h>#include<string>#include<queue>#include<algorithm>#include<vector>using namespace std;int dp[100005][7];char s[100005];int main(){ int n; int ans=0x3f3f3f3f; while(~scanf("%s",s+1)) { n=strlen(s+1); memset(dp,0x3f,sizeof dp); for(int i=1;i<=n;i++) if(s[i]=='0') ans=n-1; for(int i=1;i<=n;i++) { if(s[i]!='0') { int id=(s[i]-'0')%6; dp[i][id]=min(dp[i][id],i-1); } for(int j=0;j<6;j++) { dp[i][j]=min(dp[i][j],dp[i-1][j]+1); int next=(j*10+s[i]-'0')%6; dp[i][next]=min(dp[i][next],dp[i-1][j]); } } ans=min(ans,dp[n][0]); if(ans == 0x3f3f3f3f) printf("-1s\n"); else printf("%d\n", n - ans); } return 0;}
- woj Divide by Six 数位dp
- woj~24. Divide by Six(dp)
- woj-Divide by Six
- WOJ 24. Divide by Six
- woj-Divide by Six (dp)(方法巧妙)
- Divide by Six
- dp处理大数整除6的最大位数Divide by Six
- 17年武汉大学网络赛—Divide by Six
- 【数位dp】 Step by Step
- 2017武大校赛网络预选赛C题 Divide by Six
- (原创)数位DP专题小结--by sgx
- 数位DP专题小结--by sgx
- woj 1049 dp
- hdu 2697 WOJ dp
- WOJ 1608 状压dp
- WOJ-29 Werewolf(DP)
- CF 792C Divide by Three DP,记忆化搜索
- 数位DP专题小结--by sgx 数位DP专题小结--by sgx
- 数组
- 索引
- Openstack :1、centos7-minamal虚拟机安装devstack
- React Native 实战系列一
- C++第三次实验项目2
- woj Divide by Six 数位dp
- 网络编程_发送端的数据来自于键盘录入案例
- -----hdu2504 又见GCD
- 缓存之页面部分缓存
- mac os 搭建arm-gcc
- Attr、Style和Theme详解
- springmvc @modelAttribute
- VRP
- POJ 3614 Sunscreen(优先队列)