HDU 3699 A hard Aoshu Problem(暴力枚举)(2010 Asia Fuzhou Regional Contest)
Description
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
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
题目大意:给一个最多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)的更多相关文章
- HDU 3697 Selecting courses(贪心+暴力)(2010 Asia Fuzhou Regional Contest)
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- 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 ...
- 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 ...
- 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 ...
- HDU 3699 A hard Aoshu Problem (暴力搜索)
题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(同样的字符串进行同样的数字替换), 替换后的三个数进行四则运算要满足左边等于右边.求有几种解法. Sample Input 2 ...
- 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 ...
- 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 ...
- HDU 3721 Building Roads (2010 Asia Tianjin Regional Contest) - from lanshui_Yang
感慨一下,区域赛的题目果然很费脑啊!!不过确实是一道不可多得的好题目!! 题目大意:给你一棵有n个节点的树,让你移动树中一条边的位置,即将这条边连接到任意两个顶点(边的大小不变),要求使得到的新树的直 ...
- HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)
Problem Description In this problem, you are given several strings that contain only digits from '0' ...
随机推荐
- mycat的安装及配置文件应用
table:逻辑一 mycat的安装 1 基于jdk运行 2 获取安装包 3 解压 tar -xf Mycat***.tar.gz 4 测试运行 mycat的根目录中bin保存了mycat的核心命令文 ...
- 对UIImageView+WebCache的封装
UIImageView+SDWebImage.h #import <UIKit/UIKit.h> typedef void(^DownloadImageSuccessBlock)(UIIm ...
- Linux上往mysql中导入SQL文件
1.首先连上你的服务器 2.进入你安装好的数据库 mysql -uroot -p****, 3.执行命令 source /xxx/xxx.sql 后面是文件的路径 4.如果你要是在本地有数据库管理 ...
- #leetcode刷题之路3-无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc" ...
- 【模板】概率dp
有n个投资事件,和一个成功概率最低接受值rate.每个投资的价值是c[i],成功概率是p[i](浮点数). 在保证成功概率≥rate的情况下,使价值最大化. #include<bits/stdc ...
- shardedJedisPool工具类
这里使用的是ShardedJedisPool,而不是RedisTemplate 1.配置文件 <?xml version="1.0" encoding="UTF-8 ...
- JAVA | 学生选课系统
这里使用JAVA语言编写的简易的学生选课系统,展现的都是这个系统核心代码. 其中有不足欢迎批评和指正! 链接数据库的代码 package connection;//连接数据库student impor ...
- LeetCode 相交链表
基本思路 先计算出两个链表的长度 O(n) 将长的一个链表的指示指针移动到和短链表相同长度 O(n) 两个链表指示指针同时向前移动,直到二者相同或者NULL 代码实现 /** * Definition ...
- JetBrains PyCharm 2017.3注册码
JetBrains PyCharm 2017.3注册码 (1)在激活界面的License server输入:http://idea.liyang.io:或者:点击help→Register→Licen ...
- CodeTimer 代码性能计数器
收集整理老赵 的”CodeTimer“. 用于测试代码性能.详见可参考 老赵原文 代码如下: using System; using System.Diagnostics; using System. ...