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.
 

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.

题目大意:给一个最多5个字母的式子,要求用不同的数字替换这些字母,中间填一个符号,问有多少种填法能使等式成立。

思路:暴力枚举。

PS:易错点:不能有前导0,。可以有单个0。一个数字只能出现一次。除法不能用除号(整除的问题)。做除法前要判断被零除的问题(移项了不代表不用判断)。有些字母可能不在式子里出现。

代码(15MS):

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ; char s1[MAXN], s2[MAXN], s3[MAXN];
int trans[MAXN];
bool exist[MAXN], use[MAXN];
int ans; int s_to_i(char *s) {
if(trans[s[] - 'A'] == && s[]) return -;
int ret = ;
for(int i = ; s[i]; ++i)
ret = ret * + trans[s[i] - 'A'];
return ret;
} void dfs(int dep) {
if(dep == ) {
int a1 = s_to_i(s1), a2 = s_to_i(s2), a3 = s_to_i(s3);
if(a1 == - || a2 == - || a3 == -) return ;
if(a1 + a2 == a3) ++ans;
if(a1 - a2 == a3) ++ans;
if(a1 * a2 == a3) ++ans;
if(a2 && a1 == a2 * a3) ++ans;
return ;
}
if(!exist[dep]) {
dfs(dep + );
return ;
}
for(int i = ; i <= ; ++i) {
if(use[i]) continue;
trans[dep] = i;
use[i] = true;
dfs(dep + );
use[i] = false;
}
} void check(char *s) {
for(int i = ; s[i]; ++i)
exist[s[i] - 'A'] = true;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%s%s%s", s1, s2, s3);
memset(exist, , sizeof(exist));
check(s1), check(s2), check(s3);
ans = ;
dfs();
printf("%d\n", ans);
}
}

HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)的更多相关文章

  1. HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)

    Description     A new Semester is coming and students are troubling for selecting courses. Students ...

  2. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  3. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

  4. HDU 3696 Farm Game(拓扑+DP)(2010 Asia Fuzhou Regional Contest)

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

  5. HDU 3699 A hard Aoshu Problem (暴力搜索)

    题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(同样的字符串进行同样的数字替换), 替换后的三个数进行四则运算要满足左边等于右边.求有几种解法. Sample Input 2 ...

  6. HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)

    Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...

  7. HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)

    Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...

  8. HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang

    感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...

  9. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

随机推荐

  1. git使用过程的问题与解决办法

    一.什么是Git Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区Index / Stage:暂存区Repository:仓库区(或本地仓库)Remote ...

  2. java.util.ArrayList,java.util.LinkedList,java.util.Vector的区别,使用场合.

    下图是Collection的类继承图 从图中可以看出:Vector.ArrayList.LinkedList这三者都实现了List 接口.所有使用方式也很相似,主要区别在于实现方式的不同,所以对不同的 ...

  3. 通过swagger下载的文件乱码解决方法,求解

    这里的数据显示 点击Download Templates下载之后是显示一个response流都不是一个xlsx文件 这个是由什么原因造成的,求解?

  4. .Net Core On Liunx 环境搭建之安装Mysql8

    上一篇文章安装了MongoDB紧接上一篇随笔,来进行MySql数据库的安装 服务器环境:阿里云云服务器,操作系统CentOS.7-x64 注:文章的图片是我从我的CSDN博客中直接粘贴过来的,不是扒的 ...

  5. php成绩排序

    $arr = ['12','12','23']; $arr = $arr; $arr1=$arr; rsort($arr1); $c=[]; foreach ( $arr as $k=>$v){ ...

  6. django之单表查询

    一.创建表 1.创建模型: 创建名为book的app,在book下的models.py中创建模型: from django.db import models # Create your models ...

  7. 006---hashlib模块

    hashlib模块 HASH 一般翻译成散列,也可以叫哈希. 把任意长度的输入通过散列算法变换成固定的长度. 该转换是一种压缩映射 MD5 输入任意长度的信息,经过处理.输出为128位的信息(数字指纹 ...

  8. Black And White (DFS 训练题)

    G - Black And White ================================================================================ ...

  9. 38-JWT 设计解析及定制

    可去官网下载Security项目查看源码 只需修改 AddJwtBearer中的行为即可 public void ConfigureServices(IServiceCollection servic ...

  10. Android开发——Android手机屏幕适配方案总结

    )密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...