题目链接: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暴力搜索的更多相关文章

  1. UVALive 5844 dfs暴力搜索

    题目链接:UVAive 5844 Leet DES:大意是给出两个字符串.第一个字符串里的字符可以由1-k个字符代替.问这两个字符串是不是相等.因为1<=k<=3.而且第一个字符串长度小于 ...

  2. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  3. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  4. 洛谷P1019——单词接龙(DFS暴力搜索)

    https://www.luogu.org/problem/show?pid=1019#sub 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母, ...

  5. [HDU 1427]速度计算24点(DFS暴力搜索)

    主题连接:  pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...

  6. poj 3050 Hopscotch DFS+暴力搜索+set容器

    Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2774 Accepted: 1940 Description ...

  7. ACM 暴力搜索题 题目整理

    UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vec ...

  8. [luogu 1092] 虫食算 (暴力搜索剪枝)

    传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...

  9. HDU 3131 One…Two…Five! (暴力搜索)

    题目链接:pid=3131">HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6= ...

随机推荐

  1. 25个c#知识点

    网站地址:http://m.3y.uu456.com/mbp_56hl91r1rx5uqa87qrzo_1.html

  2. Python3基础 delattr 删除对象的属性

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. VS不显示最近打开的项目

    VS2012不显示最近打开的项目 解决方法, 在"运行"中输入 " gpedit.msc"打开后在"用户配置"-"管理模板&quo ...

  4. 论文笔记—Flattened convolution neural networks for feedforward acceleration

    1. 论文思想 一维滤过器.将三维卷积分解成三个一维卷积.convolution across channels(lateral), vertical and horizontal direction ...

  5. com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼

    起初这样能短暂解决问题,后来发现每次机器重启了就还是有这样的错误,还是要执行SQL,很麻烦: show variables like '%time_zone%'; select now(); set ...

  6. 使用Apache Kylin搭建企业级开源大数据分析平台

    转:http://www.thebigdata.cn/JieJueFangAn/30143.html 我先做一个简单介绍我叫史少锋,我曾经在IBM.eBay做过大数据.云架构的开发,现在是Kylige ...

  7. com.mysql.jdbc.PacketTooBigException,及mysql 设置 max_allow_packet

    本文为博主原创,未经允许不得转载: 在进行批量导入表格数据入库操作时,报了以下错误: 错误分析: mysql根据配置文件会限制server接受的数据包大小.有时候大的插入和更新会受max_allowe ...

  8. LA 3213 古老的密码

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=15& ...

  9. 【bzoj】3224: Tyvj 1728 普通平衡树

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 10097  Solved: 4302[Submit][St ...

  10. Error: Checksum mismatch.

    bogon:bin macname$ brew install go ==> Downloading https://homebrew.bintray.com/bottles-portable- ...