题目连接:

  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模拟的更多相关文章

  1. Vijos P1114 FBI树【DFS模拟,二叉树入门】

    描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树1,它的结点类型也包括F结点,B结点和I结点三种 ...

  2. HDU 5438 Ponds dfs模拟

    2015 ACM/ICPC Asia Regional Changchun Online 题意:n个池塘,删掉度数小于2的池塘,输出池塘数为奇数的连通块的池塘容量之和. 思路:两个dfs模拟就行了 # ...

  3. UVALive 7334 Kernel Knights (dfs)

    Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...

  4. POJ 3009 Curling 2.0(DFS + 模拟)

    题目链接:http://poj.org/problem?id=3009 题意: 题目很复杂,直接抽象化解释了.给你一个w * h的矩形格子,其中有包含一个数字“2”和一个数字“3”,剩下的格子由“0” ...

  5. UVALive - 6436、HYSBZ - 2435 (dfs)

    这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做 道路修建(HYSBZ - 2435) 在 W 星球上有n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道 ...

  6. 牛客~~扫雷~~~DFS+模拟

    链接:https://www.nowcoder.com/acm/contest/118/F来源:牛客网 题目描述 <扫雷>是一款大众类的益智小游戏,于1992年发行.游戏目标是在最短的时间 ...

  7. zzulioj--1089--make pair(dfs+模拟)

    1809: make pair Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 60  Solved: 44 SubmitStatusWeb Board ...

  8. DFS+模拟 ZOJ 3861 Valid Pattern Lock

    题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...

  9. UVALive 5880 Vigenère Cipher Encryption (模拟)

    Stack Machine Executor 题目链接: http://acm.hust.edu.cn/vjudge/problem/26628 Description http://7xjob4.c ...

随机推荐

  1. 宿主机为linux、windows分别实现VMware三种方式上网(转)

    一.VMware三种方式工作原理1 Host-only连接方式  让虚机具有与宿主机不同的各自独立IP地址,但与宿主机位于不同网段,同时为宿主主机新增一个IP地址,且保证该IP地址与各虚机IP地址位于 ...

  2. Linux rpm 命令参数使用详解[介绍和应用](转)

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  3. struts2 + ajax + json的结合使用,实例讲解

    struts2用response怎么将json值返回到页面javascript解析,这里介绍一个struts2与json整合后包的用法. 1.准备工作 ①ajax使用Jquery:jquery-1.4 ...

  4. 详谈OC(object-c)深浅复制/拷贝-什么情况下用retain和copy

    读前小提示:对于深浅复制有一个清楚的了解,对于学习oc的朋友来说,至关重要.那么首先,我们要明白深浅复制是如何定义的呢.这里为了便于朋友们理解,定义如下. 浅 复 制:在复制操作时,对于被复制的对象的 ...

  5. iOS-解决iOS8及以上设置applicationIconBadgeNumber报错的问题

    在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错.因为在IOS8中要想设置appl ...

  6. 如何使用vim的帮助功能

    set guioptions+-=m/T 更换vim的默认color schema: 将下载的color.vim文件如sonofob'sidian.vim放到/usr/share/vim/vimfil ...

  7. javascript仿天猫加入购物车动画效果

    javascript仿天猫加入购物车动画效果   注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...

  8. shell截取字符串

    image_tag="pangu-20151021102145\"" 1.用#号截取,符号-右面所有字符串 TMP=${image_tag#*-} echo $TMP 得 ...

  9. 淘宝(阿里百川)手机客户端开发日记第九篇 Looper详解

    public final class Looper: 官方的API: Class used to run a message loop for a thread. Threads by default ...

  10. DBhelper封装jdbc并解决特殊字符的问题

    1.测试数据库 1.1 数据库数据 1.2 数据库结构 1.3 数据库脚本 DROP TABLE IF EXISTS `school`.`student`; CREATE TABLE `school` ...