UVALive 6884 GREAT + SWERC = PORTO dfs模拟
题目连接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4896
We want to have a great SWERC at Porto this year and we approached this challenge in several ways.
We even framed it as a word addition problem, similar to the classic SEND+MORE=MONEY, where
each letter stands for a single digit (0, 1, 2, ..., 8, 9) that makes the arithmetic operation correct. In
word additions different letters cannot be assigned the same digit and the leftmost letter in a word
cannot be zero (0). In particular, a single letter term cannot be zero.
To solve this word addition problem we had to nd positive digits for G, S and P, and digits for R,
E, A, T, W, C, O, so that each letter has a different digit and the sum is correct. It turns out that,
unlike the classical SEND+MORE=MONEY which has a single solution, GREAT+SWERC=PORTO
has six solutions.
T=7, E=3, W=9, G=1, A=0, P=4, S=2, C=8, R=6, O=5
T=7, E=3, W=9, G=2, A=0, P=4, S=1, C=8, R=6, O=5
T=8, E=5, W=1, G=3, A=7, P=9, S=6, C=4, R=0, O=2
T=8, E=5, W=1, G=6, A=7, P=9, S=3, C=4, R=0, O=2
T=9, E=5, W=2, G=1, A=8, P=7, S=6, C=4, R=0, O=3
T=9, E=5, W=2, G=6, A=8, P=7, S=1, C=4, R=0, O=3
Having more than one solution does not make GREAT+SWERC=PORTO a good problem to solve
by hand, but it is still a piece of cake for a programer. Moreover, it gives us another reason to organize
SWERC again next year and, who knows, in years to come!
Given a word addition problem, compute the number of solutions (possibly zero)
题意:
给你 n个字符串 问你是否能将1~n-1相加得到第n个字符串
你可以用0~9中数字代替某一个字母
一种数字只能代替一种字母
不同的字母不会超过10,n不超过10
题解:
对每一位填数爆搜
要耐心写好
#include<bits/stdc++.h>
using namespace std;
const int N = , M = 1e2+, mod = 1e9+, inf = 2e9;
typedef long long ll; char s[][];
int can = ,n,v[N],num[N];
void dfs(int dep,int last,int p,int now) {
int f = strlen(s[n]+);
if(dep>=(f+)) {
if(last==)
can++;
return ;
}
f = strlen(s[p]+);
if(p!=n) {
if((f - dep + ) >= ) {
if(v[s[p][f - dep + ]] != -) {
if(f - dep + == && v[s[p][f - dep + ]]==) return ;
dfs(dep,last,p+,now+v[s[p][(f - dep + )]]);
}else {
for(int i=;i<=;i++) {
if(num[i]) continue;
if(f - dep + ==&&i==) continue;
num[i] = ;
v[s[p][(f - dep + )]] = i;
dfs(dep,last,p+,now+i);
v[s[p][(f - dep + )]] = -;
num[i] = ;
}
}
}else dfs(dep,last,p+,now);
}else {
if(v[s[p][(f - dep + )]]!=-) {
if((f-dep+==)&&v[s[p][(f - dep + )]]==) return ;
if((now+last)% != v[s[p][(f - dep + )]]) return ;
dfs(dep+,(now+last)/,,);
}
else {
if(num[(now+last)%]) return ;
if(dep==f&&(now+last)%==) {return ;}
v[s[p][(f - dep + )]] = (now+last)%;
num[(now+last)%] = ;
dfs(dep+,(now+last)/,,);
num[(now+last)%] = ;
v[s[p][(f - dep + )]] = -;
}
}
}
int main()
{
while(~scanf("%d",&n)) {
for(int i=;i<=n;i++) scanf("%s",s[i]+);
int mx = ;
for(int i=;i<n;i++) {
int len = strlen(s[i]+);
mx = max(mx,len);
}
int mxx = strlen(s[n]+);
if(mx > mxx ) {
cout<<<<endl;
continue;
}
memset(num,,sizeof(num));
memset(v,-,sizeof(v));
can = ;
dfs(,,,);
cout<<can<<endl;
}
}
UVALive 6884 GREAT + SWERC = PORTO dfs模拟的更多相关文章
- Vijos P1114 FBI树【DFS模拟,二叉树入门】
描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...
- HDU 5438 Ponds dfs模拟
2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...
- UVALive 7334 Kernel Knights (dfs)
Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...
- POJ 3009 Curling 2.0(DFS + 模拟)
题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...
- UVALive - 6436、HYSBZ - 2435 (dfs)
这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做 道路修建(HYSBZ - 2435) 在 W 星球上有n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道 ...
- 牛客~~扫雷~~~DFS+模拟
链接:https://www.nowcoder.com/acm/contest/118/F来源:牛客网 题目描述 <扫雷>是一款大众类的益智小游戏,于1992年发行.游戏目标是在最短的时间 ...
- zzulioj--1089--make pair(dfs+模拟)
1809: make pair Time Limit: 1 Sec Memory Limit: 128 MB Submit: 60 Solved: 44 SubmitStatusWeb Board ...
- DFS+模拟 ZOJ 3861 Valid Pattern Lock
题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...
- UVALive 5880 Vigenère Cipher Encryption (模拟)
Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26628 Description http://7xjob4.c ...
随机推荐
- 拆分Spring配置文件
把一个applicationContext.xml拆分(还可以再分出action部分和tx事务部分) 1.dao部分 <!-- 配置DAO --> <bean id="em ...
- 修改Flume-NG的hdfs sink解析时间戳源码大幅提高写入性能
Flume-NG中的hdfs sink的路径名(对应参数"hdfs.path",不允许为空)以及文件前缀(对应参数"hdfs.filePrefix")支持正则解 ...
- acdream.Bet(数学推导)
Bet Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- mongo数据库的导入导出
http://www.iwangzheng.com/ [root@a02]$show dbs; changhong_tv_cms 0.078GB [root@a02]$ mongodump -d ch ...
- java笔试二
16.同步和异步有何异同,在什么情况下分别使用他们?举例说明.如果数据将在线程间共享.例如正在写的数据以后可能被另一个线程读到,或者正在读的数据可能已经被另一个线程写过了,那么这些数据就是共享数据,必 ...
- HDU 1069&&HDU 1087 (DP 最长序列之和)
H - Super Jumping! Jumping! Jumping! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- Linux SUID SGID 讲解
SUID属性 UNIX的内核是根据什么来确定一个进程对资源的访问权限的呢? 是这个进程的运行用户的(有效)ID,包括user id和group id.用户可以用id命令来查到自己的或其他用户的user ...
- Linux Apache和Nginx网络模型详解
进程阻塞和挂起的定义: 阻塞是由于进程所需资源得不到满足,并会最终导致进程被挂起 进程挂起的原因并不一定是由于阻塞,也有可能是时间片得不到满足,挂起状态是进程从内存调度到外存中的一种状态,若在 ...
- linux ls正则表达式
ls就是默认排序的. 所以: ls只支持通配符,不支持正则,所以单纯用ls是不能实现的. 一些正则过滤操作需要结合支持正则的命令如grep.sed或awk. 例如:ls | grep "[0 ...
- 腾讯新浪通过IP地址获取当前地理位置(省份)的接口
腾讯新浪通过IP地址获取当前地理位置(省份)的接口 腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...