题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12707 算法决定一切,这道题目有很多方法解,个人认为这里 vexorian 给出的解法最简便,编码也最容易.而使用brute force 和 DP都比较复杂. 代码如下: #include <algorithm> #include <iostream> #include <sstream> #include <string>…
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12710 采用DFS搜索,第一次写的时候忘了加访问标志,结果状态空间呈指数增长(主要是因为有大量重复的状态),根本算不出结果,后来加入访问标志数组 v 后,就能保证不访问重复的状态的了.这道题目的启示就是使用DFS一定要记住确保不访问重复的状态,有些时候很容易就忘了这一点,导致算法失败. 代码如下: #include <algorithm> #include…
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 “先去掉这个位置,看剩下的位置能不能符合条件” 方法不太一样. 代码如下: #include <algorithm> #include <numeric> #include <iterator> #include <functional> #include &…
250 题意:有n首不同的曲子,你唱每首曲子需要花费a的时间以及一个调整的时间b,调整的时间为此首歌的曲调减去上一首歌的曲调的绝对值. 思路:我们用dp[i][k]表示前i首歌只唱k首用的最小时间花费,最后拿所有的dp[i][k]和T做比较,满足条件则选取最大的k.这样的思路是没错的,但是...没注意到一种情况,dp[i][k]只要选了就保证序列定下来了,因为取的是绝对值,序列定下来的说明和时长a没关系,所以这时候要保证abs最小,又因为是从前往后推,所以只有排序后才能保证绝对值最小,有点贪心的…
简单的题目 class KeyDungeonDiv2 { public: int countDoors(vector <int> doorR, vector <int> doorG, vector <int> keys) { ; ; i < doorR.size(); i ++){ ]+keys[] && doorG[i] <= keys[]+keys[] && doorR[i] + doorG[i] <=keys[]+…
题意:有n个等长的string(设string的长度为m),string中的字符从'A'到'Z',容许对m列执行稳定的排序操作,问说是否能通过这m种操作将这n个string调整成对应的顺序. 题解: 为了保证区间[0, n)有序,考虑最后一个操作,该操作使得某一列在区间[0, n)中是有序的,这个操作将这n个序列分成了若干组,之前的操作需要保证每一组中的序列都是有序的.操作的顺序.初始的选择以及多次同一个操作对结果没影响.…
记录dp(i, j)表示前i种卡片的排列,使得LISNumber为j的方法数. #include <iostream> #include <vector> #include <string> #include <string.h> using namespace std; typedef long long int64; ; int64 dpC[][]; int64 dpT[][]; int64 sum[]; int64 dp[][]; class LISN…
题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向图中判断环,而且要找出环的大小,后来看了解析之后才发现原来使用一个Floyd算法就搞定了,因为题目中加了很多限制,并不真的需要在一个任意的无向图中求 指定大小的环的数量.生成所有的排列组合可以使用C++ STL提供的std::next_permutation 算法,非C++使用backtrack,具体实现可以…
题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> using namespace std; string Manacher(const string &s){ string t="#"; for(char c:s) t+=c,t+="#"; int RL[t.size()]={0}; int MaxRight=…
This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string ss, consisting of lowercas…