topcoder SRM 594 DIV2 AstronomicalRecordsEasy
此题主要考查的是求最长公共子序列
设A[i]:A[j] = a:b = ac:bc B[ii]:B[jj] = c:d = ac:ad ,
如果A[i]:A[j] = B[ii]:B[jj]则bc= ad,所以A乘以B的倍数,B乘以A的倍数,则A与B所求的序列必然是一样的,即求A与B的最长公共子序列
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class AstronomicalRecordsEasy{
public:
int minimalPlanets(vector<int> A, vector<int> B){
int lenA = A.size(),lenB = B.size(),res = lenA + lenB;
for(int i = ; i < lenA; ++ i ){
for(int j = ; j < lenB; ++ j){
int a = A[i],b = B[j];
vector<vector<int> > dp(lenA+,vector<int> (lenB+,));
for(int ki = ; ki <= lenA; ++ ki ){
for(int kj = ; kj <= lenB; ++ kj){
dp[ki][kj] = max(max(dp[ki-][kj],dp[ki][kj-]),dp[ki-][kj-] + (b*A[ki-] == a*B[kj-]) );
}
}
res = min(res, lenA+lenB - dp[lenA][lenB]);
}
}
return res;
}
};
topcoder SRM 594 DIV2 AstronomicalRecordsEasy的更多相关文章
- Topcoder Srm 673 Div2 1000 BearPermutations2
\(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...
- Topcoder Srm 671 Div2 1000 BearDestroysDiv2
\(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- Topcoder srm 632 div2
脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...
- topcoder SRM 628 DIV2 BracketExpressions
先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配 #include <vector> #include <string> #include <list> # ...
- topcoder SRM 628 DIV2 BishopMove
题目比较简单. 注意看测试用例2,给的提示 Please note that this is the largest possible return value: whenever there is ...
- Topcoder SRM 683 Div2 B
贪心的题,从左向右推过去即可 #include <vector> #include <list> #include <map> #include <set&g ...
- Topcoder SRM 683 Div2 - C
树形Dp的题,根据题意建树. DP[i][0] 表示以i为根节点的树的包含i的时候的所有状态点数的总和 Dp[i][1] 表示包含i结点的状态数目 对于一个子节点v Dp[i][0] = (Dp[v] ...
- Topcoder SRM 626 DIV2 SumOfPower
本题就是求所有连续子数列的和 开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列 循环遍历即可 int findSum(vector <int> array) { ; ; ...
随机推荐
- 使用Discuz关键词服务器实现PHP中文分词
不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的.在项目中,我们只需要一个函数即可方便地进行分词.关键词提取.以下是根据Discuz!在线分词服务API写的函数 ...
- object-c 基本数据类型
1.基本数据类型 int float double char 布尔类型 枚举类型 2.对象类型和id类型 就是类类型或协议所声明的指针类型. id类型可以表示任何类型,一般只表示 ...
- 【转】tomcat下部署 solr 5.3.1
本文转自:http://blog.csdn.net/lianghyan/article/details/49467207 solr下载: http://lucene.apache.org/solr/d ...
- Android Studio中的Module,Facet
详细内容请参看 http://www.jetbrains.com/idea/webhelp/facet.html 以及 http://www.jetbrains.com/idea/webhelp/an ...
- iOS 推荐学习__bridge等ARC知识的好资料
请下载 iOS5 by Tutorials!写得很好的!
- 101 个 MySQL 的调节和优化的提示(根据实际情况调整,有些已经不适用)
英文原文:101 Tips to MySQL Tuning and Optimization ( July 12, 2011)翻译:http://www.oschina.net/translate/1 ...
- MySQL 全文搜索支持, mysql 5.6.4支持Innodb的全文检索和类memcache的nosql支持
背景:搞个个人博客的全文搜索得用like啥的,现在mysql版本号已经大于5.6.4了也就支持了innodb的全文搜索了,刚查了下目前版本号都到MySQL Community Server 5.6.1 ...
- 【python】zip()函数
来源:http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tupl ...
- 【USACO】namenum
//开始傻×了 受题目形容的误导 一心想生成所有可能的 字符串组合 之后查找非常慢 //听了同学的 将5000个dict里的字符串 转换成char型数组(不能直接用int 会越界)直接用输入的数据对着 ...
- HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)
Fibonacci again and again Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...