Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Code Written by : John Nixon
- #Date: 21:07:2022 Time: 15:07:23
- #Copyrights are applicable
- import sys
- import copy
- import os
- sys.setrecursionlimit(10000)
- try:
- sys.stdin = open('./input.txt', 'r')
- sys.stdout = open('./output.txt', 'w')
- except:
- pass
- # Write a function countConstruct(target,wordBank) that accepts a target
- # string and an array of strings.
- # The function should return the number of ways that the 'target' can
- # be constructed by concatenating elements of the 'wordBank' array.
- def countConstruct(target,wordBank,memo):
- if target in memo:
- return memo[target]
- if target == "":
- return 1
- totalCount = 0
- for word in wordBank:
- if word in target and target.index(word) == 0:
- # wordBank.remove(word)
- new_word_bank = copy.deepcopy(wordBank)
- new_word_bank.remove(word)
- numWaysForTheRest = countConstruct(target[len(word):],new_word_bank,memo)
- totalCount += numWaysForTheRest
- memo[target] = totalCount
- return memo[target]
- totalMaterial = int(input())
- concen = [item for item in input().split()]
- formula= input()
- print(countConstruct(formula,concen,{}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement