2019杭电多校第七场 A + B = C(模拟)

#include
using namespace std;
#define ll long long
const int maxn = 3e6+7;
char a[maxn],b[maxn],c[maxn];
int x[5][maxn];
int len ,la,lb,lc;
void debug(){
        for(int k=1;k<=4;k++)
        {
            for(int i=0;i<=len;i++)    printf("%d",x[k][i]);
            cout<<endl;
        }
}
void init(int len){
    for(int k=1;k<=4;k++)
    {
        for(int i=0;i<=len+1;i++)
        x[k][i] =0;
    }
}
void work(){
        for(int i=lc;i<=len;i++)c[i] = '0';
        for(int i=la;i<=len;i++)a[i] = '0';
        for(int i=lb;i<=len;i++)b[i] = '0';
        
        for(int i=0;i<=len;++i)x[1][i] = c[i]-b[i];
        // B 和 C 相同   A xiao 
        for(int i=0;i<len;++i)x[2][i] = c[i]-a[i];
        // A and C same,B xiao
        
        c[1] = c[1] + 10;
        c[0]--;
        x[3][0]=x[4][0] =c[0]- '0';
        for(int i=1;i<=len;i++)
            x[3][i] = c[i]-b[i-1];
        // C-B=1 ,A xiao
        for(int i=1;i<=len;i++)
            x[4][i] = c[i]-a[i-1];
        // C-A = 1 ,B xiao 
        for(int k=1;k<=4;k++){
            for(int i=len;i>0;i--)
            {
                if(x[k][i]<0)
                {
                    x[k][i] +=10;
                    x[k][i-1]--;
                }
                if(x[k][i]>=10)
                {
                    x[k][i]-=10;
                    x[k][i-1]++;
                }
            }
        }
}
void check() {
        int flag = 1,c=0;
        while(x[1][c]==0)c++; 
        for(int i=0;i<la;i++)
        {
            if(x[1][i+c]!=(a[i]-'0'))
            flag =0;    
        }
        if(flag ==1)
        {
            int xa  =len -la-c,xc = len -lc,xb = len-lb;
            printf("%d %d %d\n",xa,xb,xc);
            return ;
        }
        else {
            flag = 1,c=0;
            while(x[3][c]==0)c++; 
            for(int i=0;i<la;i++) 
            if(x[3][i+c]!=(a[i]-'0'))
            flag  = 0;
            if(flag ==1)
            {
                int xa  =len -la-c,xc = len -lc,xb = len-lb-1;
                printf("%d %d %d\n",xa,xb,xc);
            return ;
            }
            else {
                flag = 1,c=0;
                while(x[2][c]==0)c++; 
                for(int i=0;i<lb;i++)
                {
                    if(x[2][i+c]!=(b[i]-'0'))    {
                        flag  = 0;
                    }
                } 
            
                if(flag ==1)
                {        
                    int xb =len -lb-c,xc = len -lc,xa = len-la;
                    printf("%d %d %d\n",xa,xb,xc);
                    return ;
                }
                else {
                flag = 1,c=0;
                while(x[4][c]==0)c++; 
                for(int i=0;i<lb;i++) 
                if(x[4][i+c]!=(b[i]-'0'))
                flag  = 0;
                if(flag ==1)
                {
                    int xb =len-lb-c,xc = len -lc,xa = len-la-1;
                    printf("%d %d %d\n",xa,xb,xc);
                    return ;
                }
            }
            }
        }
        printf("-1\n");
        return ;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s%s",a,b,c);
        lc = strlen(c),lb = strlen(b),la = strlen(a);
        len = lc+la+lb+1;
        init(len);
        work();
        check();
    }
    return 0;
} 

你可能感兴趣的:(模拟)