DFS——Additive equations

Posted 只有你

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DFS——Additive equations相关的知识,希望对你有一定的参考价值。

Additive equations

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 9
Problem Description
    We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an additive equation is, let‘s look at the following examples:
    1+2=3 is an additive equation of the set {1,2,3}, since all the numbers that are summed up in the left-hand-side of the equation, namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3 and 2+1=3 the same equation, and will always output the numbers on the left-hand-side of the equation in ascending order. Therefore in this example, it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
    It is not guaranteed that any integer set has its only additive equation. For example, the set {1,2,5} has no addtive equation and the set {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6, etc. When the number of integers in a set gets large, it will eventually become impossible to find all the additive equations from the top of our minds -- unless you are John von Neumann maybe. So we need you to program the computer to solve this problem.

 

Input

The input data consists of several test cases.
The first line of the input will contain an integer N, which is the number of test cases.
Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.

Output

For each test case, you are supposed to output all the additive equations of the set. These equations will be sorted according to their lengths first( i.e, the number of integer being summed), and then the equations with the same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can‘t find any equations." in a line. Print a blank line after each test case.

Sample Input
3
3 1 2 3
3 1 2 5
6 1 2 3 5 4 6

Output for the Sample Input

1+2=3

Can‘t find any equations.

1+2=3
1+3=4
1+4=5
1+5=6
2+3=5
2+4=6
1+2+3=6


题意:按顺序列举出所有满足集合内的数个元素之和仍然属于该集合的等式.而顺序是元素数目少的优先,数目相同的按字典序排列.
分析:搜索+减枝.用par数组来判断“和”是否属于该集合,用sum数组来存放等式左边,用ans来存放等式右边.
技术分享
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 int n,m,ans,pos,len;
 6 int flag;
 7 const int maxn=110;
 8 int a[maxn],vis[maxn];
 9 int sum[maxn];
10 int par[11000];
11 void dfs(int m,int len)
12 {
13     if(m>=n)
14         return ;
15     if(pos>n)
16         return ;
17     if(ans>a[n-1])
18         return ;
19     if(par[ans]==1&&m>1&&m==len)
20     {
21         flag=1;
22         for(int j=0;j<m-1;j++)
23         {
24             printf("%d+",sum[j]);
25         }
26         printf("%d=%d\n",sum[m-1],ans);
27         return ;
28     }
29     for(int i=pos;i<n;i++)
30     {
31         if(vis[i]==0)
32         {
33             vis[i]=1;
34             sum[m]=a[i];
35             ans+=sum[m];
36             pos=i;
37             dfs(m+1,len);
38 
39             vis[i]=0;
40             ans-=sum[m];
41         }
42     }
43 }
44 void init()
45 {
46     memset(par,0,sizeof(par));
47     memset(vis,0,sizeof(vis));
48     ans=pos=flag=m=0;
49 }
50 void solve()
51 {
52     for(int i=2;i<n;i++)
53     {
54         pos=0;
55         dfs(0,i);
56     }
57 
58     if(!flag)
59         printf("Can‘t find any equations.\n\n");
60     else
61         printf("\n");
62 }
63 int main()
64 {
65     int t;
66     scanf("%d",&t);
67     while(t--)
68     {
69         scanf("%d",&n);
70         init();
71         for(int i=0;i<n;i++)
72         {
73             scanf("%d",&a[i]);
74             par[a[i]]=1;
75         }
76         sort(a,a+n);
77         solve();
78     }
79     return 0;
80 }
View Code

 













以上是关于DFS——Additive equations的主要内容,如果未能解决你的问题,请参考以下文章

代码实现 加性注意力 | additive attention #51CTO博主之星评选#

Leetcode 306. Additive Number

306. Additive Number

刷题-LeetCode306. Additive Number

0306. Additive Number (M)

306.Additive Number