792C - Divide by Three

探讨如何通过删除部分数字将一个正整数转换为尽可能长的3的倍数,并确保该数不含前导零。采用多种策略遍历所有可能的数字组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Divide by Three
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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, 09910110 are beautiful numbers, and 0003122 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.



删掉几个位置,,,使得这个数是3的倍数..删完后长度要最长.

写复杂了,,前导0考虑下..然后直接暴力判断,从后面删前面删各枚举一遍...

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
string str;
string ss;
string sss;
int max1;
string ans;
int vis[10];
int l;
void change() {
    if(ss==str)
        return;
    int l1=ss.size();
    if(l1==1) {
        if(l1>max1) {
            max1=1;
            ans=ss;
        }
    } else {
        int x;
        int flag=0;
        for(int i=0; i<l1; i++)
            if(ss[i]!='0') {
                flag=1;
                x=i;
                break;
            }
        if(flag) {
            ss="";
            for(int i=x; i<l1; i++)
                ss+=sss[i];
            l1=ss.size();
        }
        if(l1>max1) {
            max1=l1;
            ans=ss;
        }
    }
}
void work(int m,int k,int kk,int kkk,int kkkk,int kkkkk,int kkkkkk) {
    int op=k*1+kk*2+kkk*7+kkkk*4+kkkkk*5+kkkkkk*8;
    if(vis[1]>=k&&vis[2]>=kk&&vis[7]>=kkk&&vis[4]>=kkkk&&vis[5]>=kkkkk&&vis[8]>=kkkkkk&&(op%3==m)) {
        int qq=0;
        int qqq=0;
        int qqqq=0;
        int qqqqq=0;
        int qqqqqq=0;
        int qqqqqqq=0;
        ss=sss="";
        for(int i=0; i<l; i++) {
            if(str[i]=='1'&&qq<k) {
                qq++;
                continue;
            }
            if(str[i]=='2'&&qqq<kk) {
                qqq++;
                continue;
            }
            if(str[i]=='7'&&qqqq<kkk) {
                qqqq++;
                continue;
            }
            if(str[i]=='4'&&qqqqq<kkkk) {
                qqqqq++;
                continue;
            }
            if(str[i]=='5'&&qqqqqq<kkkkk) {
                qqqqqq++;
                continue;
            }
            if(str[i]=='8'&&qqqqqqq<kkkkkk) {
                qqqqqqq++;
                continue;
            }
            ss+=str[i];
            sss+=str[i];
        }
        change();
        string a;
        ss=sss="";
        qq=qqq=qqqq=qqqqq=qqqqqq=qqqqqqq=0;
        for(int i=l-1; i>=0; i--) {
            if(str[i]=='1'&&qq<k) {
                qq++;
                continue;
            }
            if(str[i]=='2'&&qqq<kk) {
                qqq++;
                continue;
            }
            if(str[i]=='7'&&qqqq<kkk) {
                qqqq++;
                continue;
            }
            if(str[i]=='4'&&qqqqq<kkkk) {
                qqqqq++;
                continue;
            }
            if(str[i]=='5'&&qqqqqq<kkkkk) {
                qqqqqq++;
                continue;
            }
            if(str[i]=='8'&&qqqqqqq<kkkkkk) {
                qqqqqqq++;
                continue;
            }
            a+=str[i];
        }
        int op=a.size();
        for(int i=op-1; i>=0; i--) {
            ss+=a[i];
            sss+=a[i];
        }
        change();
    }
}
int main() {
    while(cin>>str) {
        l=str.size();
        int sum=0;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<l; i++) {
            sum+=str[i]-'0';
            vis[str[i]-'0']++;
        }
        if(sum%3==0) {
            string qqqq="";
            int qer=-1;
            int ans1=0;
            for(int i=0; i<l; i++)
                if(str[i]!='0') {
                    qer=i;
                    break;
                } else {
                    ans1++;
                }
            if(ans1==l) {
                cout<<0<<endl;
                continue;
            }
            for(int i=qer; i<l; i++)
                qqqq+=str[i];
            cout<<qqqq<<endl;
        } else {
            max1=-9999999;
            ans="";
            if(l==1) {
                cout<<-1<<endl;
            } else {
                if(sum%3==1) {  //1 4 7    22  16  25 115 112 2221 1111 1114 11122 111112 1111111
                    for(int k=0; k<=1; k++)  //1
                        for(int kk=0; kk<=2; kk++) //2
                            for(int kkk=0; kkk<=1; kkk++) //7
                                for(int kkkk=0; kkkk<=1; kkkk++)  //4
                                    for(int kkkkk=0; kkkkk<=2; kkkkk++)  //5
                                        for(int kkkkkk=0; kkkkkk<=2; kkkkkk++) { //8
                                            work(1,k,kk,kkk,kkkk,kkkkk,kkkkkk);
                                        }
                } else if(sum%3==2) {   // 2 5 8   11
                    for(int k=0; k<=2; k++)  //1
                        for(int kk=0; kk<=1; kk++) //2
                            for(int kkk=0; kkk<=2; kkk++) //7
                                for(int kkkk=0; kkkk<=2; kkkk++)  //4
                                    for(int kkkkk=0; kkkkk<=1; kkkkk++)  //5
                                        for(int kkkkkk=0; kkkkkk<=1; kkkkkk++) { //8
                                            work(2,k,kk,kkk,kkkk,kkkkk,kkkkkk);
                                        }
                }
                int op1=ans.size();
                int res=0;
                for(int i=0; i<op1; i++)
                    if(ans[i]>='0'&&ans[i]<='9')
                        res+=ans[i]-'0';
                if(res%3==0&&ans!="") {
                    if(res==0)
                        cout<<0<<endl;
                    else
                        cout<<ans<<endl;
                } else
                    cout<<-1<<endl;
            }
        }
    }
    return 0;
}


提示报错NXOpen::Point3d没有成员DistanceTo,。参考下下面的帮助文档,以下是参考文档:#ifndef NXOpen_UGMATH_HXX_INCLUDED #define NXOpen_UGMATH_HXX_INCLUDED //-------------------------------------------------------------------------- // Header for C++ interface to JA API //-------------------------------------------------------------------------- // // Source File: // ugmath.ja // // Generated by: // apiwrap // // WARNING: // This file is automatically generated - do not edit by hand // #ifdef _MSC_VER #pragma once #endif #include <NXOpen/NXDeprecation.hxx> #include <vector> #include <NXOpen/NXString.hxx> #include <NXOpen/Callback.hxx> #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable:4996) #endif #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif namespace NXOpen { /** Represents three-dimensional cartesian coordinates for a point <br> Created in NX3.0.0. <br> */ struct Point3d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: Point3d() : X(), Y(), Z() { } /** Constructor for the Point3d struct. */ public: Point3d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ ) : X(xInitial), Y(yInitial), Z(zInitial) { } }; /** Represents a three-dimensional vector <br> Created in NX3.0.0. <br> */ struct Vector3d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: Vector3d() : X(), Y(), Z() { } /** Constructor for the Vector3d struct. */ public: Vector3d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ ) : X(xInitial), Y(yInitial), Z(zInitial) { } }; /** Represents a 3 x 3 matrix <br> Created in NX3.0.0. <br> */ struct Matrix3x3 { public: /** xx */ double Xx; public: /** xy */ double Xy; public: /** xz */ double Xz; public: /** yx */ double Yx; public: /** yy */ double Yy; public: /** yz */ double Yz; public: /** zx */ double Zx; public: /** zy */ double Zy; public: /** zz */ double Zz; public: Matrix3x3() : Xx(), Xy(), Xz(), Yx(), Yy(), Yz(), Zx(), Zy(), Zz() { } /** Constructor for the Matrix3x3 struct. */ public: Matrix3x3(double xxInitial /** xx */ , double xyInitial /** xy */ , double xzInitial /** xz */ , double yxInitial /** yx */ , double yyInitial /** yy */ , double yzInitial /** yz */ , double zxInitial /** zx */ , double zyInitial /** zy */ , double zzInitial /** zz */ ) : Xx(xxInitial), Xy(xyInitial), Xz(xzInitial), Yx(yxInitial), Yy(yyInitial), Yz(yzInitial), Zx(zxInitial), Zy(zyInitial), Zz(zzInitial) { } }; /** Represents two-dimensional cartesian coordinates for a point <br> Created in NX6.0.0. <br> */ struct Point2d { public: /** x */ double X; public: /** y */ double Y; public: Point2d() : X(), Y() { } /** Constructor for the Point2d struct. */ public: Point2d(double xInitial /** x */ , double yInitial /** y */ ) : X(xInitial), Y(yInitial) { } }; /** Represents four-dimensional homogeneous coordinates for a point. To convert to three-dimensional cartesian coordinates, divide x, y and z by w. <br> Created in NX8.5.0. <br> */ struct Point4d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: /** weight */double W; public: Point4d() : X(), Y(), Z(), W() { } /** Constructor for the Point4d struct. */ public: Point4d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ , double wInitial /** weight */) : X(xInitial), Y(yInitial), Z(zInitial), W(wInitial) { } }; } #ifdef _MSC_VER #pragma warning(pop) #endif #ifdef __GNUC__ #ifndef NX_NO_GCC_DEPRECATION_WARNINGS #pragma GCC diagnostic warning "-Wdeprecated-declarations" #endif #endif #endif
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值