[YBT High-Efficiency Advanced] 1 Basic Algorithm/4 Depth First Search/3 Worm Food Calculation

[YBT High-Efficiency Advanced] 1 Basic Algorithm/4 Depth First Search/3 Worm Food Calculation

Memory limit: 256 MiB
Time limit: 1000 ms
standard input and output
Question type: Traditional
Evaluation method: Text comparison

Title description

The so-called worm-eating calculation means that part of the original calculation is gnawed away by worms, and we need to determine the letters that have been gnawed away based on the remaining numbers. Let's look at a simple example:

43#9865#045
8468#6633
44445509678

The # sign represents the number gnawed away by bugs. According to the formula, we can easily judge: the two numbers in the first row are 5 and 3, and the number in the second row is 5.

Now, we impose two restrictions on the problem:

First, we only consider the addition of insect food calculation. The addition here is n-ary addition, and the three numbers in the formula have n bits, and leading 0s are allowed.

Secondly, the bug gnaws out all the numbers. We only know which numbers are the same. We use the same letters to represent the same numbers and different letters to represent different numbers. If this formula is in n base, we will take the first n capital letters of the English alphabet to represent the n different numbers from 0 to n-1 in the formula; but these n letters do not necessarily represent sequentially 0 to n-1. Enter the data to ensure that each of the n letters appears at least once.

BADC
CBDA
DCCC

The above formula is a hexadecimal formula. Obviously, as long as we let ABCD represent 0123, we can make this formula hold. Your task is to find the numbers represented by n different letters for a given base addition formula, so that the addition formula holds. The input data is guaranteed to have one and only one set of solutions.

Input format

The first line of input is an integer n, which represents a base number.

From the second line to the fourth line, each line has a string composed of uppercase letters, which represents two addends and a sum. There are no spaces at the left and right ends of this string. From left to right once represents from high to low, and there is exactly one bit.

Output format

Output a line of n integers separated by spaces, representing the numbers represented by A, B..., respectively.

Sample

Sample input

5
ABCED
BDACE
EBBAA

Sample output

1 0 3 4 2

Data range and tips

For 30% of the data, guarantee n<=10.
For 50% of the data, guarantee n<=15.
For 100% data, guarantee 1<=n<=26.

Ideas

Search each number from right to left in dfs,
check every time you search for a number
1. If all numbers on the right side are determined, (a+b+g)%n!=c,return 0;
2. If there are numbers on the right side that are uncertain, ( a+b)%n!=c&&(a+b+1)%n!=c,return 0;
3. If it is the highest bit, a+b+g>=n or a+b>=n,return 0;

Code

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n,tot,num[27];
char str[4][27],zm[27];
bool vis[27],use[27];
bool check()
{
    
    
	int g=0,a,b,c,i;
	for(i=n-1;i>=0;i--)
	{
    
    
		a=num[str[0][i]-'A'];
		b=num[str[1][i]-'A'];
		c=num[str[2][i]-'A'];
		if(a>-1&&b>-1&&c>-1)
		{
    
    
			if(g==-1)
			{
    
    
				if(((a+b)%n!=c&&(a+b+1)%n!=c)||(i==0&&a+b>=n))return 0;
			}
			else
			{
    
    
				if((i==0&&a+b+g>=n)||((a+b+g)%n!=c))return 0;
				g=(a+b+g)/n;
			}
		}
		else g=-1;
	}
	return 1;
}
bool DFS(int dep)
{
    
    
	if(dep>tot)return 1;
	for(int i=0;i<n;i++)
		if(!use[i])
		{
    
    
			num[zm[dep]-'A']=i,use[i]=1;
			if(check()&&DFS(dep+1))return 1;
			num[zm[dep]-'A']=-1,use[i]=0;
		}
	return 0;
}
int main()
{
    
    
	int i,j;
	ios::sync_with_stdio(false);
	memset(num,-1,sizeof(num));
	memset(vis,0,sizeof(vis));
	memset(use,0,sizeof(use));
	cin>>n>>str[0]>>str[1]>>str[2];
	for(i=n-1;i>=0;i--)
	{
    
    
		if(!vis[str[0][i]-'A'])vis[str[0][i]-'A']=1,zm[++tot]=str[0][i];
		if(!vis[str[1][i]-'A'])vis[str[1][i]-'A']=1,zm[++tot]=str[1][i];
		if(!vis[str[2][i]-'A'])vis[str[2][i]-'A']=1,zm[++tot]=str[2][i];
	}
	for(DFS(1),i=0;i<n;i++)cout<<num[i]<<' ';
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/115218224