A hard Aoshu Problem HDU - 3699

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


题意概括:题中分别用ABCDE代替0-9这10个数字,每组样例会给你三个字符串,每个字符串的长度小于等于8,这三个字符串可以代表很多种数,但是每个字母只能代表1个数字,前两个数可以经过加减乘除这四种运算,问有多少种情况能通过前两个数得到第三个数。

解题思路:把每个字符串出现过哪些字母都记录一下,然后通过搜索给这些字母赋值(如果这个字符串的长度大于1那么在给这个字符串赋值时第一个字母不能赋值为0),把所出现的每个字母都赋值完成之后,通过输入的字符串找到每个字符串对应的值,然后让前两个经过加减乘除,如果等于第三个字符串代表的数,标记变量+1。

代码:

#include<stdio.h>
#include<string.h>
int a[10],book[20],f[20],l1,l2,l3,num;
char s1[20],s2[20],s3[20];
void DFS(int s)
{
	int i;
	//printf("%d\n",s);
	if(s==5)
	{
		//for(i=0;i<5;i++)
		//	printf("%d ",a[i]);
		//printf("\n");
		int a1=0,b1=0,c1=0;
		int flag=0;
		for(i=0;i<l1;i++)
		{
			if(a[s1[0]-'A']==0&&l1!=1)  //如果把大于一位的字符串的第一字母赋值为0那么这个数不能用
			{
				flag=1;
				break;
			}
				
			a1=a[s1[i]-'A']+a1*10;
		}
		for(i=0;i<l2;i++)
		{
			if(a[s2[0]-'A']==0&&l2!=1)
			{
				flag=1;
				break;
			}
			
			b1=a[s2[i]-'A']+b1*10;
		}
		for(i=0;i<l3;i++)
		{
			if(a[s3[0]-'A']==0&&l3!=1)
			{
				flag=1;
				break;
			}
			
			c1=a[s3[i]-'A']+c1*10;
		}
		if(flag!=1)
		{
				
		if(a1+b1==c1)   //加减乘除运算
		{
			//printf("%d %d %d\n",a1,b1,c1);
			num++;
		}
		if(a1-b1==c1)
		{
			//printf("%d %d %d\n",a1,b1,c1);
			num++;
		}
		if(a1*b1==c1)
		{
			//printf("%d %d %d\n",a1,b1,c1);
			num++;
		}
		if(b1*c1==a1&&b1!=0)  //除法这样写可以避免分母为0的情况
		{
			//printf("%d %d %d\n",a1,b1,c1);
			num++;
		}
		}
		return ;
	}
	if(f[s]==0)  //如果这个字母没有出现过调到下一个字母
		DFS(s+1);
	

	
	
	for(i=0;i<=9;i++)  //遍历0-9这10个数,book数组标记这10个数是否被用过	
	{		   
		if(book[i]==0&&f[s]==1)  //给每个字母赋值
		{
			book[i]=1;
			a[s]=i;
			DFS(s+1);
			book[i]=0;	
		}
	}
}
int main()
{
	int T,i,j,k,l;
	scanf("%d",&T);
	
	while(T--)
	{
		scanf("%s%s%s",&s1,&s2,&s3);
		l1=strlen(s1);
		l2=strlen(s2);
		l3=strlen(s3);
		memset(f,0,sizeof(f));
		for(i=0;i<l1;i++)  //标记哪些字母出现过
		{
			f[s1[i]-'A']=1;
		}
		for(i=0;i<l2;i++)
		{
			f[s2[i]-'A']=1;
		}
		for(i=0;i<l3;i++)
		{
			f[s3[i]-'A']=1;
		}
		for(i=0;i<5;i++)
		{
		//	printf("%d ",f[i]);
		}
	//	printf("\n");
		memset(book,0,sizeof(book));
		memset(a,0,sizeof(a));
		num=0;
		DFS(0);
		printf("%d\n",num);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值