UVALive 5107 dfs暴力搜索
题目链接:A hard Aoshu Problem
DES:给三个字符串,包含的字符是A-E范围内的。长度都不超过8。每个字符可以而且只可以匹配一个数字。两个字符不能匹配相同的数字。前两个式子之间可以有+-*/四中关系。然后=第三个式子。问。会有多少种关系式。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; char s[][];
int a[], b[], c[]; // 字符转换成数字后存储。
int num[]; // 每个字符匹配给哪个数字。
int len[];
int vis1[], vis2[]; //vis1[]保存下标对应字符是否出现。(A B C D E->0 1 2 3 4)。vis2[]标记枚举数字时标记是否已经出现过、
int ans;
int aa, bb, cc; //三个字符串对应的数字、 int check() // 不可有前导0的检查。
{
if (len[] > && num[a[]] == ) return ;
if (len[] > && num[b[]] == ) return ;
if (len[] > && num[c[]] == ) return ;
return ;
} void getnum() //根据每次枚举把三个字符串换成数字。
{
aa = , bb = , cc = ;
for (int i=; i<len[]; ++i)
{
aa = aa * + num[a[i]];
}
for (int i=; i<len[]; ++i)
{
bb = bb * + num[b[i]];
}
for (int i=; i<len[]; ++i)
{
cc = cc * + num[c[i]];
}
} int gettot() //看有几种运算关系。
{
int temp = ;
if (aa + bb == cc) temp++;
if (aa - bb == cc) temp++;
if (aa * bb == cc) temp++;
if (bb * cc == aa && bb != ) temp++;
return temp;
} void dfs(int x) // 暴力枚举每一个字符匹配十个数字的情况。
{
if (x == ) // x表示当前枚举第几个字母。如果枚举完就可以看当前匹配有几种关系式。
{
if (check())
{
getnum();
int cnt = gettot();
ans += cnt;
return;
}
} else if (vis1[x]) // 如果x对应的字符出现在三个字符串里、
{
for (int i=; i<=; ++i) // 开始枚举。
{
if (vis2[i] == )
{
num[x] = i;
vis2[i] = ;
dfs(x+);
vis2[i] = ; // 回溯
}
}
}
else dfs(x+); // 否则就继续匹配下一个字符。
} int main()
{
int t;
cin >> t;
while(t--)
{
ans = ;
memset(vis1, , sizeof(vis1));
memset(vis2, , sizeof(vis2));
// 输入处理
for (int i=; i<; ++i)
{
cin >> s[i];
len[i] = strlen(s[i]);
for (int j=; j<len[i]; ++j)
{
int temp = s[i][j] - 'A';
if (i == ) a[j] = temp;
if (i == ) b[j] = temp;
if (i == ) c[j] = temp;
vis1[temp]++;
}
}
dfs();
cout << ans << endl;
}
return ;
}
UVALive 5107 dfs暴力搜索的更多相关文章
- UVALive 5844 dfs暴力搜索
题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- 洛谷P1019——单词接龙(DFS暴力搜索)
https://www.luogu.org/problem/show?pid=1019#sub 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母, ...
- [HDU 1427]速度计算24点(DFS暴力搜索)
主题连接: pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...
- poj 3050 Hopscotch DFS+暴力搜索+set容器
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...
- ACM 暴力搜索题 题目整理
UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...
- [luogu 1092] 虫食算 (暴力搜索剪枝)
传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...
- HDU 3131 One…Two…Five! (暴力搜索)
题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...
随机推荐
- 20145307陈俊达《网络对抗》Exp5 MSF基础应用
20145307陈俊达<网络对抗>Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,各式各样的漏洞在 ...
- BZOJ 1503 郁闷的出纳员(splay)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...
- C# MVC框架初学者
推荐网站:http://blog.csdn.net/zhuyu19911016520/article/category/6318590
- C# 计算传入的时间距离今天的时间差
/// <summary> /// 计算传入的时间距离今天的时间差 /// </summary> /// <param name="dt">&l ...
- Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】
本文转载自:https://blog.csdn.net/LoseInVain/article/details/78763303 TensorFlow支持广播机制(Broadcast),可以广播元素间操 ...
- input标签type=button时,如何禁用和开启按钮
本文为博主原创,未经允许不得转载: <input id="exportCameraButton" type="button" class="bt ...
- python正则表达式re模块详细介绍--转载
本模块提供了和Perl里的正则表达式类似的功能,不关是正则表达式本身还是被搜索的字符串,都可以是Unicode字符,这点不用担心,python会处理地和Ascii字符一样漂亮. 正则表达式使用反斜杆( ...
- python 元组列表合并
#create a tuple l = [(,), (,), (,)] print(list(zip(*l)))
- 常见dos命令汇总
常用的内部命令有md.cd.rd.dir.path.copy.type.edit.ren.del.cls.ver.date.time.prompt.常用的外部命令有deltree.format.dis ...
- Blue_Flke团队项目设计完善&编码测试
任务1:文档<软件设计方案说明书>github地址:https://github.com/13993013291/ruanjianguigexuqiu 任务2:项目集成开发环境:eclip ...