1. 程式人生 > >A hard Aoshu Problem HDU

A hard Aoshu Problem HDU

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. 

InputThe 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. 

OutputFor 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;
}