随笔分类 -  数据结构

摘要:LCA模板题,用的方法是转化为RMQ问题来求解,各种WA,折腾了一整天,哎~~~ 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <map> 6 #include <vector> 7 using namespace std; 8 const int maxn=100000+100; 9 int pos[maxn],se[maxn*2],d[2*maxn][20];10 int e[. 阅读全文
posted @ 2013-05-28 16:37 LJ_COME!!!!! 阅读(124) 评论(0) 推荐(0)
摘要:多路归并+优先队列的使用#include <iostream> #include <cstdio> #include <queue> #include <algorithm> using namespace std; const int maxn=2000+10; int a[110][maxn],b[maxn],n,m; struct item { int s,b; item(int s,int b):s(s),b(b) { } }; bool operator < (item a,item b) { return a.s>b.s; 阅读全文
posted @ 2013-05-01 17:31 LJ_COME!!!!! 阅读(193) 评论(0) 推荐(0)
摘要:trie的简单应用#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn=10000*10+10; int trie[maxn][11]; int val[maxn],tot; char str[10000+10][15]; int Insert(char *s) { int len=strlen(s); int i,u=0,loc; for(i=0;i<len;i++) { loc=s[i]-'0'; .. 阅读全文
posted @ 2013-04-17 17:19 LJ_COME!!!!! 阅读(114) 评论(0) 推荐(0)
摘要:这道题目思维+基础,好题目拓扑排序以后看到sij就要想到连续和转化为前缀和之差的思想,通过表格得到两两前缀和的大小关系,然后通过拓扑排序得到前缀和的大小序列,确定每个前缀和的大小,最后得到结果#include <iostream> #include <cstdio> #include <string.h> using namespace std; const int maxn=11; int e[maxn][maxn],b[maxn],in[maxn],sol[maxn]; int n,loc; void topo()//基于栈的topo排序 { int i 阅读全文
posted @ 2013-03-01 17:46 LJ_COME!!!!! 阅读(159) 评论(0) 推荐(0)
摘要:优先队列或者说是堆得应用,最暴力的想法就是没输出一个最小值,更新,然后再排序,若用快排,时间效率为O(knlgn),但不需要对所有的排次序,所以就用到最小堆#include <iostream> #include <queue> #include <string> using namespace std; typedef struct node { int n,p,r; }ar; bool operator<(ar a,ar b) { if(a.p==b.p) return a.n>b.n; else return a.p>b.p; } p 阅读全文
posted @ 2012-10-18 23:42 LJ_COME!!!!! 阅读(87) 评论(0) 推荐(0)
摘要:#include <iostream> using namespace std; const int maxn=8000+10; struct node { int l,r,len; }tree[maxn*3]; void Create(int p,int l,int r) { tree[p].l=l; tree[p].r=r; tree[p].len=r-l+1; if(l!=r) { int mid=(l+r)/2; Create(2*p+1,l,mid); Create(2*p+2,mid+1,r); } } int find(int p,int am) { ... 阅读全文
posted @ 2012-10-18 23:25 LJ_COME!!!!! 阅读(125) 评论(0) 推荐(0)
摘要:并查集水题#include <iostream> using namespace std; const int maxn=50010; int tot,m,n; struct node { int rank; int data; int parent; }t[maxn]; void init() { for(int i=1;i<=n;i++) { t[i].data=i; t[i].rank=1; t[i].parent=i; } } int find(int p) { if(p!=t[p].parent) t[p].parent=find(t[p].parent... 阅读全文
posted @ 2012-10-15 09:12 LJ_COME!!!!! 阅读(88) 评论(0) 推荐(0)
摘要:裸哈希,感觉oj的数据弱啊,如果N很大的话,内存就不够用,可能我没想明白?#include <stdio.h> #include <string.h> const int maxn=16000010; char s[maxn]; int li[300]; short hash[maxn]; int tot; int main() { int N,NC; scanf("%d%d",&N,&NC); scanf("%s",s); int sl=strlen(s); int t=1,i,j; memset(hash,0, 阅读全文
posted @ 2012-10-14 21:04 LJ_COME!!!!! 阅读(116) 评论(0) 推荐(0)
摘要:此题用的是并查集的思路,时间很慢,应该有更好的思路,但以目前的知识只能做成这样#include <iostream> using namespace std; const int maxn=10001; struct UFSTree { int data; int parent; int rank; }t[maxn]; int N; void init() { int i; for(i=0;i<N;i++) { t[i].data=i; t[i].parent=i; t[i].rank=1; } } void Union(int p,int c) { t[c].p... 阅读全文
posted @ 2012-10-14 16:03 LJ_COME!!!!! 阅读(81) 评论(0) 推荐(0)
摘要:树状数组,因为此是求二维数组的区间的和,所以将其扩展为二维树状数组#include <stdio.h>#include <memory.h>const int maxn=1100;int c[maxn][maxn];int n;int LowBit(int x){ return x&(x^(x-1));}void Update(int x,int y,int a){ for(int i=x;i<=n;i+=LowBit(i)) for(int j=y;j<=n;j+=LowBit(j)) c[i][j]+=a;}int getSum(int x,in 阅读全文
posted @ 2012-10-14 15:59 LJ_COME!!!!! 阅读(87) 评论(0) 推荐(0)
摘要:欧拉回路+并查集(判断图的联通)+Tire树(快速查找字符串,并且记录相应点的度数)Tire树相关知识http://zh.wikipedia.org/wiki/Trie#include <iostream> using namespace std; const int maxc = 26; const int maxn = 500001; struct TrieNode { int key; TrieNode * child[maxc]; TrieNode() { key=-1; memset(child,0,sizeof(child)); } }; int count[ma... 阅读全文
posted @ 2012-10-12 19:47 LJ_COME!!!!! 阅读(133) 评论(0) 推荐(0)
摘要:题不难,数据结构之BST,就是输入纠结了一会#include <iostream> #include <cstring> using namespace std; const int maxn=30; struct Tnode { char data; Tnode * lchild; Tnode * rchild; }; Tnode * root; void insert(Tnode * p,char c) { if(root==NULL) { root=new Tnode; root->data=c; root->lchild=NULL; root-> 阅读全文
posted @ 2012-10-09 23:17 LJ_COME!!!!! 阅读(106) 评论(0) 推荐(0)
摘要:线段树,顺序存储实现#include <stdio.h> #include <string.h> const int maxx=32000; const int maxn=15000; int l[3*(maxx+1)],h[3*(maxx+1)],w[3*(maxx+1)]; int N; int ans[maxn]; void Create(int t,int s,int f) { l[t]=s;h[t]=f; if(s<f) { int mid=(s+f)/2; Create(2*t+1,s,mid); Create(2*t+2,mid+1,f); } } 阅读全文
posted @ 2012-09-29 14:22 LJ_COME!!!!! 阅读(94) 评论(0) 推荐(0)
摘要:第一道并查集,47ms ,还是很慢啊,不过是绞尽脑汁,调试了n次,做出来的,思路还是挺清晰地#include <iostream> using namespace std; const int maxn=30010; int parent[maxn]; int amount[maxn]; int rank[maxn]; int n,m; void init() { for(int i=0;i<n;i++) { parent[i]=i; amount[i]=1; rank[i]=0; } } int find(int t) { if(parent[t]!=t) retur... 阅读全文
posted @ 2012-09-23 20:16 LJ_COME!!!!! 阅读(99) 评论(0) 推荐(0)
摘要:注意的情况比较多,尤其是空树这一种#include <iostream> #include <stdlib.h> using namespace std; const int maxn=100; struct node { int s,f; node* next; };//在此数据结构采用链表,方便后续遍历算法 int indexNode[maxn];//用于构造索引的数组 int v[maxn]; node *head; int totNode;//节点的总数 void visitNode(int temNode) { node* p=head->next; v 阅读全文
posted @ 2012-09-18 19:00 LJ_COME!!!!! 阅读(261) 评论(0) 推荐(0)
摘要:知识点:栈的应用语法经验,主函数外的函数中的指针变量不能付给主函数中的指针,因为函数调用完后就会释放内存,赋值相当于没赋#include<iostream>using namespace std;const int maxn=110;void trans(char *exp,char *atexp);int oc( char *s);int main(){ int t=1,jud; char exp[maxn]; char atexp[maxn]; while(cin.getline(exp,maxn)) { trans(exp,atexp); jud=oc(atexp); cha 阅读全文
posted @ 2012-09-15 20:50 LJ_COME!!!!! 阅读(159) 评论(0) 推荐(0)