题目连接:

  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. Quartz.net开源作业调度框架使用详解(转)

    前言 quartz.net作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本.支持 cron-like表达式,集群,数据库.功能性能强大更不 ...

  2. SVN 学习笔记

    命令参考 Api手册 清除用户密码 rm ~/.subversion/auth 撤销本地svn操作 svn revert 解决冲突 分支处理 拷贝分支 svn copy http://svn.exam ...

  3. mysql查询在一张表不在另外一张表的记录

    mysql查询在一张表不在另外一张表的记录   问题:    查询一个表(tb1)的字段记录不在另一个表(tb2)中      条件:tb1的字段key的值不在tbl2表中      -------- ...

  4. editplus快捷键大全之editplus编辑快捷键

    前面我们说了editplus快捷键大全之editplus文件快捷键和editplus快捷键大全之editplus光标快捷键,这里我们讲一下editplus快捷键大全之editplus编辑快捷键 删除光 ...

  5. JQuery $.ajax()方法详解

    jQuery Ajax 参考手册 实例 通过 AJAX 加载一段文本: jQuery 代码: $(document).ready(function(){ $("#b01").cli ...

  6. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  7. chrome浏览器关闭标签页面

    chrome浏览器关闭标签页提示:Scripts may close only the windows that were opened by it. 解决办法:通过open方法进行关闭. open( ...

  8. Android PullToRefreshListView上拉刷新和下拉刷新

    PullToRefreshListView实现上拉和下拉刷新有两个步骤: 1.设置刷新方式 pullToRefreshView.setMode(PullToRefreshBase.Mode.BOTH) ...

  9. Windows下的cmd命令行中设置环境编码

    我们都知道,Windows下的cmd命令行默认编码是Windows系统的编码,就是ANSI编码或者说是GBK编码的,这样我们编写的很多应用比如php编写utf-8编码的应用在命令行下面运行时都会出现乱 ...

  10. extjs插件开发上传下载文件简单案例

    前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...