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. vuex+vue-router拦截

    干就完了 项目中经常遇到这样一个场景,用户信息或者进行增删改的一些模块,需要根据用户是否登录,进行路由拦截,直接上代码 在store文件夹下的store.js中存放一个默认登录状态 /* * stor ...

  2. svg了解一下

    工作需求,要用svg动态生成思维导图.我的天,这是我的短板. 但是没办法,需求在这,硬着头皮上吧. 本来想偷懒,看看网上有没有现成的可以copy的,逛了一圈发现没有. 这个过程种发现了D3.Three ...

  3. Dubbo源码分析之ExtensionLoader加载过程解析

    ExtensionLoader加载机制阅读: Dubbo的类加载机制是模仿jdk的spi加载机制:  Jdk的SPI扩展加载机制:约定是当服务的提供者每增加一个接口的实现类时,需要在jar包的META ...

  4. javascript 中数组的创建 添加 与将数组转换成字符串 页面三种提交请求的方式

    创建js数组 var array=new Array(); Java中创建数组 private String[] array=new String[3]; 两个完全不同的,js中是可变长度的 添加内容 ...

  5. iOS 让视图UIView单独显示某一侧的边框线

    iOS 让视图UIView 单独显示某一侧的边框线   有时候需要让view显示某一侧的边框线,这时设置layer的border是达不到效果的.在网上查阅资料发现有一个投机取巧的办法,原理是给view ...

  6. <<学会提问>>第一章学习笔记

    中国应不应该现在取消死刑? 中医是不是伪科学? 读书无用论? 集体主义和团队精神? 欧洲难民危机,你是支持接收难民,还是反对? 欧洲白左是不是幼稚圣母,抑或是右派种族歧视,顽固保守? 如何看待&quo ...

  7. 全盘解决eclipse之maven项目报错

    每次新建maven的web(war包方式)项目时都会报错而且都要手动改,很麻烦 解决:(注意里面的jdk版本换成自己的) 改变maven配置文件   settings.xml 在文件的<prof ...

  8. Spring Boot2.4双数据源的配置

    相较于单数据源,双数据源配置有时候在数据分库的时候可能更加有利 但是在参考诸多博客以及书籍(汪云飞的实战书)的时候,发现对于spring boot1.X是完全没问题的,一旦切换到spring boot ...

  9. python核心编程2 第十一章 练习

    11-2 函数.结合你对练习5-2的解,以便你创建一个带一对相同数字并同时返回它们之和以及产物的结合函数. multiply = lambda x, y: x * y if __name__ == ' ...

  10. 批处理,%~d0 cd %~dp0 代表什么意思

    批处理,%~d0 cd %~dp0 代表什么意思   ~dp0 “d”为Drive的缩写,即为驱动器,磁盘.“p”为Path缩写,即为路径,目录cd是转到这个目录,不过我觉得cd /d %~dp0 还 ...