title: Lcs Formed Consecutive Segments Least Length K
signature: class Solution {
  
      public int LcsFormedConsecutiveSegmentsLeastLengthK(int k, string s1, string s2) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(int k, string s1, string s2, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(k, s1, s2); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  4, "aggayxysdfa", "aggajxaaasdfa", 
                  8
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  2, "55571659965107", "390286654154", 
                  2
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  3, "01011011100", "0000110001000", 
                  7
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  5, "aggasdfa", "aggajasdfaxy", 
                  5
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  2, "5710246551", "79032504084062", 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  3, "0100010", "10100000", 
                  5
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  3, "aabcaaaa", "baaabcd", 
                  4
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  1, "1219", "3337119582", 
                  3
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  2, "111000011", "011", 
                  3
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  2, "wiC oD", "csiuGOUwE", 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
source_py: def lcs_formed_consecutive_segments_least_length_k(k, s1, s2):
      n = len(s1)
      m = len(s2)
      lcs = [[0 for x in range((m + 1))] for y in range((n + 1))]
      cnt = [[0 for x in range((m + 1))] for y in range((n + 1))]
      for i in range(1, (n + 1)):
          for j in range(1, (m + 1)):
              lcs[i][j] = max(lcs[(i - 1)][j], lcs[i][(j - 1)])
              if (s1[(i - 1)] == s2[(j - 1)]):
                  cnt[i][j] = (cnt[(i - 1)][(j - 1)] + 1)
              if (cnt[i][j] >= k):
                  for a in range(k, (cnt[i][j] + 1)):
                      lcs[i][j] = max(lcs[i][j], (lcs[(i - a)][(j - a)] + a))
      return lcs[n][m]
source_cpp: int lcs_formed_consecutive_segments_least_length_k(int k, string s1, string s2) {
    int n = s1 . length ( );
    int m = s2 . length ( );
    int lcs [ n + 1 ] [ m + 1 ];
    int cnt [ n + 1 ] [ m + 1 ];
    memset ( lcs, 0, sizeof ( lcs ) );
    memset ( cnt, 0, sizeof ( cnt ) );
    for ( int i = 1;
    i <= n;
    i ++ ) {
      for ( int j = 1;
      j <= m;
      j ++ ) {
        lcs [ i ] [ j ] = max ( lcs [ i - 1 ] [ j ], lcs [ i ] [ j - 1 ] );
        if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1;
        if ( cnt [ i ] [ j ] >= k ) {
          for ( int a = k;
          a <= cnt [ i ] [ j ];
          a ++ ) lcs [ i ] [ j ] = max ( lcs [ i ] [ j ], lcs [ i - a ] [ j - a ] + a );
        }
      }
    }
    return lcs [ n ] [ m ];
  }