HDU 4681 String(DP)
枚举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)的更多相关文章
- HDU 4681 STRING dp+暴力。
题意:不说了很好懂. 这题这么水= =...当时竟然没有勇气暴力搜一下.昨天(好吧前天.)比赛的时候胃疼,看到这题想了一个办法就是对每一个出现最短的C串前后连接然后对这个串求最长公共子序列.其实优化一 ...
- HDU 4681 String(2013多校8 1006题 DP)
String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Subm ...
- HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
先预处理,用求最长公共子序列的DP顺着处理一遍,再逆着处理一遍. 再预处理串a和b中包含串c的子序列,当然,为了使这子序列尽可能短,会以c 串的第一个字符开始 ,c 串的最后一个字符结束 将这些起始位 ...
- HDU 4681 String 最长公共子序列
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4681 题意: 给你a,b,c三个串,构造一个d串使得d是a,b的子序列,并且c是d的连续子串.求d最大 ...
- hdu 4681 string
字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...
- hdu 4681(枚举+dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 思路:首先预处理出串C在A,B中的所有的位置,然后从前向后求一次最长公共子序列,从后向前求一次最 ...
- hdu 4681 String(转载)
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream& ...
- HDU 4681 String 胡搞
设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
随机推荐
- HDOJ 1690
Bus System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 反正切函数求圆周率 atan
#define PI atan(1.0)*4 原理:tan ∏/4=1; atan2: 返回给定的 X 及 Y 坐标值的反正切值.反正切的角度值等于 X 轴正方向与通过原点和给定坐标点 (Y坐标, X ...
- 使用msgfmt编译多语言文件
msgfmt --statistics --verbose -o django.mo django.po
- 【Web】关于URL中文乱码问题
关于URL编码 一.问题的由来 URL就是网址,只要上网,就一定会用到. ...
- Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...
- 【转】windows下安装和调用curl的方法
本文转自:http://1316478764.iteye.com/blog/2100778 curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP ...
- [转]处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
今天安装了windows7 开发web项目需要安装IIS,当安装完以后,web程序已经映射到了本地IIS上,运行出现如下错误提示 处理程序“PageHandlerFactory-Integrated” ...
- 如何关闭ie9烦人的提示信息?
①Q:如何关闭“IE 限制活动内容”的提示? A:去掉“IE 限制活动内容”的提示: 1. 找到IE的“Internet选项”: 2. 选择“高级”选项卡: 3. 在“设置”下,找到子标题“安全”,将 ...
- HttpHandler简介
新建的一般处理程序后缀为.ashx,一般会另外新建一个后缀为.ashx.cs的文件,其实所有的代码都写在.ashx.cs里面,只是微软帮我们做了一个傻瓜化的转换新建的一般处理程序,如:Text1,它就 ...
- LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件
//file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...