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 mebibytes

A positive integer number n is written on a blackboard. It consists of
not more than 10510^510​5​​ 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≤10​100000​​ ). 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题
题意: 给出一个最大长度为十万位的大数,问最少删去多少个数字后,能使之变为幸运数。弱无法变为幸运数 这输出“-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;}
0 0
原创粉丝点击