随笔分类 -  搜索

hdu 2874 Connections between cities
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2874裸LCA,处理森林的无向图。思路:给每棵树一个唯一的编号,查LCA的时候,判断一下是不是在同一棵树下就行了。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:36777216&qu 阅读全文

posted @ 2012-06-19 15:35 aigoruan 阅读(182) 评论(0) 推荐(0)

hdu 2586 How far away ?
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2586也是一道很裸的LCA,只不过卡了一下栈,要自己开大点栈才能过(c++)。思路:选1为根节点,先得到从根节点出发到每个点的距离,这样任意两个点的距离为:dis(u)+dis(v)-2*dis(lca(u,v)).View Code #include<stdio.h>#include<string.h>#include<iostream>#include<stdlib.h>#include<vector>#include<algorithm& 阅读全文

posted @ 2012-06-19 11:58 aigoruan 阅读(205) 评论(0) 推荐(0)

poj 1470 Closest Common Ancestors
摘要:http://poj.org/problem?id=1470裸LCA:读入数据很诡异。View Code #include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<algorithm>#include<stdlib.h>using namespace std;const int maxn = 1000;struct nd{ int to,next;}edge[maxn*2];vector<int>qes[maxn] 阅读全文

posted @ 2012-06-19 10:45 aigoruan 阅读(177) 评论(0) 推荐(0)

poj 1330 tarjan lca
摘要:http://poj.org/problem?id=1330tarjan 求解lca主要利用并差集的想法:首先遍历树,从叶子节点开始向上合并成一棵棵的子树,然后子树并子树,就成了一棵树了。查找是在合并的时候进行的,exp:u是s,t的lca,先从u节点进入s,把s并到u下面,然后发现t没有被访问,退到u,再进入t,同样把t并到u下面,发现s被访问过了,那么s的lca也就是s,t的lca了,也就是并差集里的f[s]。当然,f[s]会变的:假设当前f[s] = u; f[u] = u; 当u并到v的时候,也就是f[u]=v; 相应的f[s] = v。这也就是tarjan求解lca的关键方法。这个题 阅读全文

posted @ 2012-06-19 09:58 aigoruan 阅读(244) 评论(0) 推荐(0)

hdu 2242
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2242题目大意:给一个教室群,问能不能把这些教室群分成两部分,并且使两部分之间的权值差最小(每一个教室都有一定的权值);思路:先用双联通进行缩点,因为那些双向连通的教室肯定是分不开的,所以要把它们缩成一个点。之后再重新建图,遍历一次树!需要注意一点这道题的数据包含重边,需要考虑!!View Code #include<stdio.h>#include<string.h>#include<stdlib.h>const int N = 10005;struct nd{ int 阅读全文

posted @ 2012-06-18 14:34 aigoruan 阅读(151) 评论(0) 推荐(0)

uestc 1717
摘要:http://www.acm.uestc.edu.cn/problem.php?pid=1717&cid=169题意:先给一棵树,有n个结点(n<10^5),相邻的结点有距离;如果在这棵树上添加一条边后,查询两个点的距离是否改变近了,如果变近了,输出变近了多少。否则输出0。这个题是一个很好的练dfs的题,比赛的时候是郭化权做的,自己去弄线段树去了,不过线段树也是做不出来的~~~~。赛后想了想,觉得不是很难,结果弄了两天才出来,悲剧呀。还是郭化权的思路慎密。思路:在树上面加入一条边后,一定会有一个环出现的。仔细想想之后,最后要处理的都会归到这个环上的,因为只有要环里面的才可能出现距 阅读全文

posted @ 2012-06-12 19:15 aigoruan 阅读(372) 评论(0) 推荐(0)

hdu 2328 Corporate Identity
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2328题意:求多个字符串的最长公共子串,若有多个输出字典序最小。思路:先进行后缀数组操作。然后二分子串长度,根据height和sa数组的值,判断当前长度是否合适。当然,这里面还有hash映射的。View Code #include<stdio.h>#include<string.h>#include<stdlib.h>const int maxn = 910000;int wn[maxn],wa[maxn],wb[maxn],wv[maxn],as[maxn],sa[max 阅读全文

posted @ 2012-06-07 21:01 aigoruan 阅读(157) 评论(0) 推荐(0)

hdu 1711 Number Sequence
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1711题意:给两个数字序列,判断第一个是否包含了第二个数列。思路一(正解):直接kmp;思路二(YY):求出第一个序列的前缀和,在第一个序列中取和第二个序列等长的子序列,如果这个子序列的和与第二个序列的和一样,才进行匹配算法。这个算法可以过不了一些特殊的数据。但在很短的时间内过了。View Code #include<stdio.h>#include<string.h>long long as[1000005],bs[10005],cs[1000005];int check(long 阅读全文

posted @ 2012-06-07 15:49 aigoruan 阅读(190) 评论(0) 推荐(0)

xmu 1337 后缀数组 + 暴力匹配
摘要:http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1337题意:在一个串中求出一个长度为n的、重复了m次的子串(不能重叠)。思路:一直以为正解是后缀数组加其它高效的搜索方法。一水才知道,就是纯后缀数组+暴力匹配。先用后缀数组求出 sa,height两个数组的值,当height[i]=n时,取出当前子串,进行暴力匹配。囧~~~View Code #include<stdio.h>#include<string.h>#include<stdlib.h>const int maxn = 101000;int wn[m 阅读全文

posted @ 2012-06-07 13:44 aigoruan 阅读(244) 评论(0) 推荐(0)

pku 1743
摘要:View Code #include<stdio.h>#include<string.h>#include<stdlib.h>const int maxn = 20005;int wn[maxn],wa[maxn],wb[maxn],wv[maxn],a[maxn],sa[maxn],rank[maxn],height[maxn];char r[maxn];int n;int Max(int x,int y){return x>y?x:y;}int Min(int x,int y){return x>y?y:x;}int cmp(int *r,i 阅读全文

posted @ 2012-06-07 12:05 aigoruan 阅读(163) 评论(0) 推荐(0)

hdu 1403 简单后缀数组应用
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1403题意:给两串字符,求最长的公共子串。仔细想下就会想到后缀数组了。View Code #include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;const int maxn = 220005;int wn[maxn],wa[maxn],wb[maxn],wv[maxn],a[maxn],sa[maxn],rank[maxn],height[maxn];char r[maxn];int 阅读全文

posted @ 2012-06-07 09:59 aigoruan 阅读(158) 评论(0) 推荐(0)

hdu 1195 Open the Lock 双广
摘要://题目大意:求解开4位密码锁的最少步数.//<变化一次:加1;减1;邻位交换>思路:简单bfs能过,时间很高,双bfs好点。简单bfsView Code #include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <queue>#include <algorithm>using namespace std;struct nd{ int num,st;};int hash[10000];int s,t;i 阅读全文

posted @ 2012-06-05 23:58 aigoruan 阅读(187) 评论(0) 推荐(0)

hdu3172 Virtual Friends
摘要:很有意思的一道题:两个人成为朋友,他们就有关系,题目给出m句话,每一句是两个人,表示两个成为朋友,然后叫你求这与这两个人有关系的集合中有多少个人。以为stl过不了,结果过了,直接用map映射就好了,如果不在同一集合,就暴力合并(时间卡在这里)。注意一下这个题目的输入。View Code #include<stdio.h>#include<string.h>#include<string>#include<iostream>#include<map>#include<set>using namespace std;const 阅读全文

posted @ 2012-06-05 16:28 aigoruan 阅读(191) 评论(0) 推荐(0)

hdu 3779
摘要:简单记忆化搜索View Code #include<stdio.h>#include<string.h>int as[1005],bs[1005],cs[2005],ok,n,m;char hash[1005][1005];void dfs(int x,int y,int z){ if(hash[x][y]||ok)return; if(z==n+m)ok = 1; hash[x][y] = 1; if(as[x]==cs[z]&&x<n)dfs(x+1,y,z+1); if(ok)return; if(bs[y]==cs[z]&& 阅读全文

posted @ 2012-04-27 16:19 aigoruan 阅读(141) 评论(0) 推荐(0)

hdu 1978
摘要:记忆化搜索View Code #include<stdio.h>#include<string.h>int n,m;int as[105][105],hash[105][105];int dfs(int x,int y){ int i,j; if(x==n||y==m)return 0; if(hash[x][y]) return hash[x][y]; for(i = 0; i <= as[x][y]; ++ i) for(j = 0; j <= as[x][y]; ++ j) if((i+j)&&(i+x<n)&&( 阅读全文

posted @ 2012-04-27 16:02 aigoruan 阅读(207) 评论(0) 推荐(0)

hdu 1428
摘要:记忆化搜索View Code #include<stdio.h>#include<string.h>#include<iostream>#include<queue>using namespace std;struct nd{ int x,y;};const long long inf = 0xfffffff;long long as[100][100],hash[100][100],dp[100][100];long long dx[] = {1,-1,0,0};long long dy[] = {0,0,1,-1};long long n;v 阅读全文

posted @ 2012-04-27 16:01 aigoruan 阅读(169) 评论(0) 推荐(0)

ACM的第一乐章--DFS(入门)
摘要:DFS以一棵树的形状呈现的满是美的搜索结构,对于任何类型的搜索,DFS均可以暴力解之,然而给它好的剪枝,会使得代码简单而富有效率。A Knight's Journeyhttp://poj.org/problem?id=2488题意:给一个p*q的棋盘,按象棋里的马的‘日’字规则把所有的点仅且只走一次... 阅读全文

posted @ 2012-03-28 23:46 aigoruan 阅读(574) 评论(0) 推荐(0)

hdu 4068
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=4068挺水的一道题目,无奈比赛的时候做不出来啊。题意:三国杀1vs1,给出英雄之前的约束关系,无论对手如何安排英雄pk顺序,是否都有机会赢。思路:把映射关系弄好,stl求出全排就好做了。View Code #include<stdio.h>#include<iostream>#include<algorithm>#include<string>#include<set>#include<map>#include<vector> 阅读全文

posted @ 2012-03-23 13:50 aigoruan 阅读(153) 评论(0) 推荐(0)

hust 1017
摘要:题意:给定一个01矩阵,要求选择一些行,使每一列有且仅有一个1.Dancing Links论文中讲到的一题,并以此为基础使另外一些类型的题目转化为此种精确区间覆盖模型用DLX解决。算法描述如下:深搜:1、如果矩阵为空,得到结果,返回2、从矩阵中选择一列,以选取最少元素的列为优化方式3、删除该列及其覆盖的行4、对该列的每一行元素:删除一行及其覆盖的列,5、进行下一层搜索,如果成功则返回6、恢复现场,跳至47、恢复所选择行用双向十字链表来维护该矩阵,方便删除与恢复,其中删除操作:R[L[i]]=L[i];L[R[i]]=R[i];恢复操作:R[L[i]]=i;L[R[i]]=i;View Code 阅读全文

posted @ 2012-03-23 13:42 aigoruan 阅读(211) 评论(0) 推荐(0)

导航