A Dynamic programming problem

后端 未结 3 1277
天涯浪人
天涯浪人 2020-12-25 08:33

Can anyone help me find an optimal Dynamic programming algorithm for this problem

On the way to dinner, the CCC competitors are lining up for their delicious curly f

3条回答
  •  佛祖请我去吃肉
    2020-12-25 09:30

    I'd prefer not to solve an SPOJ problem in a practical manner for you, so take the following as an existence proof of a poly-time DP.

    For K fixed, the set of strings that can dine is context-free. I'm going to use g and h instead of G and H. For example, for K = 3, one grammar looks like

    S -> ε | g S g S g S G | h S h S h S H
    
    G -> ε | g S G
    
    H -> ε | h S H
    

    The idea is that either there are no diners, or the first diner dines with at least K - 1 others, between any two of which (and the last and the end) there is a string that can dine.

    Now use the weighted variant of CYK to find the minimum-weight parse, where nonempty S productions have weight 1, and all others have weight 0. For K fixed, the running time of CYK is O(N3).

提交回复
热议问题