数据结构
一只会旅行的猫
这个作者很懒,什么都没留下…
展开
-
链表的各种实现
1.声明typedef struct LNode{int data;struct LNode *next;}LNode,*LinkList;2.建立1)从表尾向前建立L=(LinkList)malloc(sizeof(LNode));L->next=NULL;for(i=length;i>0;i--){p=(LinkList)malloc原创 2013-05-05 16:38:05 · 589 阅读 · 0 评论 -
【KMP(循环节)】hdu 3746 Cyclic Nacklace
http://acm.hdu.edu.cn/showproblem.php?pid=3746 #include #include #include using namespace std;const int NM=100005;int next[NM];char str[NM];void get_next(int len){ int i,j;原创 2013-11-26 18:23:06 · 457 阅读 · 0 评论 -
【树状数组(逆序数)】hdu 1394 Minimum Inversion Number
http://acm.hdu.edu.cn/showproblem.php?pid=1394题意:求一个循环数组的最小逆序数#include #include #include #include using namespace std;const int NM=5005;int a[NM],c[NM];int lowbit(int x){ return x&(原创 2014-01-31 19:29:06 · 608 阅读 · 0 评论 -
【并查集】hdu 1856 More is better
#include #include #include using namespace std;const int NM=10000005;int father[NM],rank[NM],mmax;int Find(int x){ /*while(x!=father[x]) x=father[x]; return x; 超时*/ /*if(x!=father[x])原创 2014-04-21 19:45:24 · 401 阅读 · 0 评论 -
树状数组
1.树状数组图解设节点编号为x,那么这个节点管辖的区间为2^k(其中k为x二进制末尾0的个数)个元素。Cn = A(n – 2^k + 1) + ... + An,如:C4=A1+A2+A3+A4,其中4=(100)-2,末尾有两个0,2^k=2,优势:查询和修改复杂度都为log(n)的数据结构,对大型数据处理很快速,而且比线段树简洁2.三种基本函数A.求2^k原创 2014-01-25 15:59:08 · 326 阅读 · 0 评论 -
【线段树(lazy)】hdu 1698 Just a Hook
http://acm.hdu.edu.cn/showproblem.php?pid=1698题意:对n个hook进行区间3种涂色操作,初始时颜色都为1,求总和#include #include #include using namespace std;const int NM=100005;int T[NM*4],vau;void Build(int lf,int原创 2014-02-07 13:28:50 · 523 阅读 · 0 评论 -
【线段树(lazy)】poj 2777 Count Color
http://poj.org/problem?id=2777题意:把长度为L的板子分成L份,标签从1~L,开始时板子的颜色都为1,然后对板子的某一区间涂色,求在某一区间内有多少种不同的颜色?(A可能>B)分析:区间更新+区间查找#include #include #include using namespace std;const int NM=100005;i原创 2014-02-09 15:22:28 · 431 阅读 · 0 评论 -
【线段树(线段离散化+lazy)】poj 2528 Mayor's posters
http://poj.org/problem?id=2528题意:有不同长度([x,y])的海报被贴在一个长墙上,求有多少个海报可以被看到(只要有一部分漏在外面就算)分析:离散化的时候是对线段而不是对点离散,否则会错误,因为即使是[1,1]也表示一个线段,比如下例:liri11013610清除相同项原创 2014-02-09 20:14:52 · 702 阅读 · 0 评论 -
【线段树】hdu 2795 BillBoard
http://acm.hdu.edu.cn/showproblem.php?pid=2795题意:有一个h*w的布告板,每次粘贴1*wi的公告,按照 先考虑最上方,后最左边 的方式粘贴,求该公告贴到了第几行#include #include #include using namespace std;const int NM=200005;int w,T[NM*4];原创 2014-02-03 21:53:20 · 475 阅读 · 0 评论 -
【KMP(循环节)】poj 2406 Power Strings(外:hdu 1358 Period)
http://poj.org/problem?id=2406#include #include #include using namespace std;const int NM=10000005;int next[NM];char str[NM];void get_next(){ int i,j,len; len=strlen(str); i=0;next原创 2014-05-12 20:01:18 · 483 阅读 · 0 评论 -
【KMP】hdu 1867 A + B for you again(外:hdu 2594 Simpsons’ Hidden Talents)
http://acm.hdu.edu.cn/showproblem.php?pid=2594题意:求字符串1的前缀与字符串2的后缀的最大匹配分析:用字符串1匹配字符串2 ,直到字符串2 结尾两种情况: 1)len1>len2,直接匹配 2)len1#include #include #include using name原创 2013-11-03 16:29:09 · 499 阅读 · 0 评论 -
【字典树+DFS】hdu 1298 T9
http://acm.hdu.edu.cn/showproblem.php?pid=1298原创 2014-05-18 21:35:36 · 505 阅读 · 0 评论 -
【并查集+字典树】poj2513 Colored Sticks
http://poj.org/problem?id=2513分析:形成欧拉通路,即:无向图每个点的度数为偶数或有2个奇数,Trie+并查集欧拉回路的定义;图G的一个回路,若它恰通过G中每条边一次,则称该回路为欧拉(Euler)回路,具有欧拉回路的图称为欧拉图(简称E图)。类似:NYOJ42 一笔画问题#include#include#includeusing name原创 2013-04-28 20:13:54 · 615 阅读 · 0 评论 -
【并查集】hdu 1272 小希的迷宫
#include#include#includeusing namespace std;const int NM=100005;int father[NM];bool vis[NM],flag;int Find(int x){ int t,p1=x; while(p1!=father[p1]) p1=father[p1]; while(x!=p1) { t=fath原创 2014-05-08 17:51:50 · 466 阅读 · 0 评论 -
【KMP】hdu 1686 Oulipo(外:hdu 2203 亲和串)
http://acm.hdu.edu.cn/showproblem.php?pid=1686#include #include #include using namespace std;const int NM=1000005;int len1,len2,ans,next[10005];char str[NM],sub[10005];void get_nextval()原创 2013-11-03 16:32:48 · 684 阅读 · 0 评论 -
【字典树】poj 1204 Word Puzzles(外:poj Shortest Prefixes)
http://poj.org/problem?id=1204分析:对要查找的单词建立字典树,然后对table进行顺序遍历,查找方向:A~H(A:north)#include #include #include using namespace std;const int NM=1005;char str[NM][NM],sw[NM][NM];int a[NM][3],n,m原创 2014-01-23 16:26:40 · 527 阅读 · 0 评论 -
【字典树】hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846分析:字典树,注意保存每一个子串#include#include#includeusing namespace std;const int NM=10005;char str2[22];struct Node{ Node *next[26]; //代表该点的26个指针 in原创 2013-07-21 01:07:19 · 603 阅读 · 0 评论 -
【字典树】hdu 3460 Ancient Printer
http://acm.hdu.edu.cn/showproblem.php?pid=3460题意:有一台打印机,它只能每次输入一个单词并打印,输入下一个单词时,必须把第一个单词删掉(但如果两个单词有相同的前缀,只需删除不同的部分即可)分析:注意最后一个单词可以不删除,我的想法是相同单词的前缀只用打印、删除一次,其余部分*2(如:三个单词aB、aC、aD,a是前缀,那么ans=2*a+2原创 2014-01-24 13:16:39 · 657 阅读 · 0 评论 -
【拓扑排序】hdu 3342 Legal or Not
http://acm.hdu.edu.cn/showproblem.php?pid=3342#include #include #include using namespace std;const int NM=105;int a[NM][NM],indu[NM],n;bool Tuopu(){ int i,j,k,ling; memset(indu,0,sizeof(原创 2014-05-17 18:47:28 · 417 阅读 · 0 评论 -
【拓扑排序】poj 1094 Sorting It All
http://poj.org/problem?id=1094原创 2014-05-17 17:57:41 · 410 阅读 · 0 评论 -
【线段树】poj 3264 Balanced Lineup(外:hdu 1754 I Hate It)
http://poj.org/problem?id=3264题意:求某区间内最大值和最小值的差值#include #include #include using namespace std;const int NM=50005;struct Tree{ int mmin,mmax;}T[NM*4];void Build(int lf,int rg,int原创 2014-02-02 22:32:28 · 510 阅读 · 0 评论 -
【树状数组(二维)】poj 1195 Mobile phones(外:hdu 2642 Stars)
http://poj.org/problem?id=1195分析:裸二维树状数组#include #include #include using namespace std;const int NM=1030;__int64 a[NM][NM];int lowbit(int x){ return x&(-x);}void add(int x,int y,i原创 2014-01-27 23:10:04 · 590 阅读 · 0 评论 -
【单调队列】poj 2823 Sliding Windows
http://poj.org/problem?id=2823#include #include using namespace std;const int NM=1000005;int a[NM],que[NM];int n,len;void Min_win(){ int tail,head,i; head=1; tail=0; for(i=1;i<=len;i+原创 2013-11-17 18:24:36 · 393 阅读 · 0 评论 -
hdu 1237 简单计算器
http://acm.hdu.edu.cn/showproblem.php?pid=1237分析:看来对栈的应用还是不熟练,汗颜啊#include #include #include using namespace std;int main(){ double s[205],t; char f; int i,j; while( cin >> t ) { f=g原创 2013-10-23 23:29:43 · 469 阅读 · 0 评论 -
【栈】hdu 1022 Train Problem I(外:poj 1383 Rails)
http://acm.hdu.edu.cn/showproblem.php?pid=1022#include#include#includeusing namespace std;int main(){ stacks1; char a[20],b[20]; int flag[25],n,i,j,k; while(cin>>n) {原创 2013-10-27 20:38:25 · 472 阅读 · 0 评论 -
二叉树的各种实现
1.声明及建立typedef struct BiTNode{ char data; struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;int node,leafnode,depth;void CreateBiTree(BiTree &T)//先序建立二叉树{ char ch; scanf("%c",&ch); if(ch=原创 2013-05-05 16:36:34 · 551 阅读 · 0 评论 -
哈夫曼树的实现
1.声明及建立typedef struct{ unsigned int weight; unsigned int parent,lchild,rchild;}HTNode,*HuffmanTree;typedef char **HuffmanCode;struct Node{ int i,wei; char c;}node[20];priority_原创 2013-04-22 11:37:49 · 729 阅读 · 0 评论 -
【单调队列】hdu 4193 Non-negative Partial Sums
http://acm.hdu.edu.cn/showproblem.php?pid=4193题意:每次把最后一个元素移动到队首,求这n个数的所有前缀数组的和都>=0分析:这货就是一个循环数组,用单调队列找出n个前缀的最小的和,只要判断这个最小的和是否>=0即可#include #include using namespace std;const int NM=20000原创 2013-11-17 18:31:56 · 727 阅读 · 0 评论 -
【单调队列】hdu 3415 Max Sum of Max-K-sub-sequence
http://acm.hdu.edu.cn/showproblem.php?pid=3415题意:求k长度的最大子串和,单调队列原文参考:http://www.cppblog.com/baby-fly/archive/2010/08/04/122213.aspx?Pending=true#include#include#includeusing namespace原创 2013-04-08 21:06:49 · 617 阅读 · 0 评论 -
【字典树】hdu 1671 Phone List
http://acm.hdu.edu.cn/showproblem.php?pid=1671分析:字典树#include#include#includeusing namespace std;const int NM=10005;char str[NM][15];int flag;struct Node{ Node *next[10]; int id; Node()原创 2013-07-20 23:27:59 · 700 阅读 · 0 评论 -
【线段树】poj 2828 Buy Tickets
http://poj.org/problem?id=2828题意:每个新加入的人,会加入到第pos个人后面,求最后的队列顺序分析:逆向考虑,比如这组测试数据:posvau077151133269第四个人插入后,他的位置就固定了:XX69X;同理,第三个人插入的时候,第四个人对他的位置不会原创 2014-02-05 13:54:57 · 576 阅读 · 0 评论 -
【树状数组(逆序数)】hdu 3743 Frosh Week
http://acm.hdu.edu.cn/showproblem.php?pid=3743分析:就是求逆序数,要注意离散化处理,题目信息也不说行清楚#include #include #include #include using namespace std;const int NM=1000005;int a[NM],c[NM];struct Node{ i原创 2014-01-29 19:11:32 · 614 阅读 · 0 评论 -
【树状数组(二维)】poj 2155 Matrix
http://poj.org/problem?id=2155分析:插入区间,求单点#include#include#includeusing namespace std;const int NUM=1010;int a[NUM][NUM],N;int lowbit(int x){ return x&(-x);}int sum(int x,int y){原创 2014-02-05 14:24:27 · 471 阅读 · 0 评论 -
【树状数组(三维)】hdu 3584 Cube
http://acm.hdu.edu.cn/showproblem.php?pid=3584分析:三维树状数组,按照二维猜出来的,统计改变的次数即可判断每个点状态#include #include #include using namespace std;const int NM=105;int a[NM][NM][NM];int lowbit(int x){原创 2014-01-31 17:14:52 · 538 阅读 · 0 评论 -
【树状数组】nyoj 116 123 士兵杀敌
nyoj 116 分析:单点插入,求区间和#include#include#include#includeusing namespace std;const int NUM=1000010;int a[NUM],N;int lowbit(int t) //2^k{ return t&(t^(t-1));}int sum(int t){ int原创 2014-01-29 19:36:07 · 803 阅读 · 0 评论 -
【树状数组】hdu 1541 Stars
http://acm.hdu.edu.cn/showproblem.php?pid=1541题意:按照y轴升序,然后x轴升序输入星星的坐标,求从0~N-1 level的星星各有多少个?(level判断:一个星星的左下方有多少颗星星,即:y2分析:因为是按y轴升序输入的,所以不用考虑高的问题,直接比较x轴来压缩成一维树状数组#include #include #include原创 2014-01-29 16:39:44 · 528 阅读 · 0 评论 -
位运算
百科:http://baike.baidu.com/view/65101.htm#3如题,一直纠结在函数参数时“&”的功能,写个栈感受了一下 a.void ClearStack1(SqStack &S){ S.top=S.base; n1=S.top-S.base;} b.void ClearStack2(SqStack S){原创 2013-05-05 16:37:06 · 494 阅读 · 0 评论 -
【字典树】hdu 4287 Intellident IME
http://acm.hdu.edu.cn/showproblem.php?pid=4287分析:赶脚这样的数据:1 123adsss答案:0效率比较(hash、字典树):字典树(加了判断,是否为整个单词):#include #include#includeusing namespace std;const int NM=5005;原创 2014-01-22 13:59:40 · 539 阅读 · 0 评论