UVA1625Color Lenth(DP+LCS变形 未AC)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/C
紫书P276
res[i][j]表示第一个序列移动i个,第二个序列移动j个之后有几个已经出现但尚未结束,dp[i][j]表示第一个序列移动i个,第二个序列移动j个之后的总长度
dp[i][j] = min(dp[i - 1][j],dp[i][j - 1]) + res[i][j]
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = + ;
const int INF = 0x3f3f3f3f;
char str1[MAX],str2[MAX];
int dp[MAX][MAX],res[MAX][MAX];
int start1[],End1[],start2[],End2[],vis[];
int n,m;
int main()
{
int tase;
scanf("%d", &tase);
while(tase--)
{
scanf("%s%s", str1 + ,str2 + );
n = strlen(str1 + );
m = strlen(str2 + );
memset(vis, , sizeof(vis));
memset(End1, -, sizeof(End1));
memset(End2, -, sizeof(End2));
for(int i = ; i < ; i++)
start1[i] = start2[i] = INF;
for(int i = ; i <= n; i++)
{
if(start1[str1[i] - 'A'] == INF)
start1[str1[i] - 'A'] = i;
End1[str1[i] - 'A'] = i;
}
for(int i = ; i <= m; i++)
{
if(start2[str2[i] - 'A'] == INF)
start2[str2[i] - 'A'] = i ;
End2[str2[i] - 'A'] = i;
} memset(res, , sizeof(res));
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
int cnt = ;
for(int k = ; k < ; k++)
{
if(start1[k] == INF && start2[k] == INF)
continue;
if(start1[k] > i && start2[k] > j)
continue;
if(End1[k] <= i && End2[k] <= j)
continue;
cnt++;
}
res[i][j] = cnt;
}
} dp[][] = ;
for(int i = ; i <= m ; i++)
dp[][i] = dp[][i - ] + res[][i];
for(int j = ; j <= n; j++)
dp[j][] = dp[j - ][] + res[j][];
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
dp[i][j] = min(dp[i - ][j], dp[i][j - ]) + res[i][j];
}
} printf("%d\n", dp[n][m]); } return ;
}
TLE
UVA1625Color Lenth(DP+LCS变形 未AC)的更多相关文章
- UVA-1625-Color Length(DP LCS变形)
Color Length(UVA-1625)(DP LCS变形) 题目大意 输入两个长度分别为n,m(<5000)的颜色序列.要求按顺序合成同一个序列,即每次可以把一个序列开头的颜色放到新序列的 ...
- poj1080--Human Gene Functions(dp:LCS变形)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17206 Accepted: ...
- HUST 4681 String (DP LCS变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 题目大意:给定三个字符串A,B,C 求最长的串D,要求(1)D是A的字序列 (2)D是B的子序列 ...
- uva 10453 dp/LCS变形
https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...
- poj 1080 (LCS变形)
Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i ...
- POJ 1080( LCS变形)
题目链接: http://poj.org/problem?id=1080 Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K ...
- 九度OJ 1016 火星A + B 未AC版,整型存储不下
#include <iostream> #include <string.h> #include <sstream> #include <math.h> ...
- UVA.10192 Vacation (DP LCS)
UVA.10192 Vacation (DP LCS) 题意分析 某人要指定旅游路线,父母分别给出了一系列城市的旅游顺序,求满足父母建议的最大的城市数量是多少. 对于父母的建议分别作为2个子串,对其做 ...
- UVA.10066 The Twin Towers (DP LCS)
UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...
随机推荐
- sass安装
第一步:下载ruby http://www.ruby-lang.org/zh_cn/downloads/ 第二步:安装ruby http://www.ruby-lang.org/zh_cn/docum ...
- SharePoint Designer 2013 连接 Office 365 必需安装2个SP
第一个: 32位电脑安装链接:http://www.microsoft.com/downloads/details.aspx?FamilyId=278a31eb-0cf9-4b30-a670-9c9d ...
- android的消息提示(震动与提示音)
protected AudioManager audioManager; protected Vibrator vibrator; audioManager = (AudioManager)getSy ...
- JSPatch来更新已上线的App中出现的BUG(超级详细)
JSPatch的作用是什么呢? 简单来说:(后面有具体的操作步骤以及在操作过程中会出现的错误) 1.iOS应用程序上架到AppStore需要等待苹果公司的审核,一般审核时间需要1到2周.虽然程序在上架 ...
- iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条
网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...
- Android版本和API Level对应关系
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API ...
- CentOS7 安装Nginx
由于需要,这段时间学一点“nginx”.关于nginx就不介绍了,http://wiki.nginx.org/Main有非常详细的介绍.安装等. 安装软件我习惯到官网下载源码,http://nginx ...
- TCP连接建立和终止小结
TCP连接建立(三次握手) 如图: 请求端发送一个SYN到服务器的相应端口,以及初始序号ISN 服务器发送包含服务器的初始序号的SYN作为应答,同时确认序号设置为客户的ISN+1 客户将确认序号设置为 ...
- freeswitch 使用mysql替换默认的sqlite
转自 80000hz.com freeswitch 使用mysql替换默认的sqlite No Reply , Posted in 默认分类 on January 14, 2014 目标使用mysql ...
- JQuery 滚动轮播
css: *{margin: 0;padding: 0;}body{font-size: 12px;line-height: 24px;text-algin: center; }a{color: #f ...