Created
February 24, 2014 16:33
-
-
Save mryoshio/9191690 to your computer and use it in GitHub Desktop.
Naive implementation of LCS
This file contains 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
#include <iostream> | |
#include <string> | |
using namespace std; | |
int N, M; | |
string S, T; | |
int calc(int n, int m) { | |
if (n == N || m == M) | |
return 0; | |
if (S[n] == T[m]) { | |
return calc(n+1, m+1) + 1; | |
} else { | |
return max(calc(n+1, m), calc(n, m+1)); | |
} | |
} | |
void solve() { | |
int res = calc(0, 0); | |
cout << res << endl; | |
} | |
int main() { | |
cout << "n: "; cin >> N; | |
cout << "m: "; cin >> M; | |
cout << "s: "; cin >> S; | |
cout << "t: "; cin >> T; | |
solve(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment