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).

Input

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.

Output

A single line with an integer: the number of solutions of the word addition problem given as input.

Sample Input
3GREATSWERCPORTO3SENDMOREMONEY5TOOGOODTOBETRUE
Sample 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;}


0 0
原创粉丝点击