DNA Subsequence
Time limit1sMemory limit128 MB
Find the longest common subsequence of two words where every maximal matched run must be a contiguous block of at least K characters in both words.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String, Prefix sum
- Solved
- No attempts yet
Problem
Sanggeun is a computer scientist studying DNA sequences, and he wants to compute a constrained longest common subsequence of two strings.
Consider a word over an alphabet (each ). A subsequence of is chosen by indices . A subsequence in which for every (that is, characters taken from consecutive positions) is called a segment of . For example, ove is a segment of lovely, while loly is a subsequence of lovely but not a segment.
A word that is a subsequence of both and is a common subsequence; the longest such word is a longest common subsequence. The empty word of length is always a common subsequence.
Now add the following restriction. The chosen common subsequence must be formed by concatenating, in order, common segments — blocks that occur contiguously in both words — and every common segment used must have length at least . Equivalently, when the common subsequence is aligned against each word, every maximal run of consecutively matched characters must have length at least .
For example, with and the two words lovxxelyxxxxx and xxxxxxxlovely, the word lovely can be split into the two common segments lov (length ) and ely (length ), so it satisfies the condition. On the other hand, xxxxxxx cannot be split into common segments that are all of length at least , so it does not satisfy the condition.
Given the two words and , write a program that finds the maximum possible length of a common subsequence satisfying the above condition.
Input
The input consists of several test cases. The first line of each test case contains an integer (). The next two lines each contain one string consisting only of lowercase letters. Each string has length between and , inclusive. The last line of the input contains a single , which marks the end of the input.
Output
For each test case, print on its own line the maximum length of a common subsequence that satisfies the condition. If there is no such common subsequence of positive length, print .