HDU 3699--DFS+模拟
来源:互联网 发布:视频编辑软件 慢动作 编辑:程序博客网 时间:2024/06/02 22:48
题意:
给三个字符串s1,s2,s3,三个字符串的每一个字符都代表着一个数字(0~9),问s1 经过和s2进行加减乘除得到s3,问一共有多少种这
样的情况。
输入:
2A A ABCD BCD B
输出:
572
分析:
直接暴力搜索,将字符串转换成数字,(做法:每次乘10加位数)当然要用 long long 才能存的下,然后枚举加减乘除四种运算,只
要满足要求的,ans++;之后dfs遍历将0到9的数字依次填入字符串组成的数字中,注意填过的数字注意要标记。具体实现看代码。
代码:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#include <map>using namespace std;char s1[10],s2[10],s3[10];long long ans=0;map<int,int>vis;int val[5],num[5];void solve(){ long long a=0,b=0,c=0; for(int i=0;s1[i]!=0;i++) { int x=s1[i]-'A'; if(!val[x]&&!i&&strlen(s1)>1) return; a=a*10+val[x]; } for(int i=0;s2[i]!=0;i++) { int x=s2[i]-'A'; if(!val[x]&&!i&&strlen(s2)>1) return; b=b*10+val[x]; } for(int i=0;s3[i]!=0;i++) { int x=s3[i]-'A'; if(!val[x]&&!i&&strlen(s3)>1) return; c=c*10+val[x]; } if(a+b==c) ans++; if(a-b==c) ans++; if(a*b==c) ans++; if(b&&a%b==0) { if(a/b==c) ans++; }}void dfs(int x){ if(x==5) //5个字母 { solve(); return; } if(num[x]==0) dfs(x+1); else { for(int i=0;i<=9;i++) { if(vis[i]) //循环 continue; val[x]=i; vis[i]=1; dfs(x+1); vis[i]=0; } }}int main(){ int T; scanf("%d",&T); while(T--) { ans=0; vis.clear(); memset(num,0,sizeof(num)); scanf("%s%s%s",s1,s2,s3); for(int i=0;s1[i]!=0;i++) num[s1[i]-'A']=1; for(int i=0;s2[i]!=0;i++) num[s2[i]-'A']=1; for(int i=0;s3[i]!=0;i++) num[s3[i]-'A']=1; dfs(0); printf("%I64d\n",ans); } return 0;}
0 0
- HDU 3699--DFS+模拟
- HDU 5438 Ponds dfs模拟
- hdu 4431 Mahjong (模拟,枚举+dfs)
- hdu 4431 Mahjong(dfs+模拟)
- hdu 2212 DFS ---- 算法思考题(模拟)
- HDU 1112 The Proper Key DFS+模拟
- hdu 4272 LianLianKan(模拟dfs+map)
- HDU 5927 Auxiliary Set (DFS+模拟)
- hdu 4801 Pocket Cube(dfs+模拟)
- HDU 3887 Counting Offspring 树状数组 + 栈模拟dfs
- hdu 4090 GemAnd Prince (bfs+dfs+模拟)
- hdu 3259 An interesting mobile game (dfs+bfs+模拟)
- [ACM] hdu 1035 Robot Motion (模拟或DFS)
- hdu 1298 字典树 + DFS (模拟T9文本输入)
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- HDU-2259-Continuous Same Game (2)(BFS+DFS+模拟)
- HDU 4801 Pocket Cube(暴力模拟 dfs)
- HDU 1111 - Secret Code (DFS + 一点数学 + 模拟)
- hdu 水题 2041超级楼梯
- HDU3665Seaside(最短路径)
- POJ 1274 The Perfect Stall (匈牙利算法模板)
- Android学习笔记(十二):ListView和GridView
- [Scala]第一个Scala小程序
- HDU 3699--DFS+模拟
- spring_依赖注入(DI)
- 初识cache
- ACdream OJ 1063 平衡树
- Windows编程-创建窗口
- HDU1231最大的连续序列和
- LeetCode OJ-70. Climbing Stairs(爬楼梯问题)
- RESTful API 设计指南
- SQLite