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. 使用 Xtrabackup 在线对MySQL做主从复制【转】

    1. 说明 1.1 xtrabackup mysqldump对于导出10G以下的数据库或几个表,还是适用的,而且更快捷.一旦数据量达到100-500G,无论是对原库的压力还是导出的性能,mysqldu ...

  2. 程序调试命令gdb

    锁定线程 set scheduler-locking 1.要使用此命令,先用gcc -g编译程序,如:  $gcc -g test.c -o test  编译test.c源程序,输入此程序的调试版本t ...

  3. HTML+CSS图文排版

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  4. 可图性判定--Havel-Hakimi定理

    两个概念 1.度序列 若把图G所有顶点的度数排成一个序列S,则称S为图G的度序列. 2.序列是可图的 一个非负整数组成的序列如果是某个无向图的度序列,则称该序列是可图的. Havel-Hakimi定理 ...

  5. 简易web server之python实现

    网络编程一项基本功是socket编程,包括TCP socket,UDP socket的客户端.服务器端编程. 应用层的各路协议如http,smtp,telnet,ftp等都依赖于传输层的TCP或者UD ...

  6. wondows下安装pytho&pip

    1.在https://www.python.org/downloads/下载相应的python安装包, 解压安装,配置环境变量. 2.下载pip安装包:https://pypi.python.org/ ...

  7. JS获取select的value和text值的简单实例

    本篇文章主要是对JS获取select的value和text值的简单实例进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 代码如下: <select id = "cityList ...

  8. 爱奇艺全国高校算法大赛初赛B

    二分,验证. 二分一下答案,然后验证一下统计个数能否满足即可. #include <cstdio> #include <cmath> #include <cstring& ...

  9. 洛谷P3201 [HNOI2009]梦幻布丁 [链表,启发式合并]

    题目传送门 梦幻布丁 题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. 输入输 ...

  10. mysql正则查询 模糊查询

    -- ==============正则查询================ /* SQL默认是忽略大小写的 正则模式使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它们 ...