Skip to content

Instantly share code, notes, and snippets.

@KPCCoiL
Created June 23, 2014 10:12
Show Gist options
  • Save KPCCoiL/03e99e7699ea2ae31eff to your computer and use it in GitHub Desktop.
Save KPCCoiL/03e99e7699ea2ae31eff to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
string s,t;
cin>>s>>t;
int n = s.length(),m = t.length();
int dp[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!i) dp[i][j] = j?dp[i][j-1]:0 || s[i] == t[j];
else if(!j) dp[i][j] = dp[i-1][j] || s[i] == t[j];
else dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + (s[i] == t[j]);
}
}
cout << dp[n-1][m-1] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment