• hdu 3699 10 福州 现场 J


    Description

    Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem: 


    ABBDE __ ABCCC = BDBDE 

    In the equation above, a letter stands for a digit(0 � 9), and different letters stands for different digits. You can fill the blank with ‘+’, ‘-‘ , ‘×’ or ‘÷’. 

    How to make the equation right? Here is a solution: 


    12245 + 12000 = 24245 

    In that solution, A = 1, B = 2, C = 0, D = 4, E = 5, and ‘+’ is filled in the blank. 

    When I was a kid, finding a solution is OK. But now, my daughter’s teacher tells her to find all solutions. That’s terrible. I doubt whether her teacher really knows how many solutions are there. So please write a program for me to solve this kind of problems. 

     

    Input

    The first line of the input is an integer T( T <= 20) indicating the number of test cases. 

    Each test case is a line which is in the format below: 


    s1 s2 s3 

    s1, s2 and s3 are all strings which are made up of capital letters. Those capital letters only include ‘A’,’B’,’C’,’D’ and ‘E’, so forget about ‘F’ to ‘Z’. The length of s1,s2 or s3 is no more than 8. 

    When you put a ‘=’ between s2 and s3, and put a operator( ‘+’,’-‘, ‘×’ or ‘÷’.) between s1 and s2, and replace every capital letter with a digit, you get a equation. 

    You should figure out the number of solutions making the equation right. 

    Please note that same letters must be replaced by same digits, and different letters must be replaced by different digits. If a number in the equation is more than one digit, it must not have leading zero. 

     

    Output

    For each test case, print an integer in a line. It represents the number of solutions. 
     

    Sample Input

    2 A A A BCD BCD B
     

    Sample Output

    5 72
     
    暴力
    卡题原因:全排列过后数字是完全打乱的,这时候直接选取新的组合是错误的,必须恢复原来的顺序
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int per[5],temper[5];
    char left[10],right[10],equ[10];
    int index[5];
    int del(char ch){
        return index[ch-'A'];
    }
    int getnum(char* str){
        int ans=0;
        for(int i=0;str[i];i++){
            ans*=10;
            ans+=per[del(str[i])];
        }
        return ans;
    }
    void cpy(int *a,int *b,int len){
        for(int i=0;i<len;i++)a[i]=b[i];
    }
    void pr(int lnum,int rnum,int equ,int op){
        /*printf("%d ",lnum);
        if(op==0)putchar('+');
        else if(op==1)putchar('-');
        else if(op==2)putchar('*');
        else if(op==3)putchar('/');
        printf(" %d = %d
    ",rnum,equ);*/
    }
    int check(){
        int ans=0;
        int lnum=getnum(left),rnum=getnum(right),eqnum=getnum(equ);
        if(lnum+rnum==eqnum){pr(lnum,rnum,eqnum,0);ans++;}
        if(lnum-rnum==eqnum){pr(lnum,rnum,eqnum,1);ans++;}
        if(lnum*rnum==eqnum){pr(lnum,rnum,eqnum,2);ans++;}
        if(rnum!=0&&lnum/rnum==eqnum&&lnum%rnum==0){pr(lnum,rnum,eqnum,3);ans++;}
        return ans;
    }
    int dfs(int s,int ind,int rlen,int llen,int elen,int allen){
        int ans=0;
        per[s]=ind;
        if(s==allen-1){
            cpy(temper,per,allen);//注意记录原值,否则因为顺序是反的,就不能直接修改取的数字
            if(!((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1)))
            ans+=check();
            while(next_permutation(per,per+allen)){
               // for(int i=0;i<allen;i++)printf("%d%c",per[i],i==allen-1?'
    ':' ');
                if((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1))continue;
                ans+=check();
            }
            cpy(per,temper,allen);
        }
        else {
            for(int i=ind+1;i<10;i++)ans+=dfs(s+1,i,llen,rlen,elen,allen);
        }
        return ans;
    }
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%s%s%s",left,right,equ);
            int ans=0;
            for(int i=0;i<5;i++)per[i]=i;
            int llen=strlen(left),rlen=strlen(right),elen=strlen(equ);
            memset(index,-1,sizeof(index));
            int allen=0;
            for(int i=0;left[i];i++)if(index[left[i]-'A']==-1)index[left[i]-'A']=allen++;
            for(int i=0;right[i];i++)if(index[right[i]-'A']==-1)index[right[i]-'A']=allen++;
            for(int i=0;equ[i];i++)if(index[equ[i]-'A']==-1)index[equ[i]-'A']=allen++;
            for(int i=0;i<10;i++)ans+=dfs(0,i,llen,rlen,elen,allen);
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    123457123457#0#-----com.threeapp.renzhepaoku01----儿童跑酷游戏(忍者版)
    123456123456#0#-----com.threeapp.xiongMaoPaoPao01----熊猫跑酷01
    123456123456#0#-----com.threeapp.JiQiRenDaZhan01----机器人大战恐龙
    HashMap的工作原理
    Java 虚拟机
    分布式之缓存系统
    分库分表之后,id 主键如何处理?
    如何设计可以动态扩容缩容的分库分表方案?
    为什么要分库分表?用过哪些分库分表中间件?不同的分库分表中间件都有什么优点和缺点?
    如何实现 MySQL 的读写分离?MySQL 主从复制原理的是啥?如何解决 MySQL 主从同步的延时问题?
  • 原文地址:https://www.cnblogs.com/xuesu/p/4087966.html
Copyright © 2020-2023  润新知