Lightoj 1159 DP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hzh_0000/article/details/17659015

LCS的3字符串版。。。。。和两个的求法是一样的。。。。。

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main(){
	int T, Case = 1;
	char a[51], b[51], c[51];
	int dp[51][51][51];
	int length_a, length_b, length_c;

	cin >> T;
	while( T-- ){
		scanf( "%s%s%s", a + 1, b + 1, c + 1 );

		length_a = strlen( a + 1 );
		length_b = strlen( b + 1 );
		length_c = strlen( c + 1 );

		memset( dp, 0, sizeof( dp ) );
		for( int i = 1; i <= length_a; i++ ){
			for( int j = 1; j <= length_b; j++ ){
				for( int k = 1; k <= length_c; k++ ){
					if( a[i] == b[j] && b[j] == c[k] ){
						dp[i][j][k] = dp[i-1][j-1][k-1] + 1;
					}else{
						dp[i][j][k] = max( dp[i][j][k], dp[i-1][j][k] );
						dp[i][j][k] = max( dp[i][j][k], dp[i][j-1][k] );
						dp[i][j][k] = max( dp[i][j][k], dp[i][j][k-1] );
					//	dp[i][j][k] = max( dp[i][j][k], dp[i-1][j-1][k] );
					//	dp[i][j][k] = max( dp[i][j][k], dp[i-1][j][k-1] );
					//	dp[i][j][k] = max( dp[i][j][k], dp[i][j-1][k-1] );
					}
				}
			}
		}

		cout << "Case " << Case++ << ": " << dp[length_a][length_b][length_c] << endl;
	}
	return 0;
}


 

没有更多推荐了,返回首页