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 ...
随机推荐
- PHP 不让标准浏览器(firfox,chrome等)走浏览器的缓存页面
或在HTML页面里加: <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache,no-store, must-reva ...
- Python实现好友全头像的拼接
微信好友全头像 话不多说,直接上代码 import itchat import math import PIL.Image as Image import os itchat.auto_login() ...
- 千万不要运行的 Linux 命令
本文中列出的命令绝对不可以运行,即使你觉得很好奇也不行,除非你是在虚拟机上运行(出现问题你可以还原),因为它们会实实在在的破坏你的系统.所以不在root等高级管理权限下执行命令是很好的习惯. 本文的目 ...
- vmware linux虚拟机连接ip设置
首先: 点击VMware 编辑->虚拟网络编辑器: 然后选中VMnet8的查看NAT设置: 上图第二步(记下红框中网关地址和子网掩码): 第三步(用于设置虚拟机地址范围): 接下来就是设置虚拟机 ...
- HDU 1079 Calendar Game(博弈找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题目大意:给你一个日期(包含年月日),这里我表示成year,month,day,两人轮流操作,每 ...
- 【PAT】1007. 素数对猜想 (20)
1007. 素数对猜想 (20) 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数.“素数对猜想”认为“存在无穷多对相 ...
- Python全栈开发之7、模块和几种常见模块以及format知识补充
一.模块的分类 Python流行的一个原因就是因为它的第三方模块数量巨大,我们编写代码不必从零开始重新造轮子,许多要用的功能都已经写好封装成库了,我们只要直接调用即可,模块分为内建模块.自定义的模块. ...
- loadrunner参数取值方法总结
在参数设置位置有两个地方:Select next row –下一行的取值方式(针对用户)Sequential 顺序的,即所有用户都是按照同一种方式取值(都是按照Update value on方式取值, ...
- LoadRunner项目结合抓包工具
LoadRunner项目结合抓包工具 常见的抓包工具包括: 1. Http协议 报文分为"请求","应答"两大类. 请求: 方法-URL-协议/版本 ...
- Codeforces Round #302 (Div. 1) D - Road Improvement 树形dp
D - Road Improvemen 思路:0没有逆元!!!! 不能直接除,要求前缀积和后缀积!!! #include<bits/stdc++.h> #define LL long lo ...