Last active
September 23, 2015 23:13
-
-
Save joe-cai/2161df9f9af0baa9a6b5 to your computer and use it in GitHub Desktop.
Longest Repeating Subsequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// geeksforgeeks | |
// Longest Repeating Subsequence | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
string input; | |
cin >> input; | |
vector<vector<int>> dp(input.size() + 1, vector<int>(input.size() + 1, 0)); | |
// basically finding the longest common substring of input and itself | |
// with a constraint that character in same position of input is not allowed | |
for (size_t i = 1; i <= input.size(); i++) | |
for (size_t j = 1; j <= input.size(); j++) { | |
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); | |
if (i != j && input[i - 1] == input[j - 1]) | |
dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]); | |
} | |
cout << dp[input.size()][input.size()] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment