Title Description
Description
The so-called insect food calculation, is a part of the original formula has been chewed off by the worm, we need to determine the number of the remaining numbers to be chewed off the letter. Let's look at a simple example:
43#9865#045
+ 8468#6633
44445506978
The # number represents the number eaten by worms. According to the calculation, we can easily judge: the first row of two numbers are 5 and 3, the second row of the number is 5.
Now, let's do two restrictions on the problem:
First of all, we only consider the addition of insect food calculation. The addition here is N-binary addition, the calculation of three numbers have n bits, allowing a leading 0.
Secondly, the worm eats up all the numbers, we only know which ones are the same, we use the same numbers in the same letters, different numbers are represented by different letters. If this equation is n-ary, we take the first n capitals of the English alphabet to represent the n different numbers from 0 to N-1 in this equation: But the n-letters do not necessarily represent 0 to N-1 in sequence. Input data guarantees that n letters appear at least once.
Badc
+ CBDA
DCCC
The above equation is a 4-binary calculation. Obviously, we just have to let ABCD represent 0123, so we can make this equation. Your task is to find the number represented by n different letters for a given n-ary addition calculation, so that the addition formula is set up. The input data is guaranteed to have only one set of solutions,
Enter a description
Input Description
The input contains 4 rows. The first line has a positive integer N (n<=26), and the following 3 lines each have a string of uppercase letters representing two addend and A and. There are no spaces at either end of the 3 strings, from high to low, and there are exactly n bits.
Output description
Output Description
The output contains one row. In this line, the unique set of solutions should be included. The solution is that: output n numbers, respectively, a,b,c ... The number represented, the adjacent two digits are separated by a space and cannot have extra spaces.
Sample input
Sample Input
5
abced
Bdace
Ebbaa
Sample output
Sample Output
1 0 3) 4 2
Data range and Tips
Data Size & Hint
For 30% of data, n<=10; is guaranteed
For 50% of data, n<=15; is guaranteed
For all data, the n<=26 is guaranteed.
/*The enumeration is all arranged and adds two pruning ① when the letters in a column are all enumerated and do not meet the requirements; ② when the letters in one column are enumerated in 2, another number is already used; (Pay attention to rounding when pruning)*/#include<iostream>#include<cstdio>#include<cstring>#defineM 27using namespacestd;Chars[4][m];intN,now;intA[m],b[m],c[m];BOOLgoal;BOOLJudge () {inttemp=0, k=0; for(inti=n-1; i>=0;--i) {temp= (b[s[1][i]-'A']+b[s[2][i]-'A']+K)%N; K= (b[s[1][i]-'A']+b[s[2][i]-'A']+K)/N; if(temp!=b[s[3][i]-'A'])return 0; } return 1;} BOOLCleck ()//Pruning{ intTemp,t1,t2,t3; for(inti=n-1; i>=0; i--) {T1=s[1][i]-'A', t2=s[2][i]-'A', t3=s[3][i]-'A'; if(b[t1]!=-1&&b[t2]!=-1&&b[t3]!=-1) { if((b[t1]+b[t2]+1)%n==b[t3]| | (B[t1]+b[t2])%N==B[T3])Continue; Else return 0; } if(b[t1]!=-1&& b[t2]!=-1) {Temp= (B[t1]+b[t2])%N; if(a[temp]==-1|| a[(temp+1)%n]==-1)Continue; Else return 0; } if(b[t1]!=-1&&b[t3]!=-1) {Temp=b[t3]-B[T1]; if(temp>=0&&a[temp]==-1)Continue; Temp+=N; if(temp>=0&&a[temp]==-1)Continue; Temp=b[t3]-b[t1]-1; if(temp>=0&&a[temp]==-1)Continue; Temp+=N; if(temp>=0&&a[temp]==-1)Continue; return 0; } if(b[t2]!=-1&& b[t3]!=-1) {Temp=b[t3]-B[T2]; if(temp>=0&&a[temp]==-1)Continue; Temp+=N; if(temp>=0&&a[temp]==-1)Continue; Temp=b[t3]-b[t2]-1; if(temp>=0&&a[temp]==-1)Continue; Temp+=N; if(temp>=0&&a[temp]==-1)Continue; return 0; } } return 1;}voidDFS (intk) { if(k>N) {if(Judge ()) goal=1; return; } for(inti=n-1; i>=0;--i)if(a[i]==-1) {A[i]=C[k]; B[C[K]]=i; if(Cleck ()) DFS (k +1); if(goal)return; A[i]=-1; B[C[K]]=-1; }}intMain () {scanf ("%d",&N); scanf ("%s", s[1]); scanf ("%s", s[2]); scanf ("%s", s[3]); Memset (A,255,sizeof(a)); for(inti=n-1; i>=0; i--) for(intj=1; j<=3; j + +) if(b[s[j][i]-'A']==0) {B[s[j][i]-'A']=-1; c[++now]=s[j][i]-'A'; } DFS (1); for(intI=0; i<n;i++) printf ("%d", B[i]); return 0;}
View Code
Insect food calculation (Codevs 1064)