2017武汉大学校赛网络预选赛c题

5 篇文章 0 订阅

无解时输出-1s而不是WTF

数据可能有前导零

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 10^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 nn will find a mogical number such that nn 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 nn.

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 nn – a positive integer ( 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

题意:给你一个非常大的数,让你删除最小个数的数,使之成为一个不含前导零的数,并且这个数可以整除6。最后输出这个数的长度,如果不存在,输出-1s。

解题思路:数位dp,令dp[i][j] 为前i个数字,%6为j的最大长度。然后考虑每个数字,如果把这个数字加进去,长度加一,但是前面的值要乘以10,所有产生新的余数所以状态转移方程为dp[i][(j*10 + s[i])%6] = max(dp[i][(j*10 + s[i])%6],dp[i - 1][j] + 1),如果不加进去状态转移方程为dp[i][j] = max(dp[i - 1][j],dp[i][j])。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int s[maxn];
char ch[maxn];
int dp[maxn][30];
int main()
{
    memset(dp,0,sizeof(dp));
    scanf("%s",ch);
    int len1 = strlen(ch);
    int loc = -1;
    for(int i = 0; i < len1; i++)
    {
        if(ch[i] == '0') continue;
        else
        {
           loc = i;
           break;
        }
    }
    if(loc == -1) printf("-1s\n");
    else
    {
        int len2 = 0;
        bool flag = false;
        for(int i = 1,j = loc; j < len1; j++,i++)
        {
            s[i] = ch[j] - '0';
            len2++;
            if(s[i] == 0) flag = true;
        }
        dp[1][s[1]%6] = 1;
        for(int i = 2; i <= len2; i++)
        {
            if(s[i] != 0) dp[i][s[i]%6] = 1;
            for(int j = 0; j <= 5; j++)
            {
                dp[i][j] = max(dp[i][j],dp[i - 1][j]);
                if(dp[i - 1][j] != 0)
                dp[i][((10*j) + s[i])%6] = max(dp[i][((10*j) + s[i])%6],dp[i - 1][j] + 1);

            }
        }
        if(dp[len2][0] == 0)
        {
            if(flag) printf("1\n");
            else printf("-1s\n");
        }
        else
        printf("%d\n",dp[len2][0]);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值