title: Longest Repeated Subsequence 1
signature: string longestRepeatedSubsequence1(string str) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(string actual, string expected){
      return actual == expected;
  }
  
  string driver(string str, string expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(str), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver("qnQxjoRx", "x"); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver("27473733400077", "7733007"); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver("000010111111", "0000111111"); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver("TNVwgrWSLu", ""); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver("9537", ""); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver("1100", "10"); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver("lYcoiQfzN", ""); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver("52", ""); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver("00100001100", "0000010"); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver("Rkxe", ""); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }