Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created February 24, 2014 16:33
Show Gist options
  • Save mryoshio/9191690 to your computer and use it in GitHub Desktop.
Save mryoshio/9191690 to your computer and use it in GitHub Desktop.
Naive implementation of LCS
#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