您的位置:首页 > 其它

LCS dp Batman lightoj 1159

2015-11-17 16:41 302 查看
/********************************************
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个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: