Advertisement
jbn6972

Untitled

Oct 1st, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #Code Written by : John Nixon
  2. #Date: 21:07:2022  Time: 15:07:23
  3. #Copyrights are applicable
  4. import sys
  5. import copy
  6. import os
  7. sys.setrecursionlimit(10000)
  8. try:
  9.     sys.stdin = open('./input.txt', 'r')
  10.     sys.stdout = open('./output.txt', 'w')
  11. except:
  12.     pass
  13.  
  14. # Write a function countConstruct(target,wordBank) that accepts a target
  15. # string and an array of strings.
  16.  
  17. # The function should return the number of ways that the 'target' can
  18. # be constructed by concatenating elements of the 'wordBank' array.
  19.  
  20. def countConstruct(target,wordBank,memo):
  21.     if target in memo:
  22.         return memo[target]
  23.  
  24.     if target == "":
  25.         return 1
  26.    
  27.     totalCount = 0
  28.     for word in wordBank:
  29.         if word in target and target.index(word) == 0:
  30.             # wordBank.remove(word)
  31.             new_word_bank = copy.deepcopy(wordBank)
  32.             new_word_bank.remove(word)
  33.             numWaysForTheRest = countConstruct(target[len(word):],new_word_bank,memo)
  34.             totalCount += numWaysForTheRest
  35.  
  36.     memo[target] = totalCount
  37.     return memo[target]
  38.  
  39. totalMaterial = int(input())
  40. concen = [item for item in input().split()]
  41. formula= input()
  42. print(countConstruct(formula,concen,{}))
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement