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. 求出每一 ...
随机推荐
- Debugging with GDB 用GDB调试多线程程序
Debugging with GDB http://www.delorie.com/gnu/docs/gdb/gdb_25.html GDB调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为 ...
- 【NOIP 2016 总结】
距离杯赛已经很久了,然而我现在才打总结.. 我好惨的说..两场才380... DAY 1 第一题 toy 送分题,模拟的时候+一下再mod一下就好. [当时打完这题就没再看一眼了,好方的说] #inc ...
- SQL 两张结构一样的表合并查询 .
select * from table1 union all select * from table2 union all 是所有的都显示出来: select * from table1 union ...
- SSH 远程连接
ssh远程连接 准备工作: 1 准备两台linux pc 我们一般用的是VMware虚礼软件 2 这两台linux可以互通 3 linux1 :192.168.2.2 这台为你要连接的服务器 linu ...
- android下升级软件介绍
编译android: 生成:system.img,ramdisk.img,userdata.img映像文件. ramdisk.img是emulator的文件系统 system.img包括了主要的包.库 ...
- HBase 的安装与配置
实验简介 本次实验学习和了解 HBase 在不同模式下的配置和安装,以及 HBase 后续的启动和停止等. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shi ...
- 1439. Battle with You-Know-Who(splay树)
1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解 网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...
- poj 2635 The Embarrassed Cryptographer(数论)
题目:http://poj.org/problem?id=2635 高精度求模 同余模定理. 题意: 给定一个大数K,K是两个大素数的乘积的值.再给定一个int内的数L 问这两个大素数中最小的一个是 ...
- Web打印控件smsx.cab使用说明
在项目开发中,经常会用到页面打印的功能,在ASP.NET环境下推荐一款web打印控件smsx.cab. 使用方法:一般会先定义一个用于打印的母版页(Print.Master),在母版页上做好布局 ...
- WEB架构师成长之路-架构师都要懂哪些知识 转
Web架构师究竟都要学些什么?具备哪些能力呢?先网上查查架构师的大概的定义,参见架构师修炼之道这篇文章,写的还不错,再查查公司招聘Web架构师的要求. 总结起来大概有下面几点技能要求: 一. 架构师有 ...