题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681
题意:给A,B,C三个串,求一个最长的串D,满足D是A和B的subsequence,C是D的substring。。
比赛那天把substing搞成了subsequence,,,sd。。。
挺水的一题,直接枚举C在A和B串中的位置,当然是最短的位置,然后求两遍A和B的最长公共子序列,一个从前往后,另一个从后往前,然后遍历枚举就可以了,O(n^2)..
//STATUS:C++_AC_343MS_8164KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e50;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End char A[N],B[N],C[N];
int da[N][],db[N][],f1[N][N],f2[N][N];
int T; void getd(char a[],char c[],int d[][],int &cnt)
{
cnt=;
int i,j,k,len=strlen(a),lenc=strlen(c);
for(i=;i<len;i++){
if(a[i]!=c[])continue;
for(j=i,k=;j<len;j++){
if(a[j]==c[k]){
k++;
if(k==lenc)break;
}
}
if(k==lenc){
d[cnt][]=i;
d[cnt++][]=j;
}
}
} int main(){
// freopen("in.txt","r",stdin);
int i,j,lena,lenb,lenc,ca=;
int cnta,cntb,ans;
scanf("%d",&T);
while(T--)
{
scanf("%s%s%s",A,B,C);
lena=strlen(A);lenb=strlen(B);lenc=strlen(C);
getd(A,C,da,cnta);
getd(B,C,db,cntb); for(i=;i<=lena || i<=lenb;i++)
f1[i][]=f1[][i]=f2[i][lenb]=f2[lena][i]=;
for(i=;i<=lena;i++){
for(j=;j<=lenb;j++){
f1[i][j]=Max(f1[i][j-],f1[i-][j]);
if(A[i-]==B[j-])f1[i][j]=Max(f1[i][j],f1[i-][j-]+);
}
}
for(i=lena-;i>=;i--){
for(j=lenb-;j>=;j--){
f2[i][j]=Max(f2[i+][j],f2[i][j+]);
if(A[i]==B[j])f2[i][j]=Max(f2[i][j],f2[i+][j+]+);
}
}
ans=;
for(i=;i<cnta;i++){
for(j=;j<cntb;j++){
ans=Max(ans,f1[da[i][]][db[j][]]+f2[da[i][]+][db[j][]+]);
}
}
if(!cnta || !cntb)ans=-lenc; printf("Case #%d: %d\n",ca++,ans+lenc);
}
return ;
}