HDU 3697 A hard Aoshu Problem (搜索)
来源:互联网 发布:大数据o2o概念股龙头 编辑:程序博客网 时间:2024/05/29 17:00
A hard Aoshu Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)Total Submission(s): 1516 Accepted Submission(s): 798
Problem 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.
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.
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
2A A ABCD BCD B
Sample Output
572
队友的搜索代码,很不错,have数组映射。。cur跳开没有的...
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;int a[50], cnt, len1, len2, len3, len, num;bool lin[50], book[15];char num1[15], num2[15], num3[15], str[50];int have[50];void dfs(int cur, int s){ if(s >= num) { ll A, B, C; A = B = C = 0; for(int i = 0; i < len1; i++) A = A*10+a[num1[i]-'A']; for(int i = 0; i < len2; i++) B = B*10+a[num2[i]-'A']; for(int i = 0; i < len3; i++) C = C*10+a[num3[i]-'A']; if(A+B == C) cnt++; if(A-B == C) cnt++; if(A*B == C) cnt++; if(B && A == B*C) cnt++; return ; } while(!have[cur]) cur++; for(int i = 0; i < 10; i++) { if(!book[i]) { if(lin[cur] && !i) continue; book[i] = 1; a[cur] = i; dfs(cur+1, s+1); book[i] = 0; } }}int main(void){ int t; cin >> t; while(t--) { num = 0; scanf(" %s %s %s", num1, num2, num3); memset(have, 0, sizeof(have)); memset(lin, 0, sizeof(lin)); len1 = strlen(num1); len2 = strlen(num2); len3 = strlen(num3); if(len1 > 1) lin[num1[0]-'A'] = 1; if(len2 > 1) lin[num2[0]-'A'] = 1; if(len3 > 1) lin[num3[0]-'A'] = 1; for(int i = 0; i < len1; i++) { if(!have[num1[i]-'A']) { have[num1[i]-'A'] = 1; str[num++] = num1[i]; } } for(int i = 0; i < len2; i++) { if(!have[num2[i]-'A']) { have[num2[i]-'A'] = 1; str[num++] = num2[i]; } } for(int i = 0; i < len3; i++) { if(!have[num3[i]-'A']) { have[num3[i]-'A'] = 1; str[num++] = num3[i]; } } if(num > 10) puts("0"); else { cnt = 0; memset(book, 0, sizeof(book)); dfs(0, 0); printf("%d\n", cnt); } } return 0;}
0 0
- HDU 3697 A hard Aoshu Problem (搜索)
- HDU 3699 A hard Aoshu Problem (暴力搜索)
- A hard Aoshu Problem HDU
- A hard Aoshu Problem HDU
- A hard Aoshu Problem HDU
- HDU 3699(J) ——A hard Aoshu Problem(DFS暴力搜索)
- HDU 4403 A very hard Aoshu problem (DFS暴力)
- HDU 4403 A very hard Aoshu problem(DFS+暴力)
- HDU 4403 A very hard Aoshu problem(双向dfs)
- hdu 4403 A very hard Aoshu problem(dfs)
- HDU 4403 A very hard Aoshu problem (DFS+状压)
- HDU 3699 A hard Aoshu Problem
- HDU 4403 A very hard Aoshu problem
- hdu 4403 A very hard Aoshu problem
- HDU:4403 A very hard Aoshu problem
- HDU 4403 A very hard Aoshu problem
- HDU 4403 A very hard Aoshu problem
- HDU 4403(A very hard Aoshu problem)
- LA 3708
- [JZOJ5088]最小边权和
- phpmysql登陆报错 #1862
- nn优化研究(二)
- Beam学习笔记(3):Flink Streaming Pipeline Translator
- HDU 3697 A hard Aoshu Problem (搜索)
- 《Redis实战》读后感
- Windows下安装TensorFlow
- 浅析name==null, "".equals(name)和name.length==0三者的区别
- 简介Opencv在Python中的使用
- c++单元测试框架Catch
- 顺序表(约瑟夫环)
- Kotlin初探
- poj 1141Brackets Sequence