标签:des blog io ar os sp for div on
Description
Input
Output
Sample Input
Sample Output
#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\n",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?‘\n‘:‘ ‘); 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\n",ans); } return 0; }
hdu 3699 10 福州 现场 J - A hard Aoshu Problem
标签:des blog io ar os sp for div on
原文地址:http://www.cnblogs.com/xuesu/p/4087966.html