题目链接: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 ;
}

HDU-4681 String 枚举+DP的更多相关文章

  1. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  2. HDU 4681 String(2013多校8 1006题 DP)

    String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Subm ...

  3. HDU 4681 String(DP)

    题目链接 枚举A和B中每一段含有C的段,A的前面 后面和B前面后面,求最长公共子序.观察发现,可以预处理最长公共子序. #include <iostream> #include <c ...

  4. HDU 4681 STRING dp+暴力。

    题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...

  5. HDU 4681 String 最长公共子序列

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...

  6. HUST 4681 String (DP LCS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 题目大意:给定三个字符串A,B,C 求最长的串D,要求(1)D是A的字序列 (2)D是B的子序列 ...

  7. hdu 4681 string

    字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...

  8. hdu 4681 String(转载)

    #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...

  9. HDU 4681 String 胡搞

    设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...

随机推荐

  1. [笨木头FireFly 02]入门篇2_客户端发送请求,服务器处理请求

    原地址:http://www.9miao.com/question-15-53940.html 好,经过上一篇不权威的讲解,大家已经能轻易地让客户端和服务端连接起来了. 但是,仅仅是连接了,可它们俩不 ...

  2. 看文档要看仔细,英语要加强啊... cocos2d-x 的 API 和 对应版本的 cocos2d-js 的 API 没有完全对应

    /** * Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew ...

  3. Java Web开发 之小张老师总结EL、JSP、Servlet变量

    EL 11 JSP 9 Servlet JSP类别 pageContext pageContext * 作用域 pageScope pageContext.getAttribute() * reque ...

  4. live555源码研究(一)------live555MediaServer的启动过程和基本类图

    live555MediaServer.cpp就是live555服务器启动的过程. 一.启动过程 1,构造运行环境,运行环境包括了TaskScheduler 2,构造鉴权数据,也就是登陆的用户名和密码等 ...

  5. knowledge about apache

    http://wenku.baidu.com/link?url=6O51BQJdtFRFWDGszKfN3aK7IY92QTCpuc7miBhRLazXvxL5gXb18B_TqIdi3EruX1o_ ...

  6. 再谈 retain,copy,mutableCopy(官方SDK,声明NSString都用copy非retain)

    之前一直以为retain就是简单的计数器+1,copy就是重新开辟内存复制对象: 其实不是这样,原来之前的自己独自徘徊于糊涂之中. (官方SDK,对NSString属性的定义都是用copy,而不是re ...

  7. 【剑指offer】找出数组中出现一次的两个数

    2013-09-08 10:50:46 一个整型数组中,除了两个数字之外,其他数字都出现了2次,找出这两个只出现一次的数字,要求时间复杂度是O(N),空间复杂度是O(1). 小结: 任何数与0异或,结 ...

  8. 转载:C++ list 类学习笔记

    声明:本文转自http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面 ...

  9. short s1 = 1; s1 = s1 + 1;有错而short s1 = 1; s1 += 1正确

    这个问题以前碰到过,也研究过,发表一下:    如果你认为表达式(x += i)只是表达式(x = x + i)的简写方式,这并不准确.这两个表达式都被称为赋值表达式.第二个表达式使用的是简单赋值操作 ...

  10. UVa 10539 (筛素数、二分查找) Almost Prime Numbers

    题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...