
算法笔记
石前有座桥
学生
展开
-
优先队列
#include//priority_queueq 大顶堆 #include//priority_queue,greater >q 小顶堆 using namespace std; int main(){ int n,x,i; while(scanf("%d",&n)!=EOF){ priority_queue,greater >q;//空格 for(i=0;i<n;i++){原创 2017-05-24 12:36:51 · 178 阅读 · 0 评论 -
memset按照字节赋值
memset按照字节赋值0 -1的二进制代码(补码)为 全0 和 全1所以最好使用fill对其他值赋初值#include<stdio.h>#include<math.h>#include<string.h>//memsetint main(){ int a[2]; memset(a,1,sizeof(a));//memset按照字节赋值 pr...原创 2018-03-05 20:23:00 · 335 阅读 · 0 评论 -
大整数乘法
描述求两个不超过200位的非负整数的积。输入有两行,每行是一个不超过200位的非负整数,没有多余的前导0。输出一行,即相乘后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。样例输入12345678900 98765432100样例输出1219326311126352690000参考别人的:#include<stdlib.h> #include<...转载 2018-03-12 14:14:41 · 1636 阅读 · 0 评论 -
大整数减法
参照算法笔记P1741065. A+B and C (64bit) (20)#include#includestruct bign{ int d[1000]; int len;};void init(bign &a){ int i; a.len=0; for(i=0;i<1000;i++){ a.d[i]=0; }}bign change(char转载 2017-07-25 19:59:07 · 285 阅读 · 0 评论 -
计算时间差
参照算法笔记P98甲级1016. Phone Bills (25)void get_ans(int on, int off, int& time, int& money) { temp = rec[on]; while(temp.dd < rec[off].dd || temp.hh < rec[off].hh || temp.mm < rec[off]转载 2018-01-09 15:37:00 · 175 阅读 · 0 评论 -
to_string
string to_string (int val);devc++上貌似用不了 pat上可以#include using namespace std;int main() { int a, b; cin >> a >> b; string s = to_string(a + b); int len = s.length();原创 2017-12-28 15:05:16 · 527 阅读 · 0 评论 -
二分查找
参照算法笔记P126Shopping in Mars (25)Find Coins (25)#include//普通二分 int binarySearch(int A[],int left,int right,int x){//[0,n-1] int mid; while(left<=right){ mid=(left+right)/2; if(A[转载 2017-08-03 20:40:32 · 195 阅读 · 0 评论 -
大整数加法
参照算法笔记P172#include#includestruct bign{ int d[1000]; int len;};void init(bign &a){ a.len=0; int i; for(i=0;i<1000;i++){ a.d[i]=0; }}bign change(char *s){ bign a; init(a); a.len=转载 2017-07-23 19:23:42 · 286 阅读 · 0 评论 -
并查集
http://blog.youkuaiyun.com/lyw_321/article/details/71372497原创 2017-08-24 18:22:26 · 314 阅读 · 0 评论 -
堆
参照浙大MOOC数据结构void Swap( ElementType *a, ElementType *b ){ ElementType t = *a; *a = *b; *b = t;} void PercDown( ElementType A[], int p, int N ){ /* 改编代码4.24的PercDown( MaxHeap H, int p )转载 2017-08-25 18:20:22 · 268 阅读 · 0 评论 -
C++字符串输入
参照算法笔记P75char str[1000];cin.getline(str,100);string str;getline(cin,str);转载 2017-08-02 20:40:00 · 176 阅读 · 0 评论 -
筛选素数
参照算法笔记P162首先确定2是素数,那么2的倍数一定不是素数,标记它们。3未被标记,则3是素数,那么那么3的倍数一定不是素数,标记它们。4已被标记,不是素数。5未被标记,则5是素数,那么5的倍数一定不是素数,标记它们。。。。const int maxn = 1000001;int prime[maxn], num = 0;bool p[maxn] =转载 2017-06-22 14:23:58 · 230 阅读 · 0 评论 -
字符串哈希
参照算法笔记P109先假设字符串均由大写字母A~Z构成。在这个基础上,不妨把A~Z视为0~25,这样就把26个大写字母对应到了26进制中。接着,按照将26进制转化为10进制的思路,由进制的转换结论可知,在进制转换过程中,得到的10进制肯定是唯一的,由此便可实现将字符串映射为整数的需求(注意:转换成的最大整数位(int)(pow(26,len)-1),len为字符串长度)int转载 2017-06-09 10:14:29 · 680 阅读 · 0 评论 -
树的高度
int getheight(node T){ if(T){ int HL=getheight(T->left); int HR=getheight(T->right); int maxH=HL>HR?HL:HR; return maxH+1; } else{ return 0; } }原创 2017-05-21 16:14:21 · 288 阅读 · 0 评论 -
1066. Root of AVL Tree (25)
时间限制100 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueAn AVL tree is a self-balancing binary search tree. In an AVL tree, the heig转载 2017-05-31 17:29:54 · 280 阅读 · 0 评论 -
04-树7 二叉搜索树的操作集 (30分)
本题要求实现给定二叉搜索树的5种常用操作。函数接口定义:BinTree Insert( BinTree BST, ElementType X );BinTree Delete( BinTree BST, ElementType X );Position Find( BinTree BST, ElementType X );Position FindMin( BinTree BST原创 2017-05-15 15:31:48 · 388 阅读 · 0 评论 -
dijkstra+dfs模板
算法笔记P384.甲级1018.void dijkstra(int s){ int i,newp=s; mark[s]=1; dis[s]=0; while(1){ for(i=0;i<=n;i++){ if(mark[i]==0&&dismap[newp][i]<inf){ if(dis[i]>dis[newp]+dismap[newp][i]){原创 2017-05-08 18:30:53 · 306 阅读 · 0 评论 -
静态链表模板
参照算法笔记P263.甲级1052//定义静态链表struct Node{ int address; typedname data; int next; XXX;//结点的某个性质 }; //初始化for(i=0;i<maxn;i++){ node[i].XXX=0;//设置一个正常情况下达不到的数字 } //修改XXXint p=begin转载 2017-05-16 14:44:38 · 290 阅读 · 0 评论 -
LCA查找最近公共祖先
参考大佬的思路//AC code/* 1:根据中序,先序建树 2: 用 findnode( )查看待查询节点是否存在 3:若待查询节点是存在,用 LCA( )查找最近公共祖先其中LCA有三种: u,v都在左子树或右子树,或者u,v分别在左子树和右子树,因此可采取如下步骤: (1)::判断当前遍历的节点是否为空,为空返回null, (2)::节...转载 2019-04-03 18:21:45 · 221 阅读 · 0 评论