天天看點

POJ1458Common Subsequence【最長公共子序列】

Common Subsequence
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;

string s1, s2;
const int N = 510;
int dp[N][N];
void run(){
	memset(dp, 0, sizeof dp);
	int len1 = s1.length(), len2 = s2.length();
	for(int i = 1; i <= len1; i++){
		for(int j = 1; j <= len2; j++){
			if(s1[i - 1] == s2[j - 1]){
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}else{
				dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
			}
		}
	}
	printf("%d\n", dp[len1][len2]);
}

int main(){
	while(cin >> s1 >> s2)	run();
	return 0;
}