swerc2014 GREAT + SWERC = PORTO(dfs,注意姿势)
来源:互联网 发布:超牛数据恢复手机版 编辑:程序博客网 时间:2024/06/01 14:25
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.
GREAT + SWERC = PORTO
Given a word addition problem, compute the number of solutions (possibly zero).
A line with an integer n, followed by n lines containing a word each with maximum length of 10 letters. The first n-1 words are the terms to be added and the last line is the result.
Words contain only capital letters. If words have different lengths, they must be interpreted as aligning to the right. For instance, in the SEND+MORE=MONEY problem, the D of the first word and E of the second word align with the Y of the final word. You can also assume that the size of the last word is greater than or equal to the maximum size of the preceding words, and moreover, at most ten distinct letters are involved in a word problem.
3<=n<=10 Each word has at most 10 symbols (capital letters). A word problem has at most 10 distinct letters.
A single line with an integer: the number of solutions of the word addition problem given as input.
Sample Input3GREATSWERCPORTO3SENDMOREMONEY5TOOGOODTOBETRUESample Output
6193
这题很久都写不对。原因在于考虑的方式错误。同样dfs我是想从左到右一列一列往下dfs,等到字母没有出现过再尝试赋值。而标程是先赋值一个字母,再dfs一遍所有字符串(最多10*10)判断是否可行:如果可行的话继续赋值下一个字母再dfs,直到所有字母都被赋值结束。
分析一下我的错误原因。假如遇到了一个没出现过字母,我先赋值,再继续沿规定路线dfs,这样一直往后往后碰到了一列不成立的等式,那肯定要把赋的这个值变回-1再return。然而return回去的那个点很可能是在原本赋值的点还要往下/右,它默认的是前面原本算上赋值点成立的等式现在还成立,但现在这个赋值点的值变回-1了,所以应该不能成立。
#include<iostream>#include<algorithm>#include<string>#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}#include<vector>#include<cmath>#include<queue>#include<string.h>#include<stdlib.h>#include<cstdio>#define mod 1e9+7#define ll long longusing namespace std;map<char,int> r;string x[20];char y[20]; int v[20],rr[200],n,s,maxx;int isok(int k){int g=0;for(int i=0;i<n;++i){if(k==0&&rr[x[i][x[i].length()-1]]==k)return 0;}for(int j=0;j<maxx;++j){int s=0;for(int i=0;i<n-1;++i){if(x[i].length()<=j)continue;if(rr[x[i][j]]==-1)return 1;s+=rr[x[i][j]];}if(x[n-1].length()<=j)return 0;if(rr[x[n-1][j]]==-1)return 1;if((s+g)%10!=rr[x[n-1][j]])return 0;g=(s+g)/10;}return (g==0?1:0); //注意}void dfs(int p){if(p==0){s++;return;}for(int k=0;k<=9;++k){if(v[k]==1)continue;v[k]=1;rr[y[p]]=k;if(isok(k))dfs(p-1);rr[y[p]]=-1;v[k]=0;}}int main(){while(cin>>n){s=0;maxx=0;r.clear();int c=0;for(int i=0;i<n;++i){cin>>x[i];if(x[i].length()>maxx)maxx=x[i].length();for(int j=0;j<x[i].length();++j){if(r[x[i][j]]==0){r[x[i][j]]=++c;y[c]=x[i][j];}}reverse(x[i].begin(),x[i].end());}memset(rr,-1,sizeof(rr));dfs(c);cout<<s<<endl;}return 0;}
- swerc2014 GREAT + SWERC = PORTO(dfs,注意姿势)
- HNU11374:GREAT+SWERC=PORTO(DFS)
- GREAT + SWERC = PORTO UVALive
- SWERC 2016 F题(dfs序+主席树)
- Kaggle笔记:Porto Seguro’s Safe Driver Prediction(1)
- Kaggle笔记:Porto Seguro’s Safe Driver Prediction(2)
- SGU 322 The Great Union dfs模拟
- hdu1627 Krypton Factor(注意后缀+dfs)
- SWERC2014 PRO A
- 习题8-1 装箱(Bin Packing, SWERC 2005, UVa1149)
- jzoj5336 【NOIP2017提高A组模拟8.24】提米树 (dfs序dp,奇异姿势dp)
- [论树剖与DFS序的正确姿势]
- Porto竞赛第一名解决方法
- 数字dfs注意问题
- ZOJ 1004 Anagrams by Stack (DFS 注意栈!)
- 姿势
- dfs的姿势 尽量减少不必要的搜索
- 顾客是上帝(Keep the Customer Satisfied, ACM/ICPC SWERC 2005, UVa1153)
- 【Splay】 HDU 1754 I Hate It
- bzoj 2049
- IOS 开发笔记——做日历开发的总结
- Version xx of the JVM is not suitable for this product
- 惠普记事(一)
- swerc2014 GREAT + SWERC = PORTO(dfs,注意姿势)
- HashMap实现原理
- CSS基础(一)
- XMPP本地服务器搭建
- MVC模式
- Switch
- service注入DAO组件 与 Action注入service区别
- JAVA中转义字符
- 使用call_user_func_array()来回调执行函数与直接使用函数的区别是什么? 周梦康周梦康 839 2014年04月15日 提问 · 2014年04月15日 更新 关注 0 关注 收藏