USACO 1.1 broken necklace 分析

Main idea:

      Given a string of beads composed of b, r, w, where w can be used as b or r, it can be disconnected from one place, and then collected on both sides, only the same color can be collected, otherwise stop, and find the most collectable Number of beads

scale:

      Bead string length<=350

Method 1: Simulation

      Enumerate the broken positions, and then start collecting beads from both sides;

      The efficiency is O(N^2)

After reading Analysis, I learned the method of Dynamic Progamming

Method 2:

     If you start collecting in a certain direction, the beads that can be collected are only related to the previous collection, which satisfies no backwardness.

   

 Preprocess the two directions from left to back and right to left respectively, and then enumerate the discontinuous points.

     Efficiency is O(N)

 

The biggest inspiration for this question is to double the length of the string to deal with the problem of looping.

 

My code:

Guess you like

Origin blog.csdn.net/zjsxzjb/article/details/6173380