CodeForces - 792C Divide by Three(思路)(分类讨论)

115 篇文章 0 订阅
68 篇文章 0 订阅

Divide by Three

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 beautiful number by erasing some of the digits, and you want to erase as few digits as possible.

The number is called beautiful if it consists of at least one digit, doesn’t have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not.

Write a program which for the given n will find a beautiful 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.

If it’s impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them.

Input
The first line of input contains n — a positive integer number without leading zeroes (1 ≤ n < 10100000).

Output
Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print  - 1.

Examples
input
1033
output
33
input
10
output
0
input
11
output
-1
Note
In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two.

思路:在满足情况下的条件下要么删一个,要么删两个,所以分类讨论就行了。当输入的长度为1或2时特殊判断,因为怕全被删除没了。然后贪心的时候要逆着来,因为可能有前导0(具体详见代码)

代码:

#include<stdio.h>
#include<string.h>

#define maxn 100010
const int inf=0x3f3f3f3f;
char s[maxn];
int len;

void print(int x,int y)//输出
{
    if(x!=-1)
        s[x]='0';
    if(y!=-1)
        s[y]='0';
    int k=0;
    while(s[k]=='0'&&k<len)
        ++k;
    if(k==len)
    {
        printf("0\n");
        return ;
    }
    for(int i=k; i<len; ++i)
        if(i!=x&&i!=y)
            printf("%c",s[i]);
    printf("\n");
}

int cal(int x,int y)//要删除的长度
{
    if(y==-1)
    {
        int k=1;
        while(s[k]=='0')
            ++k;
        return k;
    }
    else
    {
        char a=s[x],b=s[y];
        s[x]=s[y]='0';
        int k=0,cnt=2;
        while(s[k]=='0')
        {
            if(k==x||k==y)
                --cnt;
            ++k;
        }
        s[x]=a,s[y]=b;
        return k+cnt;
    }
}

int main()
{
    scanf("%s",s);
    len=strlen(s);
    int sum=0;
    for(int i=0; i<len; ++i)
        sum=(sum+(s[i]-'0'))%3;
    if(!sum)//能被三整除
    {
        print(-1,-1);
        return 0;
    }
    if(len==1)//长度为1
    {
        if((s[0]-'0')%3==0)
            printf("%s\n",s);
        else printf("-1\n");
    }
    else if(len==2)//长度为2
    {
        if((s[0]-'0'+s[1]-'0')%3==0)
            printf("%s\n",s);
        else if((s[1]-'0')%3==0)
            printf("%c\n",s[1]);
        else if((s[0]-'0')%3==0)
            printf("%c\n",s[0]);
        else printf("-1\n");
    }
    else
    {
        for(int i=len-1; i>=1; --i)//删除一个
        {
            if((s[i]-'0')%3==sum)
            {
                print(i,-1);
                return 0;
            }
        }
        int cnt1=inf;
        if((s[0]-'0')%3==sum)//如果第一个删去符合条件,(后面可能有0)先别删,和删除两个的比较一下
            cnt1=cal(0,-1);
        int x=3-sum,r1=0,r2=0,cnt2=inf;
        for(int i=len-1; ~i; --i)
        {
            if((s[i]-'0')%3==x)
                if(r1)
                {
                    r2=i;
                    cnt2=cal(r1,r2);
                    break;
                }
                else r1=i;
        }
        if(cnt1==inf&&cnt2==inf)
            printf("-1\n");
        else if(cnt1<cnt2)
            print(0,-1);
        else
            print(r1,r2);
    }
    return 0;
}

看了一个大神的代码,发现string对于删除的这种操作真是好用,
这种思路就是另加两个string类,对两种情况进行删除操作,最后比较哪一个更长,输出就好了

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int cal(string s)//判断是否是能被3整除的数列
{
    int sum=0;
    for(int i=0; i<s.length(); ++i)
        sum=(sum+s[i]-'0')%3;
    return sum|!s.length();//当长度为0时说明不能被3整除
}

void deal(string &s)//去除前缀0
{
    while(s[0]=='0'&&s.length()>1)
        s.erase(0,1);
}

int main()
{
    string str,s1,s2;
    cin>>str;
    int tot=cal(str),q=3-tot,w=1,e=2;
    s1=s2=str;
    for(int i=str.length()-1; ~i; --i)
    {
        if((str[i]-'0')%3==tot&&w)//删除一个
            s1.erase(i,1),--w;
        if((str[i]-'0')%3==q&&e)//删除两个
            s2.erase(i,1),--e;
    }
    q=cal(s1),w=cal(s2),deal(s1),deal(s2);
    if(q&&w)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    if(s1.length()>s2.length()&&!q||w)
        s2=s1;
    cout<<s2<<endl;
    return 0;
}
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值