Count Substrings

5

4 votes
String, 3D dynamic programming, Dynamic Programming, Algorithms
Problem

You are given two strings, A and B, of lengths N and M, respectively, containing lowercase English letters. You are also given a parameter K. You take out K non-overlapping non-empty sub-strings from A and concatenate them in the order in which they appear in A to get a new string. Your task is to calculate the number of ways to make the new string and string B equal. Since the answer can be large, calculate it modulo 109+7.

Note that the two ways are different if at least one of the sub-strings is different at any position or the indices with which the sub-strings are made are different.

Input Format

  • The first line contains a single integer N, denoting the length of the string A.
  • The second line contains a single integer M, denoting the length of the string B.
  • The third line contains a single integer K, denoting the number of substrings you need to take from A.
  • The fourth line contains the string A, containing lowercase English letters without spaces.
  • The fifth line contains the string B, containing lowercase English letters without spaces.

Output Format

Print the number of ways to build the string B using exactly K non-overlapping non-empty substrings from A modulo 109+7.

Constraints

1N10001M2001KM

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are two ways to obtain the ‘B’ from ‘A’ using only one sub-string. They are at positions A[0:3]=[0,1,2,3] and A[4:7]=[4,5,6,7].

Editor Image

?