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=a1a2ar, where ai ∈ Σ, for i = 1, 2, …,r. A subsequence of w is a word x=ai1ai2ais such that 1 ≤ i1 < i2 < … < isr. Subsequence x is a segment of w if ij+1=ij + 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 w1 and w2 if it is a subsequence of each of the two words. A longest common subsequence of w1 and w2 is a common subsequence of w1 and w2 having the largest possible length. For example, consider the words w1=lovxxelyxxxxx and w2=xxxxxxxlovely. The words w3=lovely and w4=xxxxxxx, the latter of length 7, are both common subsequences of w1 and w2. In fact, w4 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 ≤ 103. 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
!!AV!!: 2015-06-05 20:34:57

O(n^2) in Java.. passes... no issues with time complexity...:D

Adi: 2015-06-04 00:10:03

2
aab
aaab
2
aabc
aadbc

answer:
3
4

Last edit: 2015-06-04 00:19:31
i_am_looser: 2015-05-31 20:14:47

N^2 passes.... ; )

priteshranjan: 2015-05-15 08:22:07

@Walid Amin : How is the answer 7 and not 8 ?
I think , cabcdefg should be that sequence.

Please correct me, where i went wrong.

Aadil Ahmad: 2015-04-07 16:55:00

For those who are getting TLE, dont use strlen in for loop.

ankit kumar: 2015-04-02 11:37:15

"whereas xxxxxxx, which has length 7 and is also a common subsequence, is not acceptable. " WHAT DOES THIS MEAN?

Walid Amin: 2015-03-14 21:29:05

I have seen enormous number of solutions that must get WA or TLE but they got AC in this problem
So the test cases are very weak

This case will cause many WA:
5
cabcdeabcdefg
cabcdefg
0
correct answer is 7 while many AC solutions output 8 ?!!

This case also cause TLE for many AC solutions
1
aaaaaaaa...aaaa ("a" is repeated 1000 times)
aaaaaaaa...aaaa ("a" is repeated 1000 times)
0

i suggest to add these two test cases and rejudge
Thank you :D

anti_tourist: 2014-12-10 10:44:49

unnecessary initialization cause TLE

dhanno power!! B-): 2013-12-12 08:02:35

Do bottom up and save time!
Top down gives TLE.

Adam Niedzielski: 2013-04-25 17:36:11

Tricky test case:
2
abc
abbc
0

answer:
2


Added by:Diego Satoba
Date:2008-11-23
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:C C++ 4.3.2 CPP JAVA PAS-GPC PAS-FPC
Resource:South American Regional Contests 2008