UVALive - 5107 - A hard Aoshu Problem

题目链接:https://vjudge.net/problem/UVALive-5107

题目大意:用ABCDE代表不同的数字,给出形如ABBDE___ABCCC = BDBDE的东西;

空格里面可以填入+-*/的运算符,给字母赋予不同的值,问有多少种情况使得

等式成立。

题目分析:

可以直接用大模拟+暴力求解,注意对于重复情况的判重。

给出代码:

  1 #include <iostream>
  2 #include <set>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <string>
  6 using namespace std;
  7 string a;
  8 string b;
  9 string c;
 10 set<int> mark;
 11 set<int> marks;
 12 int num[30];
 13 int main()
 14 {
 15     int N;
 16     cin>>N;
 17     while(N--)
 18     {
 19         cin>>a>>b>>c;
 20         marks.clear();
 21         int t1=a.length();
 22         int t2=b.length();
 23         int t3=c.length();
 24         for(int i=0; i<t1; i++)
 25         {
 26             int t=a[i]-‘A‘;
 27             marks.insert(t);
 28         }
 29         for(int i=0; i<t2; i++)
 30         {
 31             int t=b[i]-‘A‘;
 32             marks.insert(t);
 33         }
 34         for(int i=0; i<t3; i++)
 35         {
 36             int t=c[i]-‘A‘;
 37             marks.insert(t);
 38         }
 39         mark.clear();
 40        // marks.clear();
 41         int cnt=0;
 42         for(int i=0; i<=9; i++)
 43         {
 44             for(int j=0; j<=9; j++)
 45             {
 46                 if(i==j)
 47                     continue;
 48                 for(int k=0; k<=9; k++)
 49                 {
 50                     if(k==i||k==j)
 51                         continue;
 52                     for(int m=0; m<=9; m++)
 53                     {
 54                         if(m==i||m==j||m==k)
 55                             continue;
 56                         for(int n=0; n<=9; n++)
 57                         {
 58                             if(n==i||n==j||n==m||n==k||n==m)
 59                                 continue;
 60                             int ttt=marks.size();
 61                             //int sum=0;
 62                             int ans=0;
 63                             for(int y=0;y<ttt;y++)
 64                             {
 65                                 if(y==0)
 66                                    ans=ans*10+i;
 67                                 if(y==1)
 68                                     ans=ans*10+j;
 69                                 if(y==2)
 70                                     ans=ans*10+k;
 71                                 if(y==3)
 72                                     ans=ans*10+m;
 73                                 if(y==4)
 74                                     ans=ans*10+n;
 75                             }
 76                             if(mark.count(ans)==1)
 77                                 continue;
 78                             mark.insert(ans);
 79                             set<int>::iterator ite=marks.begin();
 80                             for(int ans=0; ite!=marks.end(); ite++,ans++)
 81                             {
 82                                 int t=*ite;
 83                                 if(ans==0)
 84                                     num[t]=i;
 85                                 if(ans==1)
 86                                     num[t]=j;
 87                                 if(ans==2)
 88                                     num[t]=k;
 89                                 if(ans==3)
 90                                     num[t]=m;
 91                                 if(ans==4)
 92                                     num[t]=n;
 93                             }
 94                             int flag=a[0]-‘A‘;
 95                             if(num[flag]==0&&t1>1)
 96                                 continue;
 97                             flag=b[0]-‘A‘;
 98                             if(num[flag]==0&&t2>1)
 99                                 continue;
100                             flag=c[0]-‘A‘;
101                             if(num[flag]==0&&t3>1)
102                                 continue;
103                             long long int num1=0,num2=0,num3=0;
104                             for(int i=0; i<t1; i++)
105                             {
106                                 int tt=a[i]-‘A‘;
107                                 num1=num1*10+num[tt];
108                             }
109                             for(int i=0; i<t2; i++)
110                             {
111                                 int tt=b[i]-‘A‘;
112                                 num2=num2*10+num[tt];
113                             }
114                             for(int i=0; i<t3; i++)
115                             {
116                                 int tt=c[i]-‘A‘;
117                                 num3=num3*10+num[tt];
118                             }
119                             if(num1+num2==num3)
120                                 cnt++;
121                             if(num1-num2==num3)
122                                 cnt++;
123                             if(num1*num2==num3)
124                                 cnt++;
125                             if(num2!=0&&num1/num2==num3&&num1%num2==0)
126                                 cnt++;
127                         }
128                     }
129                 }
130             }
131         }
132         cout<<cnt<<endl;
133     }
134     return 0;
135 }

时间: 2024-10-26 17:40:28

UVALive - 5107 - A hard Aoshu Problem的相关文章

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

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

hdu 4403 A very hard Aoshu problem

hdu 4403 A very hard Aoshu problem 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 DFS 这几天集训,一天也就写个4题,被虐哭QAQ.回寝室后游少说解搜索就大胆搜,最后剪个枝就好了Orz,然后我就尝试解这题(剪枝要风骚).我先枚举等号的位置equ,然后搜索加号add的位置,然后主要的剪枝:如果等号左边运算后小于等号右边各个位上的数的和,那么后面的肯定不满足条件,右边同理.最后要小心爆int,这里吃了很多W

HDU 4403 A very hard Aoshu problem(DFS)

A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a pro

HDU4403 A very hard Aoshu problem DFS

A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than o

hdu3699 A hard Aoshu Problem 暴搜

http://acm.hdu.edu.cn/showproblem.php?pid=3699 A hard Aoshu Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 490 Problem Description Math Olympiad is calle

hdu 3699 10 福州 现场 J - A hard Aoshu 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

[ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)

A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a pro

UVALive 5107 dfs暴力搜索

题目链接:A hard Aoshu Problem DES:给三个字符串,包含的字符是A-E范围内的.长度都不超过8.每个字符可以而且只可以匹配一个数字.两个字符不能匹配相同的数字.前两个式子之间可以有+-*/四中关系.然后=第三个式子.问.会有多少种关系式. #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char s[3][10]; int a[10], b[1

A very hard Aoshu problem(dfs或者数位)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 A very hard Aoshu problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1080    Accepted Submission(s): 742 Problem Description Aoshu is ver