Introduction:
EDIT: See the solution at the bottom of this question (C ++)
I already have a programming contest, and I'm cooking :)
I use these questions:
http://cemc.math.uwaterloo.ca/contests/computing/2009/stage2/day1.pdf
I look at issue B ("Dinner").
Any idea where to start? I canβt even think of anything other than a naive approach (i.e., to try permutations), which for too long would be considered the right answer.
Btw, which is spoken by C ++ and pascal, I think, but I don't care which language you use. I mean, all I want is a hint at the direction in which I should work, as well as a brief explanation, agree with this. It seems to me that I will miss something obvious ...
Of course, extended assumptions are more than welcome, but I just wanted to clarify that I'm not looking for a complete solution here :)
Short version of the question:
You have a binary string N of length 1-100 (in the question they use H and G instead of one and 0). You must remove all the numbers from it in the least number of steps . At each step, you can delete any number of adjacent digits if they are the same. That is, at each step, you can delete any number of neighboring Gs or any number of neighboring H, but you cannot delete H and G in one step.
Example:
HHHGHHGHH
Solution for example:
1. HHGGHH (remove middle Hs) 2. HHHH (remove middle Gs) 3. Done (remove Hs) -->Would return '3' as the answer.
Please note that there may also be a restriction on how large adjacent groups should be when you delete them. For example, he may say β2,β and then you cannot delete individual numbers (you will need to delete pairs or larger groups at a time).
Decision
I took the main algorithm of Mark Harrison and the idea of ββgrouping Paradigm and used them to create the solution below, you can try it on official tests , if you want.
//B.cpp //include debug messages?
Some of the official test cases you can try:
Test case 3
8 2 GHHGHGGH
Answer: 4
Test Example 6
20 2 GGHGGHHGGGHHGHHGHHGG
Answer: 6
Test Example 14
100 4 HGHGGGHGGGHGHGGGHHGHGGGHHGHHHGHGGHGGHHHGGHHGHHGHGHHHHGHHGGGHGGGHGHGHHGGGHGHGHGGGHHGHHHGHGGGHGGGHGHHH
Answer: -1
Explanation: -1 is output when there is no correct answer.
Test Example 18
100 5 GHGGGGGHGGGGGGGHHHHGGGGGHGGHHHGGGGGHHHHGGHHHHHGGGGGGHHHHHHGGGHHHHHGHHGGHHHHHGGGHHGGHHGGGGGGHHHGGGGHH
Answer: 16
Test Example 21
95 2 GGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGGHHGHGHGHGHGHGHGHGHGHGHGHGHGHGHGHG
Answer: 32