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. int(long) 类型转换为char

    char类型占一个字节,8位 int类型四个字节32位 (long类型的转换跟int类型相同) #include <stdio.h> ]) { buffer[] = (char)tmp; ...

  2. StringBuilder基本用法

    //StringBuilder用法 public class StringBuilderTest { public static void main(String[] args) { StringBu ...

  3. 修改TortoiseSVN客户端登陆用户

    TortoiseSVN是一款常用且非常不错的SVN工具,俗称小乌龟.开发的时候,经常用的当然是TortoiseSVN客户端了. 一般情况下,TortoiseSVN服务器提供的IP地址和用户都不会变,而 ...

  4. JAVA实现图的邻接表以及DFS

    一:定义邻接表结构储存图 package 图的遍历; //邻接表实现图的建立 //储存边 class EdgeNode { int index; // 习惯了用index,其实标准写法是(adjVer ...

  5. python中__dict__与dir()的区别

    在python中__dict__与dir()都可以返回一个对象的属性,区别在于: __dict__是对象的一个属性,而dir()是一个built-in的方法: __dict__返回一个对象的属性名和值 ...

  6. [loj2116]「HNOI2015」开店 动态点分治

    4012: [HNOI2015]开店 Time Limit: 70 Sec  Memory Limit: 512 MBSubmit: 2452  Solved: 1089[Submit][Status ...

  7. python基本数据类型的用法和区别

    原文:http://www.cnblogs.com/soaringEveryday/p/5044007.html Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置 ...

  8. [转]C/C++关于全局变量和局部变量初始化与不初始化的区别

    原文链接:http://www.kingofcoders.com/viewNews.php?type=newsCpp&id=189&number=4836955386 在C语言里,全局 ...

  9. loadrunner场景执行出现:Failed to Initialize. Reason: TimeOut

      问题1: Failed to Initialize. Reason: TimeOut LoadRunner的异常原因(Failed to Initialize. Reason: TimeOut) ...

  10. GNU Wget 1.19.4 for Windows

    资源地址:https://eternallybored.org/misc/wget/ 然后将工具目录加入环境变量