随笔分类 - 数据结构--树
1
摘要:链接捣鼓了一下午。。按堆建树 写完交 返回TLE。。数据不大 感觉不会超了 无奈拿了数据来看什么奇葩数据会超 发现数据跟我输出不一样 看了好久才明白理解错题意了给出的字符串有两个标签 按前一个来建二叉搜索树 按后一个建堆 搜索树直接排序 然后依次插入右子树 若当前值比前一个的值大 就在与其父亲比较 总之放到合适位置。这样的树叫笛卡尔树 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include10 #include11 #include12 using na
阅读全文
摘要:1439路漫漫其修远兮~手抄一枚splay树 长长的模版。。关于spaly树的讲解 网上很多 随手贴一篇貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找、删除特别快 因为都有序 而且平衡~看题很容易想到用线段树做 不过数太大了 离散化嘛 你肯定这么想 不过这题真不好离...
阅读全文
摘要:http://dongxicheng.org/structure/trietree/http://hi.baidu.com/piaoshi111/item/ad5f7c12ca63f38889a95622http://poj.org/problem?id=3630刚开始套字典树 将字符串末尾标记 查询到末尾时标记 动态的 超时了 参考着别人的写了个静态的 过了 172MSView Code 1 #include 2 #include 3 #include 4 using namespace std; 5 struct node 6 { 7 int count; 8 int n...
阅读全文
摘要:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2128有些忘记了View Code 1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct tree 4 { 5 struct tree *l,*r; 6 int data; 7 }tt; 8 int k; 9 void creat(tt *head,int a)10 {11 tt *p;12 p = (tt *)malloc(sizeof(tt));13 ..
阅读全文
摘要:与1182一个思路 并查集加偏移量 不过比1182好做一些View Code 1 #include 2 int father[100005], r[100005]; 3 void init(int n) 4 { 5 int i; 6 for(i = 1 ; i <= n ; i++) 7 { 8 r[i] = 0; 9 father[i] = i;10 }11 }12 int find(int x)13 {14 if(x!=father[x])15 {16 int pre = father[x]...
阅读全文
摘要:把两个节点的关系转换为对根节点的关系的比较View Code 1 #include <stdio.h> 2 int father[50001] ,r[50001]; 3 void init(int n) 4 { 5 int i; 6 for(i = 1 ; i <= n ; i++) 7 { 8 father[i] = i; 9 r[i] = 0;10 }11 }12 int find(int x)13 {14 if(x == father[x])15 return father[x];16 else17...
阅读全文
摘要:作者:Dong | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址:http://dongxicheng.org/structure/union-find-set/1、 概述并查集(Disjoint set或者Union-find set)是一种树型的数据结构,常用于处理一些不相交集合(Disjoint Sets)的合并及查询问题。2、 基本操作并查集是一种非常简单的数据结构,它主要涉及两个基本操作,分别为:A. 合并两个不相交集合B. 判断两个元素是否属于同一个集合(1) 合并两个不相交集合(Union(x,y))合并操作很简单:先设置一个数组Father[x],表示
阅读全文
摘要:001_02时间限制:1000 ms | 内存限制:65535 KB描述合并数列给定两个非降序排列的数列A,B。数列中元素的值为int, 元素个数不超过1,000。将两个已排序的数列合并成一个非升序的数列输出。[Any Problem: trueshlqsh@gmail.com ||dengdong1211@sse.buaa.edu.cn|| oeddyo@gmail.com]输入输入有3m+1行。第一行为测试数据的组数m。下面的3m分别为m组测试数据,每组测试数据的第一行包括a,b两个数,表示接下来两行分别有a个数和b个数,接下来数列A B占两行,每个数列中的元素用空格隔开。输出输出有m行,
阅读全文
摘要:数据结构实验之求二叉树后序遍历和层次遍历Time Limit: 1000MS Memory limit: 65536K题目描述已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历。输入输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。输出每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列示例输入2abdegcfdbgeafcxnliulnixu示例输出dgebfcaabcdefglinuxxnuli探索了解钻研的过程是痛苦的 结果是享受
阅读全文
摘要:题目描述 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。输入输入一个长度小于50个字符的字符串。输出输出共有4行:第1行输出中序遍历序列;第2行输出后序遍历序列;第3行输出叶子节点个数;第4行输出二叉树深度。对于严老师书上写的那些 表示 看得真的很吃力 换了本书 网上搜搜 东凑西凑 总算把这个题写出来了 1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct btnode 4 { 5 char
阅读全文
摘要:函数名: strcat 功 能: 字符串拼接函数 用 法: char *strcat(char *destin, char *source); 程序例:#include <string.h> #include <stdio.h>int main(void) { char destination[25]; char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland); strcat(destination, bl
阅读全文
摘要:n圆括号和方括号,其嵌套的顺序随意。如([]())或[([ ][ ])]等为正确的匹配;而[( ])或([ ]( )或 ( ( ) ) )均为错误的匹配。 这里只介绍了()【】其实加上{}是一样的性质 看代码 1 #include<stdio.h> 2 #include<string.h> 3 int sw(char str) 4 { 5 if(str == '(') 6 return 1; 7 else 8 if(str == ')') 9 return 9;10 else11 if(str == '['...
阅读全文
摘要:后缀式求值的方法参见我的另一篇文章 把运算符变成表达式 1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int top = 0,i,k,s,num[50]; 6 char str; 7 while(scanf("%c", &str),str!='#') 8 { 9 if(str>='0'&&str<='9')10 num[++top] = str-48;11 else12 {13 switch(s
阅读全文
摘要:1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int n,m,num[10000],x,y,z,d,f = 1,i,r = 0; 6 char str[10]; 7 scanf("%d", &m); 8 for(i = 1 ; i <= m;i++) 9 scanf("%d", &num[i]);10 scanf("%d",&n);11 r = m;12 while(n--)13 {14 scanf(&quo
阅读全文
摘要:1 #include<stdio.h> 2 int main() 3 { 4 int n,m,i,r,top = 0; 5 char stack[100]; 6 scanf("%d%d", &n,&m); 7 while(n) 8 { 9 top++;10 stack[top] = n%m;11 n = n/m;12 }13 while(top!=0)14 {15 printf("%d", stack[top]);16 top--;17 }1...
阅读全文
摘要:1 /* 将中缀表达式(a+b)转换为后缀表达式(ab+)的算法思想: 2 ·当读到数字直接送至输出队列中 3 ·当读到运算符t时, 4 a.将栈中所有优先级高于或等于t的运算符弹出,送到输出队列中; 5 b.t进栈 6 ·读到左括号时总是将它压入栈中 7 ·读到右括号时,将靠近栈顶的第一个左括号上面的运算符全部依次弹出,送至输出队列后,再丢弃左括号。 8 9 运用后缀表达式进行计算的具体做法: 10 ·建立一个栈S 11 ·从左到右读后缀表达式,读到数字就将...
阅读全文
摘要:1 #include<iostream.h> 2 #include<malloc.h> 3 struct node 4 { 5 int data; 6 struct node *next; 7 }; 8 void traverse_list(struct node *head) 9 {10 int i = 1;11 struct node *p;12 p = head->next;13 while(p)14 {15 if(i != 1)16 cout<<' ';17 cou...
阅读全文
摘要:题目描述n个人想玩残酷的死亡游戏,游戏规则如下: n个人进行编号,分别从1到n,排成一个圈,顺时针从1开始数到m,数到m的人被杀,剩下的人继续游戏,活到最后的一个人是胜利者。请输出最后一个人的编号。输入输入n和m值。输出输出胜利者的编号。示例输入5 3示例输出4提示第一轮:3被杀第二轮:1被杀第三轮:5被杀第四轮:2被杀 1 #include<iostream.h> 2 #include<malloc.h> 3 struct mon 4 { 5 int num; 6 struct mon *next; 7 }; 8 struct mon *creat(int n) 9
阅读全文
摘要:1 #include<stdio.h> 2 #include<malloc.h> 3 struct stu 4 { 5 int data ; 6 struct stu *next ; 7 }; 8 struct stu *creat(int n) 9 {10 int i;11 struct stu *head,*p;12 head = (struct stu *)malloc(sizeof(struct stu));13 head->next = NULL;14 for(i = 1 ; i <= n ;i++)15 {16 ...
阅读全文
摘要:1 #include<stdio.h> 2 #include<malloc.h> 3 struct stu 4 { 5 int data ; 6 struct stu *next ; 7 }; 8 struct stu *creat(int n) 9 {10 int i;11 struct stu *head,*p,*tail;12 head = (struct stu *)malloc(sizeof(struct stu));13 head->next = NULL;14 tail = head; 15 for(i = 1 ...
阅读全文
1

浙公网安备 33010602011771号