题目链接

枚举A和B中每一段含有C的段,A的前面 后面和B前面后面,求最长公共子序。观察发现,可以预处理最长公共子序。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int dp1[][],dp2[][];
char s1[];
char s2[];
char s3[];
char s4[];
char s5[];
int que1[][];
int que2[][];
int main()
{
int t,cas = ,i,j,n1,n2,k;
int len1,len2,len3;
scanf("%d",&t);
while(t--)
{
scanf("%s%s%s",s1,s2,s3);
len1 = strlen(s1);
len2 = strlen(s2);
len3 = strlen(s3);
for(i = ;i <= len1;i ++)
{
for(j = ;j <= len2;j ++)
{
dp1[i][j] = dp2[i][j] = ;
}
}
for(i = ;i <= len1;i ++)
{
for(j = ;j <= len2;j ++)
{
if(s1[i-] == s2[j-])
dp1[i][j] = dp1[i-][j-] + ;
else
dp1[i][j] = max(dp1[i-][j],dp1[i][j-]);
}
}
for(i = ;i < len1;i ++)
{
s4[i] = s1[len1-i-];
}
for(i = ;i < len2;i ++)
{
s5[i] = s2[len2-i-];
}
for(i = ;i <= len1;i ++)
{
for(j = ;j <= len2;j ++)
{
if(s4[i-] == s5[j-])
dp2[i][j] = dp2[i-][j-] + ;
else
dp2[i][j] = max(dp2[i-][j],dp2[i][j-]);
}
}
n1 = n2 = ;
for(i = ;i < len1;i ++)
{
if(s1[i] == s3[])
{
k = ;
for(j = i+;j < len1;j ++)
{
if(s1[j] == s3[k])
k ++;
if(k == len3) break;
}
if(j != len1)
{
que1[n1][] = i;
que1[n1][] = j;
n1 ++;
}
}
}
for(i = ;i < len2;i ++)
{
if(s2[i] == s3[])
{
k = ;
for(j = i+;j < len2;j ++)
{
if(s2[j] == s3[k])
k ++;
if(k == len3) break;
}
if(j != len2)
{
que2[n2][] = i;
que2[n2][] = j;
n2 ++;
}
}
}
int ans = ;
/*for(i = 1;i <= len1;i ++)
{
for(j = 1;j <= len2;j ++)
{
printf("%d ",dp1[i][j]);
}
printf("\n");
}*/
/*for(i = 0;i < n1;i ++)
{
printf("%d %d\n",que1[i][0],que1[i][1]);
}
for(i = 0;i < n2;i ++)
{
printf("%d %d\n",que2[i][0],que2[i][1]);
}*/
for(i = ;i < n1;i ++)
{
for(j = ;j < n2;j ++)
{
ans = max(ans,dp1[que1[i][]][que2[j][]] + dp2[len1-que1[i][]-][len2-que2[j][]-]);
}
}
printf("Case #%d: %d\n",cas++,ans + len3);
}
return ;
}

HDU 4681 String(DP)的更多相关文章

  1. HDU 4681 STRING dp+暴力。

    题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...

  2. HDU 4681 String(2013多校8 1006题 DP)

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

  3. HDU 4681 string 求最长公共子序列的简单DP+暴力枚举

    先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...

  4. HDU 4681 String 最长公共子序列

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...

  5. hdu 4681 string

    字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...

  6. hdu 4681(枚举+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 思路:首先预处理出串C在A,B中的所有的位置,然后从前向后求一次最长公共子序列,从后向前求一次最 ...

  7. hdu 4681 String(转载)

    #include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...

  8. HDU 4681 String 胡搞

    设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...

  9. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

随机推荐

  1. Nested List Weight Sum I & II

    Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...

  2. Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1

    Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1 源码及详细文档下载 svn checkout http://cvg02.googlecode.co ...

  3. 【USACO】checker

    一看题目 经典的8皇后问题 不过是皇后数量可变而已 不用想 回溯法. 需要个生成每次可选择序列的函数, 在存储可选择的序列时按照先大后小的顺序排的.这样每次找最小和去掉最小都很方便,只要有个记录数量的 ...

  4. MFC 颜色选择对话框、颜色按钮

    COLORREF color=RGB(0,255,0); unsigned char r=GetRValue(color); unsigned char g=GetGValue(color); uns ...

  5. javaweb数据库操作

    本文主要内容有C3P0数据库连接池,dbutils的使用,元数据的应用 在对数据库进行增删改查时,使用数据库连接池可以有效的提高效率,节省资源,C3P0是Apache组织提供的一个有效方式 C3P0的 ...

  6. iptables 开启80端口

    [root@v01-svn-test-server online]# iptables -F#清空规则 [root@v01-svn-test-server online]# iptables -L# ...

  7. async/await 异步编程

    前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入的,由于之前对于异步编程不是很了解,所以花费了一些时间学习一下相关的知识,并整理成这篇博客,如果在 ...

  8. C# JSON字符串序列化与反序列化常见模型举例

    C#中实体转Json常用的类JavaScriptSerializer,该类位于using System.Web.Script.Serialization;命名空间中,添加引用system.web.ex ...

  9. 使用MyEclipse Swing/Matisse

     经常使用JBuilder开发工具的人都知道,在JBuilder中开发Swing应用程序是比较方便的,虽然比不上曾经红遍一时的Visual Basic,但开发界面的工作确实被大大简化了.     JB ...

  10. 基于superagent 与 cheerio 的node简单爬虫

    最近重新玩起了node,便总结下基本的东西,在本文中通过node的superagent与cheerio来抓取分析网页的数据. 目的  superagent 抓取网页 cheerio 分析网页 准备 N ...