Longest Common subsequence
1210: maximum public subsequence Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 10 Solved: 8
[Submit] [Status] [Web Board] Description
Give two strings and find the longest common sub-sequence length: each character in the sub-sequence can be found in the two original strings, the order of each character is the same as that of the original string.
For example:
ABCBDAB
BDCABA
Their Longest Common subsequence is BCBA with a length of 4.
Input
Each group contains two strings of less than 100 characters.
Output
The length of the output Longest Common subsequence.
Sample Input
ABCBDAB BDCABA
Sample Output
4
#include
#include
#include #include
using namespace std; int main(){ string str1,str2; int dp[200][200]; while(cin>>str1>>str2) { memset(dp,0,sizeof(dp)); int la = str1.length(); int lb = str2.length(); for(int i = 1; i <= la; i++) for(int j = 1; j <= lb; j++) { if(str1[i - 1] == str2[j - 1]) { dp[i][j] = dp[i-1][j-1]+1; } else dp[i][j] = max(dp[i-1][j],dp[i][j-1]); } cout<