画圆的沙滩

亦简亦美

2011年3月21日 #

最大和子数组

摘要: 编程之美2.14, 2.15节。这个问题在编程珠玑中的算法章有很深入的分析。可以试解下题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=44ht 阅读全文

posted @ 2011-03-21 22:23 acmaru 阅读(178) 评论(0) 推荐(0) 编辑

最长递增子序列

摘要: 编程之美2.16节。较容易想到的方法是O(n^2)的。这里有一个细节没有使用到,假设记录下所有长度的子序列结果,那么我们可以保证长度为n的子序列尾元素一定大于等于长度为n-1的子序列尾元素,否则的话,我们可以从长度为n的子序列中获取一个n-1的子序列,将之前的结果覆盖。利用这个性质,我们可以获得一个O(nlogn)的算法:template<class It>vector<typename iterator_traits<It>::value_type>lis(It first, It last) { typedef iterator_traits<It 阅读全文

posted @ 2011-03-21 19:18 acmaru 阅读(201) 评论(0) 推荐(0) 编辑

树的重建

摘要: 任给一棵二叉树合法的两种遍历结果,就可以将它重建出来。事实上,不需重建这棵树,而进行隐式的遍历。可以尝试下面的这道题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489对于编程之美3.9节的这个问题及其扩展问题,简单的实现代码如下:对扩展问题1,显然是不唯一的,考虑所有节点都标注a的极端情况即知。实现方面,需要把下面的find查找修改为直到没有匹配为止,然后分别尝试重建。template & 阅读全文

posted @ 2011-03-21 18:07 acmaru 阅读(174) 评论(0) 推荐(0) 编辑

树的直径

摘要: 编程之美3.8。这题让我想起了另外一道著名的题目:lowest common root. 两题都是典型的树之上的DP。下面为实现:struct Node { int v; Node *left, *right;};size_t diameter(Node* root, size_t& depth) { depth = 0; if (!root) return 0; size_t ldepth, rdepth; size_t ldiameter = diameter(root->left, ldepth); size_t rdiameter = diameter(root-> 阅读全文

posted @ 2011-03-21 17:02 acmaru 阅读(154) 评论(0) 推荐(0) 编辑

树的分层遍历

摘要: 是的,这个使用BFS可以很简洁地实现。不过编程之美3.10节有两个扩展问题。对于第二个问题,用BFS就不那么简便了,相反递归在这里更合适。另一方面,对于初始问题,不一定要使用queue,虽然空间上略微浪费一些(最大为其两倍),但实现代码可以同第二个扩展问题有部分共享。下面的代码,注意swap的使用。初看以为这里会很浪费时间,实际上STL的swap实现是O(1)的。另,关于第二个扩展问题,可以联系到下面的题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103& 阅读全文

posted @ 2011-03-21 16:30 acmaru 阅读(194) 评论(0) 推荐(0) 编辑

最大公约数

摘要: 编程之美2.7节。解法三即stein算法。下面是其迭代式实现。template<class T>T gcdStein(T a, T b) { for(T r = 1;;) { if (!a) return r *= b; if (!b) return r *= a; T o1 = 0; while (!(a&1)) a>>=1, ++o1; T o2 = 0; while (!(b&1)) b>>=1, ++o2; r <<= min(o1, o2); if (a == 1 || b == 1) return r; if (a & 阅读全文

posted @ 2011-03-21 15:13 acmaru 阅读(136) 评论(0) 推荐(0) 编辑

相交的链表

摘要: 下面的代码尝试解决带环和不带环的链表是否相交以及如果相交,交点的位置两个问题。见编程之美3.6节,代码应当同时解决了后面的两个扩展问题。Node* commonEnd(Node* a, Node* b) { if (!a || !b) return 0; Node *p1 = a, *p2 = a; do { if (p2->next) p2 = p2->next; if (p2->next) p2 = p2->next; if (p1->next) p1 = p1->next; } while (p1 != p2); Node *q1 = b, *q2 = 阅读全文

posted @ 2011-03-21 13:44 acmaru 阅读(176) 评论(0) 推荐(0) 编辑

相交的蚂蚁

摘要: 编程之美4.7节。参考下题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1655int main() { vector<int> positions; int t, len, n; cin>>t; while (t--) { cin>>len>>n; positions.clear(); while (n--) *back_inserter(po 阅读全文

posted @ 2011-03-21 12:11 acmaru 阅读(199) 评论(0) 推荐(0) 编辑

2011年3月18日 #

哑谜,回文和暴力之美

摘要: 暴力搜索是一个有趣的东西。至少刘汝佳是这么认为的。编程之美的4.10节就是典型的暴力题。虽然作者将其难度定义为一颗星,但却不能因此认为这个类型的问题就是那么容易的,很多可能需要一些有创造力的想法。不妨试试下面这几道题:Island of Logic:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=533Meta-loopless sort:http://uva.onlinejudge.org/in 阅读全文

posted @ 2011-03-18 11:56 acmaru 阅读(199) 评论(0) 推荐(0) 编辑

2011年3月12日 #

关于max queue的另一个简单写法

摘要: 编程之美的第3.7节。作者使用了堆和max stack来实现。其实,第二种方法是可以借鉴到queue上面去的。实现上的差别是push的复杂度不是O(1),而pop则是O(1)的。template<class T, class C = deque<T> >class max_queue : queue<T, C> { deque<T> mq_;public: const T& max() const { return const_cast<max_queue<T, C>*>(this)->max(); } T& 阅读全文

posted @ 2011-03-12 12:40 acmaru 阅读(222) 评论(0) 推荐(0) 编辑

导航