Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. MOD = 10 ** 9 + 7
  2.  
  3.  
  4. def solveBrute(K, S):
  5.     N = len(S)
  6.     dp = [0] * (N + 1)
  7.     dp[0] = 1
  8.     for i in range(N + K):
  9.         # dp[j] currently counts all strings of length i which contains S[:j] as a subsequence
  10.  
  11.         nextDp = [0] * (N + 1)
  12.         for j in range(N):
  13.             nextDp[j] += dp[j] * 25  # don't extend subsequence
  14.             nextDp[j + 1] += dp[j]  # extend subsequence
  15.         nextDp[N] += dp[N] * 26  # max len subsequence can't be extended anymore
  16.  
  17.         dp = nextDp
  18.         for j in range(N + 1):
  19.             dp[j] %= MOD
  20.         # print(dp)
  21.  
  22.     return dp[N]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement