woj Divide by Six 数位dp

数据可能有前导零

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值