上一页 1 ··· 6 7 8 9 10 11 12 下一页
摘要: 本文内容与《算法导论》中字符串匹配章节相关并部分摘录。 常用的字符串匹配算法有朴素字符串匹配算法,Rabin-Karp算法,利用有限自动机进行字符串匹配和KMP算法等。前面两种比较简单,重点是后面两种。利用有限自动机进行字符串匹配 假设要对文本字符串T进行扫描,找出模式P的所有出现位置。这个方法可以通过一些办法先对模式P进行预处理,然后只需要对T的每个文本字符检查一次,并且检查每个文本字符所用时间为常数,所以在预处理建好自动机之后进行匹配所需时间只是Θ(n)。 假设文本长度为n,模式长度为m,则自动机将会有0,1,...,m这么多种状态,并且初始状态为0。先抛开自动机是怎样计算出来的... 阅读全文
posted @ 2013-11-26 16:16 Jolin123 阅读(3380) 评论(0) 推荐(1) 编辑
摘要: 题目传送门:1049. Mondriaan思路: 找规律的一道水题。 假设长度为n的有f(n)种画法。容易求出f(0)=1,f(1)=2,f(2)=7... 找出递推式f(n)=3f(n-1)+f(n-2)-f(n-3)(推导了半节英语课。。。),打好表之后输入输出搞定。代码: 1 #include 2 using namespace std; 3 4 const int MAX=1000001; 5 int f[MAX]; 6 7 int main(){ 8 int testcases,l; 9 cin>>testcases;10 f[0]=1;11 ... 阅读全文
posted @ 2013-11-19 00:06 Jolin123 阅读(155) 评论(0) 推荐(0) 编辑
摘要: 题目传送门:1002. Anti-prime Sequences思路: 本题使用了dfs深度优先搜索(回溯法),用递归的形式实现。 一开始见到3s的时间竟然傻乎乎的跑去用回溯法按照字典序生成全排列,然后一个个的去判断是否是anti-prime,直接被TLE拍死。 后来才突然醒悟在搜索到某个数字时可以利用2到degree的相邻数字和不是素数这个性质进行大量剪枝才搞定。 这里选择了vector作为容器是为了能同时实现对元素的快速下标访问于在某一特定位置的增删元素。 用一个vector result存储生成的anti-prime sequence,然后将n~m这些数字一个一个的插进resu... 阅读全文
posted @ 2013-11-16 22:08 Jolin123 阅读(451) 评论(0) 推荐(0) 编辑
摘要: 题目地址:Sicily 1012. Stacking Cylinders思路: 最低层如果有n个圆,则一共会有n层,其中最高一层有1个。用n个数组记录n层圆的坐标,一开始输入底层的坐标,排序之后再不断利用下一层的坐标算出当前一层的坐标,知道最高层就行。对于每一个圆心坐标,可以用支撑它的下面两个圆心坐标通过几何计算方法算出。注意输出的格式。代码: 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct Point{ 8 double x,y; 9 };10 11 void get_a_... 阅读全文
posted @ 2013-11-14 16:05 Jolin123 阅读(336) 评论(0) 推荐(0) 编辑
摘要: 题目地址:1156. Binary tree思路: 简单的一道二叉树相关的题目,题目会给出一颗树现在的形态,然后用前序遍历这棵树的节点输出数据即可。 每个节点会输入该节点的identifier,有点类似于key,然后输入该节点储存的数据(char类型)和该节点左右子节点的identifier。 这里直接用开了几个数组存储数据,对于一个节点identifier,将其数据存进data[identifier]里,左右子树分别存进left[identifier],right[identifier]中。输出的时候需要找到树的根root,所以可以开一个bool数组来找根,初始化全为false,读进的... 阅读全文
posted @ 2013-11-12 14:27 Jolin123 阅读(390) 评论(0) 推荐(0) 编辑
摘要: 题目:1934. 移动小球思路: 想了很久,即使用链表在插入和删除元素的时候比较快,但用来查找删除插入的位置的时间也太长。 看了别人的代码之后顿时开窍,用两个数组分别记录每一个球的左边和右边球的编号,这样就可以实现数组对元素的快速访问。非常高明而简单的方法!感觉有点类似于基于数组实现的双端链表。代码: 1 #include 2 using namespace std; 3 4 int lefts[500001]; //lefts[i] stores the lefts ball's number of the ball i. 5 int rights[500001]; //rights 阅读全文
posted @ 2013-11-10 20:49 Jolin123 阅读(585) 评论(5) 推荐(0) 编辑
摘要: 题目:DescriptionFarmer John has decided to take a family portrait of some (maybeall) of the cows. In order to take the portrait, FJ has arrangedall N (1 1 1 0 1 0 1 1+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+... 阅读全文
posted @ 2013-11-10 12:12 Jolin123 阅读(498) 评论(0) 推荐(0) 编辑
摘要: 题目:DescriptionDr lee cuts a string S into N pieces,s[1],…,s[N].Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”}, the string S could be “aabac”,”aacab”,”abaac”,…Y 阅读全文
posted @ 2013-11-08 13:01 Jolin123 阅读(382) 评论(0) 推荐(0) 编辑
摘要: 题目:ConstraintsTime Limit: 1 secs, Memory Limit: 256 MBDescriptionThe programming language Better And Portable Code (BAPC) is a language for working with lists of integers. The language has two built-in functions: ‘R’ (reverse) and ‘D’ (drop).The function ‘R’ reverses its input list, and ’D’ drops th 阅读全文
posted @ 2013-11-04 18:39 Jolin123 阅读(285) 评论(0) 推荐(0) 编辑
摘要: 题目:DescriptionGiven a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:• Emergency 911• Alice 97 625 999• Bob 91 12 54 26In this case, it’s not possible to call Bob, because the central would 阅读全文
posted @ 2013-11-04 00:15 Jolin123 阅读(418) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 下一页