Coming soon - Get a detailed view of why an account is flagged as spam!
view details
2
[C++] Depth-First optimisation: how?
Post Body

Hey /r/learnprogramming,

I'm practising for the spanish computer olympiads. In doing so I stumbled upon a problem in which I think a Depth-First search would work great.

For those of you who know spanish, the problem PDF is here: http://cl.ly/NoCy

Basically, I need to check if I can get the number n by adding or subtracting a vector of numbers I'm given. For example, with the numbers [1, 2, 3, 4, 5, 6, 7] you can get the number -2 by doing 1 2 3-4-5-6 7 = -2.

So, my approach is to check all the possible permutations of the signs by traversing a tree that looks like this: http://cl.ly/NnjY

I can get the 60% of the problem using this code: http://paste.pm/5m2.cpp

However, for large input vectors my program takes a lot of time. This is where I'd need help optimising my implementation.

I think I could "cache" chunks of the trees, but I'm not sure how I'd decide which branches to cache and how to uniquely identify them.

Any help would be appreciated!

Author
Account Strength
100%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
17,815
Link Karma
2,613
Comment Karma
15,202
Profile updated: 3 days ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
11 years ago