Advertisement
nein_yards

Untitled

Sep 30th, 2022 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. def coin_chain(s: str, tourists: list[tuple[str, int]]):
  2. def coin_chain_recur(s):
  3. nonlocal tourists, profit, result
  4. if len(s) == 0:
  5. result = max(result, profit)
  6. return
  7. for i in range(len(tourists)):
  8. substr, price = tourists[i]
  9. substr_i = s.find(substr)
  10. if substr_i == -1:
  11. continue
  12. profit += price
  13. s_trunc = s[:substr_i] + s[substr_i+len(substr):]
  14. coin_chain_recur(s_trunc)
  15. coin_chain_recur(s_trunc[::-1])
  16. profit -= price
  17.  
  18. result = max(result, profit)
  19. return
  20.  
  21. result = float('-inf')
  22. profit = 0
  23. coin_chain_recur(s)
  24. coin_chain_recur(s[::-1])
  25. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement