上一页 1 ··· 4 5 6 7 8 9 10 11 下一页
摘要: 遇上文本处理的话,基本上都是先读取文件,再逐行读取文本,再进行文本分割。1、读取文本使用头文件#include ,包含ifstream-输入流,ofstream-输出流,iostream-输入输出流三种类型的文件流。ifstream iFile;int a;iFile.open(filename);//ifstream默认以读方式打开//do your jobiFile>>a;iFile.close();2、逐行读取char buf[300000];while (!iFile.eof()){// find the end of the file iFile.getline(buf, 阅读全文
posted @ 2013-04-10 10:03 iyjhabc 阅读(4872) 评论(0) 推荐(0) 编辑
摘要: 一、后缀树其实是把一个单词所有的后缀都加上的一棵经过合并简化的字典树(trie tree)。如"mississip",其实就是在字典树中插入了"mississip","ississip","ssissip",sissip","issip","ssip","sip","ip","p"。这棵字典树已经可以完成后缀树的功能,只是它的空间复杂度极高。二、后缀树就是把上面字典树中不同的分支合并成字符串三、如何在O(N 阅读全文
posted @ 2013-04-03 16:12 iyjhabc 阅读(949) 评论(0) 推荐(0) 编辑
摘要: 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash表,是的,在具有定位查找上具有O(1)的常量时间。但hash table需要使用巨大的内存空间,显然在处理大数据时会显得力不从心。 bitmap可以有效地节省内存的使用,它的思想其实就是用1bit来代替一个index(通常是一个unsigned int)。比如我要对{1,5,7,2}这四个byte类型的数字做排序,该怎么做呢?我们知道byte是占8个bit位,其实我们可以将数组中的值作为bit位的key,value用”0,1“来标识该key是否出现过?下面看图:从图中我们精彩的看到,我们的数组值都已经作为byte中的key了,... 阅读全文
posted @ 2013-03-30 16:35 iyjhabc 阅读(200) 评论(0) 推荐(0) 编辑
摘要: 第八十一题:1、求数组中大于左边所有元素,小于右边素有元素的数。只能用一个额外数组。思路:从左往右遍历先把max记录在额外数组中。从右往左遍历记录当前min在一个变量中。对比min和max和当前即可。//复杂度O(n),2次迭代,使用1个数组void helper(int *a,int n){ int nowmax=a[0],nowmin=a[n-1]; int max[n]; for(int i=0;i<n;++i){ if(a[i]>nowmax)nowmax=a[i]; max[i]=nowmax; } for(int i=n-1... 阅读全文
posted @ 2013-03-29 10:59 iyjhabc 阅读(312) 评论(0) 推荐(0) 编辑
摘要: 第一题:把二分查找树转换为升序排序的双向链表。不能新建节点,只能改变树的指针。二分查找树:左小于父,右大于父。中序遍历就是升序遍历。中序遍历递归法:void showMidTree(BSTreeNode *pRoot){ if(pRoot!=NULL) { showMidTree(pRoot->m_pLeft); cout<<pRoot->m_nValue<<endl; showMidTree(pRoot->m_pRight); }}思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左 子树转换成一个排好序的... 阅读全文
posted @ 2013-03-28 09:40 iyjhabc 阅读(464) 评论(0) 推荐(0) 编辑
摘要: 第十一题:求二元树中两个节点最远的距离方法:使用递归函数求左右子树的深度。左子树深度+右子树深度=最大距离。int BinaryTreeNode::getDeep(int floor){//(也是形参传递参数,返回值传递结果) int leftDeep=this->m_pLeft!=NULL ? this->m_pLeft->getDeep(floor+1) : floor; int rightDeep=this->m_pRight!=NULL ? this->m_pRight->getDeep(floor+1) : floor; return leftDe 阅读全文
posted @ 2013-03-28 09:36 iyjhabc 阅读(272) 评论(0) 推荐(0) 编辑
摘要: 第二十一题:输入m、n,打印出1~n中所有和等于m的组合(01背包问题)递归法:void printSolution(int *flag){ for(int i=1;i<100;++i) if(flag[i]!=0)cout<<i<<" "; cout<<endl;}void bao(int m,int n,int *flag){ if(m<1 || n<1)return; if(n>m)n=m; if(n==m){//end , print it flag[n]=1; printSolution(flag); . 阅读全文
posted @ 2013-03-28 09:34 iyjhabc 阅读(204) 评论(0) 推荐(0) 编辑
摘要: 第三十六题:比赛排名void match(int w[][8],int *order,int *result,int n){ memcpy(result,order,n*sizeof(int)); int round=n; while(round>0){ for(int i=0,j=0;i<round;i+=2,++j){ if(w[result[i]][result[i+1]]==result[i])swap(i,j,result); else swap(i+1,j,result); }... 阅读全文
posted @ 2013-03-28 09:32 iyjhabc 阅读(252) 评论(0) 推荐(0) 编辑
摘要: 第四十三题:二叉树的非递归前中后序遍历前序遍历:void preorderShowTreeIterative(BSTreeNode *pRoot){ stack<BSTreeNode*> buf; buf.push(pRoot); while(!buf.empty()){ BSTreeNode *p=buf.top(); buf.pop(); cout<<p->m_nValue<<endl; if(p->m_pRight!=NULL)buf.push(p->m_pRight);//right first i... 阅读全文
posted @ 2013-03-28 09:30 iyjhabc 阅读(203) 评论(0) 推荐(0) 编辑
摘要: 第五十一题:输出所有和为n的连续正数序列方法1:从1遍历到n/2,算出所有sum,如果sum刚好等于n则输出,效率极低。方法2:用一个变量b标志连续序列的开头,e表示结尾。如果序列的sum<n,则e++;如果sum>n,则b++。复杂度O(n)void constSumSequence(int n){ int b=1,e=1,sum=1,bound=n/2+1; while(e<=bound){ if(sum==n){ for(int k=b;k<=e;++k)cout<<k<<" "; cout<<endl; . 阅读全文
posted @ 2013-03-28 09:28 iyjhabc 阅读(196) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 下一页