hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0
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
Sample Input
A A A
BCD BCD B
Sample Output
72
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int per[5],temper[5];
char left[10],right[10],equ[10];
int index[5];
int del(char ch){
return index[ch-'A'];
}
int getnum(char* str){
int ans=0;
for(int i=0;str[i];i++){
ans*=10;
ans+=per[del(str[i])];
}
return ans;
}
void cpy(int *a,int *b,int len){
for(int i=0;i<len;i++)a[i]=b[i];
}
void pr(int lnum,int rnum,int equ,int op){
/*printf("%d ",lnum);
if(op==0)putchar('+');
else if(op==1)putchar('-');
else if(op==2)putchar('*');
else if(op==3)putchar('/');
printf(" %d = %d\n",rnum,equ);*/
}
int check(){
int ans=0;
int lnum=getnum(left),rnum=getnum(right),eqnum=getnum(equ);
if(lnum+rnum==eqnum){pr(lnum,rnum,eqnum,0);ans++;}
if(lnum-rnum==eqnum){pr(lnum,rnum,eqnum,1);ans++;}
if(lnum*rnum==eqnum){pr(lnum,rnum,eqnum,2);ans++;}
if(rnum!=0&&lnum/rnum==eqnum&&lnum%rnum==0){pr(lnum,rnum,eqnum,3);ans++;}
return ans;
}
int dfs(int s,int ind,int rlen,int llen,int elen,int allen){
int ans=0;
per[s]=ind;
if(s==allen-1){
cpy(temper,per,allen);//注意记录原值,否则因为顺序是反的,就不能直接修改取的数字
if(!((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1)))
ans+=check();
while(next_permutation(per,per+allen)){
// for(int i=0;i<allen;i++)printf("%d%c",per[i],i==allen-1?'\n':' ');
if((per[del(left[0])]==0&&llen>1)||(per[del(right[0])]==0&&rlen>1)||(per[del(equ[0])]==0&&elen>1))continue;
ans+=check();
}
cpy(per,temper,allen);
}
else {
for(int i=ind+1;i<10;i++)ans+=dfs(s+1,i,llen,rlen,elen,allen);
}
return ans;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s%s",left,right,equ);
int ans=0;
for(int i=0;i<5;i++)per[i]=i;
int llen=strlen(left),rlen=strlen(right),elen=strlen(equ);
memset(index,-1,sizeof(index));
int allen=0;
for(int i=0;left[i];i++)if(index[left[i]-'A']==-1)index[left[i]-'A']=allen++;
for(int i=0;right[i];i++)if(index[right[i]-'A']==-1)index[right[i]-'A']=allen++;
for(int i=0;equ[i];i++)if(index[equ[i]-'A']==-1)index[equ[i]-'A']=allen++;
for(int i=0;i<10;i++)ans+=dfs(0,i,llen,rlen,elen,allen);
printf("%d\n",ans);
}
return 0;
}
hdu 3699 10 福州 现场 J - A hard Aoshu Problem 暴力 难度:0的更多相关文章
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
H - National Day Parade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- hdu 3697 10 福州 现场 H - Selecting courses 贪心 难度:0
Description A new Semester is coming and students are troubling for selecting courses. Students ...
- hdu 3694 10 福州 现场 E - Fermat Point in Quadrangle 费马点 计算几何 难度:1
In geometry the Fermat point of a triangle, also called Torricelli point, is a point such that the t ...
- hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0
Description “Farm Game” is one of the most popular games in online community. In the community each ...
- hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥 难度:1
C - To Be an Dream Architect Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0
C - To Be an Dream Architect Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu 3685 10 杭州 现场 F - Rotational Painting 重心 计算几何 难度:1
F - Rotational Painting Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- hdu 3262 09 宁波 现场 C - Seat taking up is tough 暴力 难度:0
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
随机推荐
- Apache 2.4 编码GB2312中文乱码的问题
今天部署了一个项目,代码和数据库都是gb2312的,本地和服务器都是apache2.4的版本,本地编码没问题,response的content-type是空的.按html的mete解析的,查看源码也是 ...
- nginx + ngx_lua安装测试
nginx lua模块淘宝开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力.nginx以高并发而知名,lua脚本轻便,两者的搭配堪称完美. ...
- 开发一个根据xml创建代理类的小框架
github地址 https://github.com/1367356/GradleTestUseSubModule/tree/master/CreateMyFrameWork 1:定义一些规则
- idea一个类中,各个修饰符的符号表示
1: 2:
- Spark Core (一) 什么是RDD的Transformation和Action以及Dependency(转载)
1. Spark的RDD RDD(Resilient Distributed Datasets),弹性分布式数据集,是对分布式数据集的一种抽象. RDD所具备5个主要特性: 一组分区列表 计算每一个数 ...
- 用Python实现的数据结构与算法:堆栈
一.概述 堆栈(Stack)是一种后进先出(LIFO)的线性数据结构,对堆栈的插入和删除操作都只能在栈顶(top)进行. 二.ADT 堆栈ADT(抽象数据类型)一般提供以下接口: Stack() 创建 ...
- 4.1 Routing -- Introduction
一.Routing 1. 当用户与应用程序交互时,它会经过很多状态.Ember.js为你提供了有用的工具去管理它的状态和扩展你的app. 2. 要理解为什么这是重要的,假设我们正在编写一个Web应用程 ...
- SpringData关键字查询练习
我们在上一节知道SpringData关键字有很多,我就拿几个例子练练手 1.需求我们查询lastName like sun and id < ?的查询 package com.fxr.sprin ...
- cocos进阶教程(5)CC_CALLBACK_X系列的使用技巧
CC_CALLBACK_1,CC_CALLBACK_2,CC_CALLBACK_3 这些都是std::bind的宏,数字1,2,3主要表示要占位的数量,也是将来传递参数的数量. // new call ...
- cocos代码研究(23)Widget子类ScrollView学习笔记
基础理论 一个能够被用户触摸滚动的一个层次型布局容器视图,允许其尺寸大于屏幕显示的物理尺寸,其内部维护有一个布局用于水平的或垂直的存放子节点.继承自 Layout,被 ListView 继承. 代码实 ...