Checking if a list of strings can be chained

后端 未结 8 777
庸人自扰
庸人自扰 2021-01-31 23:45

Question

Implement a function bool chainable(vector v), which takes a set of strings as parameters and returns true if they

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-31 23:56

    Here's a case where your algorithm doesn't work:

    ship
    pass
    lion
    nail
    

    Your start and end lists are both s, p, l, n, but you can't make a single chain (you get two chains - ship->pass and lion->nail).

    A recursive search is probably going to be best - pick a starting word (1), and, for each word that can follow it (2), try to solve the smaller problem of creating a chain starting with (2) that contains all of the words except (1).

提交回复
热议问题