HDU 4681 String(2013多校8 1006题 DP)
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 11 Accepted Submission(s): 4
a) D is the subsequence of A
b) D is the subsequence of B
c) C is the substring of D
Substring here means a consecutive subsequnce.
You need to output the length of D.
For each test case, the first line only contains string A, the second line only contains string B, and the third only contains string C.
The length of each string will not exceed 1000, and string C should always be the subsequence of string A and string B.
All the letters in each string are in lowercase.
aaaaa
aaaa
aa
abcdef
acebdf
cf
Case #2: 3
For test one, D is "aaaa", and for test two, D is "acf".
明显的DP题。
求最长公共子串,正和倒各求一次,得到dp和dp3
dp[i][j]表示str1的前i个字符 和 str2的前j个字符 最长的公共子串。
dp3[i][j]表示str1的后i个字符 和 str2的后j个字符 最长的公共子串。
dp1[i][j]表示str3的前j个字符,和str1的前i个字符匹配,最后的匹配起始位置,为-1表示不能匹配。
dp2一样
然后枚举str3在str1,str2匹配的终点
/* ***********************************************
Author :kuangbin
Created Time :2013/8/15 12:34:55
File Name :F:\2013ACM练习\2013多校8\1006.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
char str1[MAXN],str2[MAXN],str3[MAXN];
int dp1[MAXN][MAXN];
int dp2[MAXN][MAXN];
int dp[MAXN][MAXN];
int dp3[MAXN][MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
int iCase = ;
while(T--)
{
iCase++;
scanf("%s%s%s",str1,str2,str3);
int len1 = strlen(str1);
int len2 = strlen(str2);
int len3 = strlen(str3);
for(int i = ; i <= len1;i++)
dp[i][] = ;
for(int i = ;i <= len2;i++)
dp[][i] = ;
for(int i = ;i <= len1;i++)
for(int j = ;j <= len2;j++)
{
dp[i][j] = max(dp[i-][j],dp[i][j-]);
if(str1[i-] == str2[j-])
dp[i][j] = max(dp[i][j],dp[i-][j-]+);
}
for(int i = ; i <= len1;i++)
dp3[i][] = ;
for(int i = ;i <= len2;i++)
dp3[][i] = ;
for(int i = ;i <= len1;i++)
for(int j = ;j <= len2;j++)
{
dp3[i][j] = max(dp3[i-][j],dp3[i][j-]);
if(str1[len1-i] == str2[len2-j])
dp3[i][j] = max(dp3[i][j],dp3[i-][j-]+);
}
for(int i = ;i <= len3;i++)
dp1[][i] = -;
for(int i = ;i <= len1;i++)
dp1[i][] = i;
for(int i = ;i <= len1;i++)
for(int j = ;j <= len3;j++)
{
if(str1[i-] == str3[j-])
dp1[i][j] = dp1[i-][j-];
else dp1[i][j] = dp1[i-][j];
}
for(int i = ;i <= len3;i++)
dp2[][i] = -;
for(int i = ;i <= len2;i++)
dp2[i][] = i;
for(int i = ;i <= len2;i++)
for(int j = ;j <= len3;j++)
{
if(str2[i-] == str3[j-])
dp2[i][j] = dp2[i-][j-];
else dp2[i][j] = dp2[i-][j];
}
int ans = ;
for(int i = ;i <= len1;i++)
for(int j = ;j <= len2;j++)
{
int t1 = dp1[len1-i][len3];
int t2 = dp2[len2-j][len3];
if(t1 == - || t2 == -)continue;
ans = max(ans,dp3[i][j]+dp[t1][t2]);
}
printf("Case #%d: %d\n",iCase,ans+len3);
}
return ;
}
HDU 4681 String(2013多校8 1006题 DP)的更多相关文章
- HDU 4691 Front compression (2013多校9 1006题 后缀数组)
Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Othe ...
- HDU 4671 Backup Plan (2013多校7 1006题 构造)
Backup Plan Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total ...
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- HDU 4704 Sum (2013多校10,1009题)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- HDU 4696 Answers (2013多校10,1001题 )
Answers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
Arc of Dream Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4678 Mine (2013多校8 1003题 博弈)
Mine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
随机推荐
- HDU 5936 朋友
题意为给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根. 当一方操作时,他们需要先选择一个不为根的点,满足该点到其父亲的边权为1; 然后 ...
- [ python ] 进程的操作
目录 (见右侧目录栏导航)- 1. 前言- 2. multiprocess模块- 2.1 multiprocess.Process模块 - 2.2 使用Process模块创建进程 - 2. ...
- [ python ] 线程的操作
目录 (见右侧目录栏导航) - 1. 前言 - 1.1 进程 - 1.2 有了进程为什么要有线程 - 1.3 线程的出现 - 1.4 进程和线程的关系 - 1.5 线程的 ...
- html清屏 meta http-equiv="refresh" content="3">
<meta http-equiv="refresh" content="3"> 什么意思? <meta http-equiv="re ...
- 网页查看源码中<div>的含义
代码如下: /** HTML转义 **/ String s = HtmlUtils.htmlEscape("<div>hello world</div><p&g ...
- SSL与HTTPS,HTTP有什么联系
有人问:http和https有什么区别? HTTP,全称"Hyper Text Transfer Protocol",是从浏览器访问网站时使用的默认协议.由于浏览器到网站之间的数据 ...
- Windows内核执行体对象管理器的操作过程与分析
我之前写过一个有关于对象管理的读书笔记.但是这篇文章与前面的不同,这是我个人对对象管理器到底是什么的一个分析,而且也是直接对WRK代码进行的阅读. 执行体对象即我们通常所言的内核对象,我们知道Wind ...
- 向SD卡写入树莓派的操作系统
这是 meelo 原创的 玩转树莓派 系列文章 用到的工具: Win32 Disk Imager: sd卡读卡器 Raspbian操作系统镜像:下载地址 步骤1:下载操作系统的镜像 树莓派基金会的网 ...
- ASP.NET Zero--1.如何开始
1.加群 群号:104390185,下载这个文件并解压 用VS2015打开aspnet-zero-1.9.0.1 2.修改配置文件 Web项目web.config连接字符串 <add name= ...
- 十五oracle 触发器
一.触发器简介 触发器的定义就是说某个条件成立的时候,触发器里面所定义的语句就会被自动的执行.因此触发器不需要人为的去调用,也不能调用.然后,触发器的触发条件其实在你定义的时候就已经设定好了.这里面需 ...