792C - Divide by Three

本文介绍了一种算法,用于解决给定由数字组成的字符串,如何通过最少地删除字符,使得剩余的数字能被3整除的问题。文章提供了一个具体的实现方案,包括如何寻找合适的字符进行删除以达到目标条件。

题意:有个string ,由数字组成。 你要尽可能少的删除字符,使得该sting 化的数字能被3整除,输出 删除后的任意结果(不含有前缀0)。
最多删除2个非零字符。sum =2时,最多(s[i]%3==1+s[j]%3==1);
sum = 1 ,最多 (s[i]%3==2+s[j]%3==2)%3
(1)如果 sum % 3==0,直接输出。
(2)s[i]%3 == sum , 直接删除s[i]
( 3 )存在 两个 (s[i] + s[k])%3 == sum ,删除 s[i],s[k] 。 0,3的倍数, s[i] % 3 ==sum ,的不管。

找 s[i] 的时候,从低位往高位走,可以尽量避免前缀0的存在。

#include <bits\stdc++.h>
using namespace std;
string s;
vector<string> ans;

string ts(string ex){
    int n = ex.size();
    int id = 0;
    while(id+1<n && ex[id]=='0') id++; 
    return ex.substr(id);
}

int main(){
    cin>>s;
    int sum = 0, flag = 0;
    int len = s.length();
    for(int i=0;i<len;i++){
        sum  = (sum + s[i]-'0') % 3;
        if(s[i] == '0') flag = 1;
    }
    if(sum==0){
        cout<<s<<endl;
        return 0;
    }   
    for(int i=len-1;i>=0;i--){
        int x = s[i]-'0';
        if(x%3 == sum ){
            string t = s.substr(0,i) + s.substr(i+1);
            t = ts(t);
            if(!t.empty()){
                ans.push_back(t); 
                break;
            }
        }
    }
    int k = -1; 
    for(int i=len-1;i>=0;i--){
        int x= s[i]-'0';
        if(x%3==0 || x%3 == sum) continue;
        if(k==-1) {
            k = i;
            continue;
        }
       string str = s.substr(0,i);                  // 0 i-1 
       if(i+1 != k) str += s.substr(i+1,k-i-1);    // i+1 k-1  
       if(k+1 < len) str += s.substr(k+1);          // k+1  
        str = ts(str);
        if(!str.empty()) {
          ans.push_back(str);   
        };
        break;
    }
    if(ans.empty()) {
        if(flag) printf("0\n");
        else printf("-1\n");
    }
    else{
        string ss;
        int lens = ans.size(), leng = 0;
        for(int i=0;i<lens;i++){
            if(ans[i].length() > leng ){
                ss = ans[i];
                leng = ans[i].length();
            }
        }
        cout<<ss<<"\n";
    }
    return 0;
}
/* ================================================================================= File name: SVGEN_DQ.H (IQ version) Originator: Digital Control Systems Group Texas Instruments Description: Header file containing constants, data type, and macro definitions for the SVGEN_DQ module . ===================================================================================== History: ------------------------------------------------------------------------------------- 4-15-2010 Version 1.1 ------------------------------------------------------------------------------*/ #ifndef __SVGEN_DQ_H__ #define __SVGEN_DQ_H__ typedef struct { _iq Ualpha; // Input: reference alpha-axis phase voltage _iq Ubeta; // Input: reference beta-axis phase voltage _iq Ta; // Output: reference phase-a switching function _iq Tb; // Output: reference phase-b switching function _iq Tc; // Output: reference phase-c switching function Uint16 N; } SVGENDQ; typedef SVGENDQ *SVGENDQ_handle; /*----------------------------------------------------------------------------- Default initalizer for the SVGENDQ object. -----------------------------------------------------------------------------*/ #define SVGENDQ_DEFAULTS { 0,0,0,0,0,0 } /*------------------------------------------------------------------------------ Space Vector PWM Generator (SVGEN_DQ) Macro Definition ------------------------------------------------------------------------------*/ _iq Va,Vb,Vc,t1,t2,temp_sv1,temp_sv2; Uint16 Sector = 0; // Sector is treated as Q0 - independently with global Q #define SVGEN_MACRO(v) \ \ Sector = 0; \ temp_sv1=_IQdiv2(v.Ubeta); /*divide by 2*/ \ temp_sv2=_IQmpy(_IQ(0.8660254),v.Ualpha); /* 0.8660254 = sqrt(3)/2*/ \ \ /* Inverse clarke transformation */ \ Va = v.Ubeta; \ Vb = -temp_sv1 + temp_sv2; \ Vc = -temp_sv1 - temp_sv2; \ /* 60 degree Sector determination */ \ if (Va>_IQ(0)) Sector = 1; \ if (Vb>_IQ(0)) Sector = Sector+2; \ if (Vc>_IQ(0)) Sector = Sector+4; \ v.N = Sector; \ /* X,Y,Z (Va,Vb,Vc) calculations X = Va, Y = Vb, Z = Vc */ \ Va = v.Ubeta; \ Vb = temp_sv1 + temp_sv2; \ Vc = temp_sv1 - temp_sv2; \ /* Sector 0: this is special case for (Ualpha,Ubeta) = (0,0)*/ \ switch(Sector) \ { \ case 0: \ v.Ta = _IQ(0.5); \ v.Tb = _IQ(0.5); \ v.Tc = _IQ(0.5); \ break; \ case 1: /*Sector 1: t1=Z and t2=Y (abc ---> Tb,Ta,Tc)*/ \ t1 = Vc; \ t2 = Vb; \ v.Tb=_IQdiv2((_IQ(1)-t1-t2)); \ v.Ta = v.Tb+t1; /* taon = tbon+t1 */ \ v.Tc = v.Ta+t2; /* tcon = taon+t2 */ \ break; \ case 2: /* Sector 2: t1=Y and t2=-X (abc ---> Ta,Tc,Tb)*/ \ t1 = Vb; \ t2 = -Va; \ v.Ta=_IQdiv2((_IQ(1)-t1-t2)); \ v.Tc = v.Ta+t1; /* tcon = taon+t1 */ \ v.Tb = v.Tc+t2; /* tbon = tcon+t2 */ \ break; \ case 3: /* Sector 3: t1=-Z and t2=X (abc ---> Ta,Tb,Tc)*/ \ t1 = -Vc; \ t2 = Va; \ v.Ta=_IQdiv2((_IQ(1)-t1-t2)); \ v.Tb = v.Ta+t1; /* tbon = taon+t1 */ \ v.Tc = v.Tb+t2; /* tcon = tbon+t2 */ \ break; \ case 4: /* Sector 4: t1=-X and t2=Z (abc ---> Tc,Tb,Ta)*/ \ t1 = -Va; \ t2 = Vc; \ v.Tc=_IQdiv2((_IQ(1)-t1-t2)); \ v.Tb = v.Tc+t1; /* tbon = tcon+t1 */ \ v.Ta = v.Tb+t2; /* taon = tbon+t2 */ \ break; \ case 5: /* Sector 5: t1=X and t2=-Y (abc ---> Tb,Tc,Ta)*/ \ t1 = Va; \ t2 = -Vb; /* tbon = (1-t1-t2)/2 */ \ v.Tb=_IQdiv2((_IQ(1)-t1-t2)); \ v.Tc = v.Tb+t1; /* taon = tcon+t2 */ \ v.Ta = v.Tc+t2; \ break; \ case 6: /* Sector 6: t1=-Y and t2=-Z (abc ---> Tc,Ta,Tb)*/ \ t1 = -Vb; \ t2 = -Vc; \ v.Tc=_IQdiv2((_IQ(1)-t1-t2)); \ v.Ta = v.Tc+t1; /* taon = tcon+t1 */ \ v.Tb = v.Ta+t2; /* tbon = taon+t2 */ \ break; \ } \ /* Convert the unsigned GLOBAL_Q format (ranged (0,1)) ->.. */ \ /* ..signed GLOBAL_Q format (ranged (-1,1))*/ \ v.Ta = _IQmpy2(v.Ta-_IQ(0.5)); \ v.Tb = _IQmpy2(v.Tb-_IQ(0.5)); \ v.Tc = _IQmpy2(v.Tc-_IQ(0.5)); \ #endif // __SVGEN_DQ_H__#define PWM_MACRO(ch1,ch2,ch3,v) \ \ /* Mfuncx range is (-1,1) */ \ /* The code below changes PeriodMax*Mfuncx range .... */ \ /* from (-PeriodMax,PeriodMax) to (0,PeriodMax) where HalfPerMax=PeriodMax/2 */ \ \ (*ePWM[ch1]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC1)+ v.HalfPerMax; \ (*ePWM[ch2]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC2)+ v.HalfPerMax; \ (*ePWM[ch3]).CMPA.half.CMPA = _IQmpy(v.HalfPerMax,v.MfuncC3)+ v.HalfPerMax; \
最新发布
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值