String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 11    Accepted Submission(s): 4

Problem Description
Given 3 strings A, B, C, find the longest string D which satisfy the following rules:
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.
 
Input
The first line of the input contains an integer T(T = 20) which means the number of test cases.
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.
 
Output
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
 
Sample Input
2
aaaaa
aaaa
aa
abcdef
acebdf
cf
 
Sample Output
Case #1: 4
Case #2: 3

Hint

For test one, D is "aaaa", and for test two, D is "acf".

 
Source
 
Recommend
zhuyuanchen520
 

明显的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)的更多相关文章

  1. HDU 4691 Front compression (2013多校9 1006题 后缀数组)

    Front compression Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Othe ...

  2. HDU 4671 Backup Plan (2013多校7 1006题 构造)

    Backup Plan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  3. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  4. HDU 4704 Sum (2013多校10,1009题)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submi ...

  5. HDU 4699 Editor (2013多校10,1004题)

    Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  6. HDU 4696 Answers (2013多校10,1001题 )

    Answers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  7. 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 ...

  8. 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 ...

  9. HDU 4678 Mine (2013多校8 1003题 博弈)

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

随机推荐

  1. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  2. 微信access_token和refresh_token保存于redis

    简介 通常理解的access_token和refresh_token access_token是用来对客户端进行认证的,类似与密码,有一定的有效期.当过期后可使用refresh_token重新获取一个 ...

  3. [How to] 使用HBase协处理器---Endpoint服务端的实现

    1.简介 前篇文章[How to] 使用HBase协处理器---基本概念和regionObserver的简单实现中提到了两种不同的协处理器,并且实现了regionObserver. 本文将介绍如何使用 ...

  4. fail2ban安全设置

    1.先安装fail2ban服务包(这里我采用的是fail2ban-0.8.14.tar.gz) 2.解压安装包 cd /data/software tar xzf fail2ban-0.8.14.ta ...

  5. csu 1757(贪心或者树状数组)

    1757: 火车入站 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 209  Solved: 51[Submit][Status][Web Board] ...

  6. TEC-2几条微指令的微码说明 & TEC-2微程序运行测试步骤

    个人理解,不保证完全正确…… 给正在被何朝东虐的,以及将来会被何朝东虐的同胞们………… 祈祷软院赶快更新课程让下一代逃脱TEC-2魔爪,monitor里那1994的年份真是看得人一口老血…… 微码说明 ...

  7. 写在Web考试后的一点小总结

    在实验室折腾附加题折腾了一个多钟没做出来……晚上回到宿舍决定再试一试,按原来的思路居然行了,目测在实验室的时候什么地方打错字了吧(心在流血) 实现晃过元素后出现跟随鼠标的悬浮窗,只有几行代码给我折腾了 ...

  8. thinkjs项目中使用mongoose需要注意的地方

    原文链接thinkjs项目中使用mongoose需要注意的地方 由于thinkjs不支持mongodb的关联模型查询,所以我不得不使用mongoose这个第三方odm. 我有两个选择,一是像我在exp ...

  9. Wannafly挑战赛7 C - 小Q与氪金游戏

    题目描述 “为世界上所有的美好而战!”小Q同学最近沉迷“稳固3”,为了从最新的蛋池中抽出自己喜欢的角色卡,不惜氪下重金.在这个游戏中,氪一单可以得到x个宝石,而抽一次卡需要花费y个宝石,由于游戏策划十 ...

  10. 洛谷P2657 [SCOI2009]windy数 [数位DP,记忆化搜索]

    题目传送门 windy数 题目描述 windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道, 在A和B之间,包括A和B,总共有多少个win ...