HNU11374:GREAT+SWERC=PORTO(DFS)

Problem description

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.

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
3
GREAT
SWERC
PORTO
3
SEND
MORE
MONEY
5
TOO
GOOD
TO
BE
TRUE
Sample Output
6
1
93
Problem Source
HNU Contest 


题意:

给出你一个字母组成的加法式子,在右对齐之后,前n-1个相加得到第n个字符串,要求你对每个字母赋值0~9,每个字母代表的数字不能一样,且没有前导0,有几种赋值方法


思路:

直接对于每个字母分别尝试不同的赋值,搜索下去


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 20005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 1000000007;

int n,letter[20],cot,len[20],hsh[200],num[200],ans;
char str[20][20];

int check()
{
    int i,j,k;
    for(i = 0; i


你可能感兴趣的:(搜索)