Algorithm


problem link- https://www.spoj.com/problems/SAMER08D/

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=a1a2 …ar, where ai ∈ Σ, for i = 1, 2, …,r. A subsequence of w is a word x=ai1ai2 …ais such that 1 ≤ i1 < i2 < … < is ≤ r. 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

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define M_PI                3.14159265358979323846

template <typename T> T gcd(T a, T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a, T b){return a*(b/gcd(a,b));}
template <typename T> T mod_exp(T b, T p, T m){T x = 1;while(p){if(p&1)x=(x*b)%m;b=(b*b)%m;p=p>>1;}return x;}
template <typename T> T invFermat(T a, T p){return mod_exp(a, p-2, p);}
template <typename T> T exp(T b, T p){T x = 1;while(p){if(p&1)x=(x*b);b=(b*b);p=p>>1;}return x;}

const int MAXN = 1e3+5;
int dp[MAXN][MAXN];

int main(){
    io;
    int n;
    while(1){
        memset(dp, 0, sizeof dp);
        cin >> n;
        if(n == 0)
            break;
        string s1, s2;
        cin >> s1 >> s2;
        for(int i = 1;i <= s1.size(); i++){
            for(int j = 1;j <= s2.size(); j++){
                int c = 1;
                while(i-c >= 0 && j-c >= 0 && s1[i-c] == s2[j-c]){
                    if(c >= n)
                        dp[i][j] = max(dp[i][j], c + dp[i-c][j-c]);
                    c++;
                }
                dp[i][j] = max(dp[i][j], dp[i-1][j]);
                dp[i][j] = max(dp[i][j], dp[i][j-1]);
            }
        }
        cout << dp[s1.size()][s2.size()] << endl;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
lovxxelyxxxxx
xxxxxxxlovely
1
lovxxelyxxxxx
xxxxxxxlovely
3
lovxxxelxyxxxx
xxxlovelyxxxxxxx
4
lovxxxelyxxx
xxxxxxlovely
0

Output

x
+
cmd
6
7
10
0
Advertisements

Demonstration


SPOJ Solution-DNA Sequences-Solution in C, C++, Java, Python

Previous
SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python