摘要: 这一题是我写的第一个树形dp题。View Code #include <iostream>#include <cstdio>#include <memory.h>#include <algorithm>using namespace std;const int INF = 6005;int l[INF],r[INF];int d[INF],w[INF];bool vis[INF],in[INF];int dp[INF][2];void add(int x,int y){ if(d[x]==0)l[x]=y; else r[d[x]]=y; d[x 阅读全文
posted @ 2012-07-27 16:27 HNU_AlienForce 阅读(127) 评论(0) 推荐(0)
摘要: 这一题就是要求一组数据的中位数。思路有几种:1、快排。再找出中间的数就行了,但是复杂度为nlog(n);2、利用求中位数的最优算法是O(N)的Select算法。但是我怀疑这个算法的复杂度应该不是O(N)的。下面就我自己的思路来理解一下select算法。先贴一下代码:#include <iostream>#include <cstdio>#include <algorithm>#include <iterator>using namespace std;int a[10001];int find_min_N(int s,int e,int n){ 阅读全文
posted @ 2012-07-24 15:12 HNU_AlienForce 阅读(157) 评论(0) 推荐(0)
摘要: hdu 3336 Count the stringhttp://acm.hdu.edu.cn/showproblem.php?pid=3336其实这一题只要理解了kmp算法的核心,就能迎刃而解了。利用kmp+dp;Matrix67详解kmp:http://www.matrix67.com/blog/archives/115/kmp的核心就是要求出p数组(即next数组),而p【i】数组有一个特点:它的前j个数和最后j个数要相同。利用这个性质就可以得出递归表达式:dp[i]=dp[p[i]]+1;(+1是因为它本身肯定存在);View Code #include <iostream> 阅读全文
posted @ 2012-06-26 19:47 HNU_AlienForce 阅读(266) 评论(0) 推荐(0)
摘要: View Code 1 #include <iostream> 2 #include <stdlib.h> 3 #include <memory.h> 4 5 using namespace std; 6 const int INF = 1000000000; 7 int d[2005][1005]; 8 int min(int a,int b){ 9 return a<b?a:b;10 }11 int cmp(const void *a,const void*b){12 return *(int *)a - *(int *)b;13 }14 int 阅读全文
posted @ 2012-05-05 14:15 HNU_AlienForce 阅读(144) 评论(0) 推荐(0)
摘要: 题目大意:给你两个字符串,问:第一个字符串按入栈出栈规则,能否达到第二个字符串,输出所有的方法,i表示入栈,o表示出栈。解题思路:DFS(深度优先搜索)注意事项:1、要注意回溯2、要理清思路View Code 1 #include <iostream> 2 #include <stdlib.h> 3 #include <memory.h> 4 5 using namespace std; 6 char op[110]; 7 char ch1[110],ch2[110]; 8 char st1[110]; 9 int len,k;10 11 void dfs( 阅读全文
posted @ 2012-04-27 11:15 HNU_AlienForce 阅读(170) 评论(0) 推荐(0)