title: Longest Repeated Subsequence 1
signature: function longest_repeated_subsequence_1(str)
source_py: def longest_repeated_subsequence_1(str):
      n = len(str)
      dp = [[0 for i in range((n + 1))] for j in range((n + 1))]
      for i in range(1, (n + 1)):
          for j in range(1, (n + 1)):
              if ((str[(i - 1)] == str[(j - 1)]) and (i != j)):
                  dp[i][j] = (1 + dp[(i - 1)][(j - 1)])
              else:
                  dp[i][j] = max(dp[i][(j - 1)], dp[(i - 1)][j])
      res = ''
      i = n
      j = n
      while ((i > 0) and (j > 0)):
          if (dp[i][j] == (dp[(i - 1)][(j - 1)] + 1)):
              res += str[(i - 1)]
              i -= 1
              j -= 1
          elif (dp[i][j] == dp[(i - 1)][j]):
              i -= 1
          else:
              j -= 1
      res = ''.join(reversed(res))
      return res
source_cpp: string longest_repeated_subsequence_1(string str) {
    int n = str . length ( );
    int dp [ n + 1 ] [ n + 1 ];
    for ( int i = 0;
    i <= n;
    i ++ ) for ( int j = 0;
    j <= n;
    j ++ ) dp [ i ] [ j ] = 0;
    for ( int i = 1;
    i <= n;
    i ++ ) for ( int j = 1;
    j <= n;
    j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ];
    else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ], dp [ i - 1 ] [ j ] );
    string res = "";
    int i = n, j = n;
    while ( i > 0 && j > 0 ) {
      if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) {
        res = res + str [ i - 1 ];
        i --;
        j --;
      }
      else if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) i --;
      else j --;
    }
    reverse ( res . begin ( ), res . end ( ) );
    return res;
  }
question_info.entry_fn_name: longest_repeated_subsequence_1
question_info.entry_cls_name: Solution
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(str, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(str) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver("qnQxjoRx", "x")
      print(string.format("TEST-0...%s",result))
  
      result = driver("27473733400077", "7733007")
      print(string.format("TEST-1...%s",result))
  
      result = driver("000010111111", "0000111111")
      print(string.format("TEST-2...%s",result))
  
      result = driver("TNVwgrWSLu", "")
      print(string.format("TEST-3...%s",result))
  
      result = driver("9537", "")
      print(string.format("TEST-4...%s",result))
  
      result = driver("1100", "10")
      print(string.format("TEST-5...%s",result))
  
      result = driver("lYcoiQfzN", "")
      print(string.format("TEST-6...%s",result))
  
      result = driver("52", "")
      print(string.format("TEST-7...%s",result))
  
      result = driver("00100001100", "0000010")
      print(string.format("TEST-8...%s",result))
  
      result = driver("Rkxe", "")
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.test_list: [{"idx": 0, "outputs": "x", "inputs": {"str": "qnQxjoRx"}}, {"idx": 1, "outputs": "7733007", "inputs": {"str": "27473733400077"}}, {"idx": 2, "outputs": "0000111111", "inputs": {"str": "000010111111"}}, {"idx": 3, "outputs": "", "inputs": {"str": "TNVwgrWSLu"}}, {"idx": 4, "outputs": "", "inputs": {"str": "9537"}}, {"idx": 5, "outputs": "10", "inputs": {"str": "1100"}}, {"idx": 6, "outputs": "", "inputs": {"str": "lYcoiQfzN"}}, {"idx": 7, "outputs": "", "inputs": {"str": "52"}}, {"idx": 8, "outputs": "0000010", "inputs": {"str": "00100001100"}}, {"idx": 9, "outputs": "", "inputs": {"str": "Rkxe"}}]