LCS dp Batman lightoj 1159

/********************************************
Author         :Crystal
Created Time   :
File Name      :
********************************************/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
typedef pair<int ,int> pii;
#define MEM(a,b) memset(a,b,sizeof a)
#define CLR(a) memset(a,0,sizeof a);
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
//#define LOCAL
int dp[55][55][55];
int main()
{
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	int t;cin >> t;
	int kase = 1;
	while(t--){
		CLR(dp);
		char ch1[55],ch2[55],ch3[55];
		scanf("%s%s%s",ch1+1,ch2+1,ch3+1);
		int a = strlen(ch1+1), b = strlen(ch2+1), c = strlen(ch3+1);
		int nmax = 0;
		for(int i=1;i<=a;i++){
			for(int j=1;j<=b;j++){
				for(int k=1;k<=c;k++){
					if(ch1[i] == ch2[j] && ch2[j] == ch3[k]){
						dp[i][j][k] = dp[i-1][j-1][k-1]+1;
					}
					else{
						dp[i][j][k] = max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));
					}
				}
			}
		}
		printf("Case %d: %d\n",kase++,dp[a][b][c]);
	}
	return 0;
}








就是一lcs變形

dp[i][j][k]表示第一個字符串前i個,第二個字符串前j個,第三個字符串前k個


發佈了75 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章