A hard Aoshu Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 1550 Accepted Submission(s): 813
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
2 A A A BCD BCD B
Sample Output
5 72
Source
Recommend
题目大意:
给三个字符串,要求给字符赋值加上运算符使等式成立,求能够成立的种数。
思路:
看到情况并不太多,就一套循环硬怼,代码7000+,果然WA了。想到dfs,但不会这种骚操作(TAT),赛后补出来的。注意
前导0 的情况。
附上AC代码:
#include<iostream>
#include<string>
#include<cstring>
#include<map>
using namespace std;
int n,cnt,ans;
string s1,s2,s3;
char alpha[10];
int vis[10],flag[10];
map<char,int>_map;
void scan(){
for(int i=0;i<s1.length();i++){
if(!vis[s1[i]-'A']){
vis[s1[i]-'A']=1;
alpha[cnt++]=s1[i];
}
}
for(int i=0;i<s2.length();i++){
if(!vis[s2[i]-'A']){
vis[s2[i]-'A']=1;
alpha[cnt++]=s2[i];
}
}
for(int i=0;i<s3.length();i++){
if(!vis[s3[i]-'A']){
vis[s3[i]-'A']=1;
alpha[cnt++]=s3[i];
}
}
}
void judge(){
int t1=0,t2=0,t3=0;
for(int i=0;i<s1.length();i++)t1=t1*10+_map[s1[i]];
for(int i=0;i<s2.length();i++)t2=t2*10+_map[s2[i]];
for(int i=0;i<s3.length();i++)t3=t3*10+_map[s3[i]];
if(t1+t2==t3)ans++;
if(t1-t2==t3)ans++;
if(t1*t2==t3)ans++;
if(t2&&t3*t2==t1)ans++;
}
void dfs(int num){
if(num>=cnt){
judge();
return;
}
for(int i=0;i<10;i++){
if(i==0&&vis[alpha[num]-'A']==1)continue;
if(!flag[i]){
flag[i]=1;
_map[alpha[num]]=i;
dfs(num+1);
flag[i]=0;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
while(n--){
cnt=0;ans=0;
_map.clear();
memset(vis,0,sizeof(vis));
cin>>s1>>s2>>s3;
scan();
memset(vis,0,sizeof(vis));
if(s1.length()>1)vis[s1[0]-'A']=1;
if(s2.length()>1)vis[s2[0]-'A']=1;
if(s3.length()>1)vis[s3[0]-'A']=1;
dfs(0);
cout<<ans<<endl;
}
return 0;
}