摘要:   http://poj.org/problem?id=3630   方法一: 利用Trie可以很好的解决此类前缀问题。它有一个很大的好处就是可以边插入边判断是否是前缀。 由于号码只由0~9组合,因此Trie的数组分支也为10个,即son[10](就像当英文字典时为son[26])。 一般而言Trie的节点都在需要时被new出来,但这样无疑会浪费太多的时间。因此我们可以... 阅读全文
posted @ 2010-11-08 19:59 Allen Sun 阅读(415) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3517   方法:具体参见3750 假设此环为以m+1起始,m-1终止的n-1环,且m+1--->1,求得此环最后出队的编号 再将此编号转换成原n环的编号 注意: 此题m为起始点,k为步长。且首先出队编号m,再以步长k循环 Description Let’s play a stone removing g... 阅读全文
posted @ 2010-11-08 19:56 Allen Sun 阅读(319) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3414   方法: 该题简化之后即为求从初始状态(0,0)到终止状态(i,C)或(C,j)的最短路径 对于每个状态(i,j)存在由6种操作得到的6个邻接状态,即为图中的邻接节点 将每种操作和得到的节点状态对应上以便打印路径,即pathArr[6]和adjVertex[6]   Description ... 阅读全文
posted @ 2010-11-08 19:53 Allen Sun 阅读(450) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3321 方法: 该题要经常查询数值总是变化的区间和。 如果采取常规做法求得积累数组c[N],那么当a[i]改变时,同时需要改变c[i...N],使得每次调整都会很慢,复杂度O(N)。求和复杂度O(1) 树状数组BIT形状很像二项树,适用于对经常改变的数组快速求得区间和。 采用树状数组tree[N]的话,每次调整与求和的复杂度为O... 阅读全文
posted @ 2010-11-08 19:50 Allen Sun 阅读(506) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3278   方法: 单源无权图最短距离,即BFS 该问题只需要求得某两点间的最短距离,所以不必求得所有节点的最短距离,一旦处理了目的地点,即可返回结果   Description Farmer John has been informed of the location of a fugitive ... 阅读全文
posted @ 2010-11-08 19:46 Allen Sun 阅读(317) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3210   方法: 当n个硬币初始同面,则只能偶次翻转才能保持同面,所以答案肯定是偶数。 n为偶数时,假设1个为正,n-1个为反,两中情况同为奇数,所以只能通过奇数次翻转才能保持同面,    这与上面的情况矛盾,所以n为偶数时无解 n为奇数时,正反的个数必然是一个偶数一个奇数。假设1个为正,n-... 阅读全文
posted @ 2010-11-08 19:43 Allen Sun 阅读(403) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3176   方法: DP:将大问题转化为小问题解决。由递归写循环。 c[i][j]表示从a[1][1]开始到a[i][j]的最大和。而最终的结果为c[n][1...n]中的最大值 递归式为: c[1][1] = a[1][1] c[i][j] = max{ c[i-1][j-1], c[i-1][j] } + a[... 阅读全文
posted @ 2010-11-08 19:41 Allen Sun 阅读(279) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3126 方法: 先筛出1000~9999的所有素数,对于其中每个素数求得他在图中的邻接点。其邻接点就是所有与其有一位不同的素数。 再用BFS搜索图找到指定源节点和目标节点之间的最短路径。若不联通则输出失败   Description The ministers of the cabinet were quite ... 阅读全文
posted @ 2010-11-08 19:38 Allen Sun 阅读(402) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3087   简单的模拟题,模拟洗牌   Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by sta... 阅读全文
posted @ 2010-11-08 19:35 Allen Sun 阅读(276) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3080   方法: 将第一个字符串的所有子串枚举为pattern,匹配其他所有的字符串,找到与所有匹配的最大子串 匹配用KMP,注意到当不同pattern前缀相同时,预处理不必从头做起,因为每个next[i]之于其之前的next有关   Description The Genographic Pro... 阅读全文
posted @ 2010-11-08 19:33 Allen Sun 阅读(409) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3070 方法: divide and conquer,类似于求幂 矩阵求幂 复杂度:O(logn) | F(n+1) F(n)   | = | 1  1 |^n | F(n)   F(n-1) |   | 1  0 |   注意: 矩阵... 阅读全文
posted @ 2010-11-08 19:30 Allen Sun 阅读(514) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=3067 方法: 求交点个数,我们可以将其转化为求逆序数问题。 两线段(x1,y1)、(x2,y2)相交当且仅当(x1-x2)*(y1-y2)<0 对所有输入线段按x排序,对同x的线段按y排序。利用排序后的输入计算逆序数。 数组元素a[i]记录y为i的线段的个数。依次读入排序后的线段,每取一个y值,++a[y],并计算 区间... 阅读全文
posted @ 2010-11-08 19:27 Allen Sun 阅读(299) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=2739   方法: 埃氏筛法得到N以下的所有素数,复杂度n*lglgn 利用积累数组计算数组中连续元素的和   Description Some positive integers can be represented by a sum of one or more consecutive prim... 阅读全文
posted @ 2010-11-08 19:24 Allen Sun 阅读(568) 评论(0) 推荐(0) 编辑
摘要:   http://poj.org/problem?id=2593   Maximum Sequence 求数组两段不重叠的连续子数组的最大和 详见2479   Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should outpu... 阅读全文
posted @ 2010-11-08 19:20 Allen Sun 阅读(264) 评论(0) 推荐(0) 编辑
摘要: http://poj.org/problem?id=2499 root为(1,1),节点(a,b)的左孩子为(a+b,b),右孩子为(a,a+b)。求从root到给定节点(a,b)所走过的左右路径各为多少 方法:不断优化算法的经典例子,类似于求最大公约数TLE了几次才逐渐改进的算法:TLE-->0ms详见注释 Description BackgroundBinary trees are a... 阅读全文
posted @ 2010-11-08 19:17 Allen Sun 阅读(1073) 评论(0) 推荐(0) 编辑