Ahmad_Elsagheer's blog

By Ahmad_Elsagheer, history, 8 years ago, In English

Hello,

Sometimes I encounter problems and can't come up with a solution better than backtracking. Even my backtracking solution, I can't analyze its time complexity. So, I try implementing it, see the worst case and find my intuition is true. Here are two of them, if someone can help me to prove why it should pass the time limit.

Problem 1: UVa 10506 — The Ouroboros problem. link, solution

Problem 2: UVa 165 — Stamps. link, solution

Thanks!

  • Vote: I like it
  • +37
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it -19 Vote: I do not like it

It happened with me too in 2-3 questions in the past,I just memoize with the help of map and my code gets accepted.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The first problem actually doesn't need backtracking. You can implement an euler cycle algorithm on the graph where each node is one of the $$$n^m$$$ strings, minus the last digit. So you have a graph with $$$n^{(m-1)}$$$ nodes, each with $$$m$$$ outgoing edges. For example, $$$ (223) \overset{1}{\rightarrow} (231) $$$ is an edge.

Then, each string of the problem has a one-to-one correspondence with an edge. And the "ouroboros" is an euler cycle on this graph. code, similar problem