SAMER08D  DNA Sequences
Thomas, a computer scientist that works with DNA sequences, needs to compute longest common subsequences of given pairs of strings. Consider an alphabet Σ of letters and a word w=a_{1}a_{2} …a_{r}, where a_{i} ∈ Σ, for i = 1, 2, …,r. A subsequence of w is a word x=a_{i1}a_{i2} …a_{is} such that 1 ≤ i_{1} < i_{2} < … < i_{s} ≤ r. Subsequence x is a segment of w if i_{j+1}=i_{j} + 1, for j = 1,2, …,s 1. For example the word ove is a segment of the word lovely, whereas the word loly is a subsequence of lovely, but not a segment.
A word is a common subsequence of two words w_{1} and w_{2} if it is a subsequence of each of the two words. A longest common subsequence of w_{1} and w_{2} is a common subsequence of w_{1} and w_{2} having the largest possible length. For example, consider the words w_{1}=lovxxelyxxxxx and w_{2}=xxxxxxxlovely. The words w_{3}=lovely and w_{4}=xxxxxxx, the latter of length 7, are both common subsequences of w_{1} and w_{2}. In fact, w_{4} is their longest common subsequence. Notice that the empty word, of length zero, is always a common subsequence, although not necessarily the longest.
In the case of Thomas, there is an extra requirement: the subsequence must be formed from common segments having length K or more. For example, if Thomas decides that K=3, then he considers lovely to be an acceptable common subsequence of lovxxelyxxxxx and xxxxxxxlovely, whereas xxxxxxx, which has length 7 and is also a common subsequence, is not acceptable. Can you help Thomas?
Input
The input contains several test cases. The first line of a test case contains an integer K representing the minimum length of common segments, where 1 ≤ K ≤ 100. The next two lines contain each a string on lowercase letters from the regular alphabet of 26 letters. The length l of each string satisfies the inequality 1 ≤ l ≤ 10^{3}. There are no spaces on any line in the input. The end of the input is indicated by a line containing a zero.
Output
For each test case in the input, your program must print a single line, containing the length of the longest subsequence formed by consecutive segments of length at least K from both strings. If no such common subsequence of length greater than zero exists, then 0 must be printed.
Example
Input: 3 lovxxelyxxxxx xxxxxxxlovely 1 lovxxelyxxxxx xxxxxxxlovely 3 lovxxxelxyxxxx xxxlovelyxxxxxxx 4 lovxxxelyxxx xxxxxxlovely 0 Output: 6 7 10 0
hide comments
kira49:
20210104 18:21:50
:(


karthikeyahs:
20200709 09:36:41
For 3rd test case, in the first string select 'lovxxx''xxxx' and in the second string select 'lov''xxxxxxx' 

saurabh__15:
20200325 10:52:03
Last edit: 20200325 10:57:55 

cloudy_07:
20191212 15:27:38
Last edit: 20191212 15:28:46 

prabhat7298:
20190902 22:58:53
How's the output of this sample input is 10


devarshi09:
20190510 14:29:53
Test cases are weak. O(N^3) solution passed. 

shubham_04_04:
20181026 14:10:22
:( Last edit: 20181026 15:57:21 

damu_k:
20180926 16:48:02
Can anyone tell how the output of third test case is 10 instead of 8 

kingfran1907:
20180719 15:42:26
Much easier with splay tree 

paroaro:
20180719 10:30:08
AC in one go with splay :D 
Added by:  Diego Satoba 
Date:  20081123 
Time limit:  1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  C C++ 4.3.2 CPP JAVA PASGPC PASFPC 
Resource:  South American Regional Contests 2008 