HDU-4681 String 枚举+DP
题目链接: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的更多相关文章
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- HDU 4681 String(2013多校8 1006题 DP)
String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- HDU 4681 String(DP)
题目链接 枚举A和B中每一段含有C的段,A的前面 后面和B前面后面,求最长公共子序.观察发现,可以预处理最长公共子序. #include <iostream> #include <c ...
- HDU 4681 STRING dp+暴力。
题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...
- HDU 4681 String 最长公共子序列
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...
- HUST 4681 String (DP LCS变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 题目大意:给定三个字符串A,B,C 求最长的串D,要求(1)D是A的字序列 (2)D是B的子序列 ...
- hdu 4681 string
字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...
- hdu 4681 String(转载)
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...
- HDU 4681 String 胡搞
设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...
随机推荐
- C++ 中判断非空的错误指针
最近在写网络上的东西,程序经过长时间的运行,会出现崩溃的问题,经过DUMP文件的查看,发现在recv的地方接收返回值的时候,数据的长度异常的大差不多16亿多字节.而查看分配后的char指针显示为错误的 ...
- rand5()产生rand7()
http://www.cnblogs.com/dwdxdy/archive/2012/07/28/2613135.html 利用rand5()产生rand7().rand5()产生1到5的整数,ran ...
- TCP协议可靠性数据传输实现原理分析
http://blog.csdn.net/chexlong/article/details/6123087 TCP 协议是一种面向连接的,为不同主机进程间提供可靠数据传输的协议.TCP 协议假定其所使 ...
- 从iPhone4、iPhone5、iPhone6看手机外壳加工工艺进化史
从iPhone4.iPhone5到iPhone6,苹果为我们推出了一代又一代新产品,让我们享受到最新的科技产品.每次不只是配置上的改变,苹果在工艺上也不断改变.下面就阐述一下我对这几款手机在设计和制造 ...
- 安装Ubuntu双系统系列——安装中文输入法
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架.在Ubuntu的中文 ...
- 配置oschina for pc 开发环境
oschina for pc 是一款用python + pyqt + html + css +js开发的桌面程序,感觉十分强大.python开发快捷, 网页技术绚丽,正是桌面程序需要的. 感谢铂金小鸟 ...
- hdu4745Two Rabbits(dp)
链接 哎..比赛中一下想到了公共子序 之后思维就被局限了 一直在这附近徘徊 想着怎么优化 怎么预处理.. 观看了众多神牛的代码 ..以前觉得自己能写出个记忆化的最长回文长度 还挺高兴的...现在觉得好 ...
- js 动态计算折扣后总价格
<script type="text/javascript"> <!---计算折扣后的总价格---> function outtotalprice(i) { ...
- UVa 1213 (01背包变形) Sum of Different Primes
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...
- PHP Apache Access Log 分析工具 拆分字段成CSV文件并插入Mysql数据库分析
现在需要分析访问日志,怎么办? 比如分析D:\Servers\Apache2.2\logs\access2014-05-22.log http://my.oschina.net/cart/针对这个问题 ...