├── Acwing ├── 0primaryAlgorithm │ ├── README.md │ ├── chp1 │ │ ├── bit.cpp │ │ ├── bsearch.cpp │ │ ├── difference.cpp │ │ ├── discretize.cpp │ │ ├── doublepointers.cpp │ │ ├── highPrecision.cpp │ │ ├── mergeSort.cpp │ │ ├── quickSort.cpp │ │ └── section.cpp │ ├── chp2 │ │ ├── advancedUnionfind.cpp │ │ ├── advancedheap.cpp │ │ ├── doublelink.cpp │ │ ├── hash.cpp │ │ ├── heap.cpp │ │ ├── kmp.cpp │ │ ├── monotonic.cpp │ │ ├── singleLink.cpp │ │ ├── stackqueue.cpp │ │ ├── stl.cpp │ │ ├── strhash.cpp │ │ ├── trie.cpp │ │ ├── unionfind.cpp │ │ └── xor.cpp │ ├── chp3 │ │ ├── Floyd.cpp │ │ ├── bellman_ford.cpp │ │ ├── bfsGraph.cpp │ │ ├── bipartiteGraphI.cpp │ │ ├── bipartiteGraphII.cpp │ │ ├── dfsGraph.cpp │ │ ├── dijkstraList.cpp │ │ ├── dijkstraMatrix.cpp │ │ ├── heapDijkstra.cpp │ │ ├── kruskal.cpp │ │ ├── maze.cpp │ │ ├── permutation.cpp │ │ ├── prim.cpp │ │ ├── queen.cpp │ │ ├── spfa.cpp │ │ ├── spfanNegetive.cpp │ │ └── topsort.cpp │ ├── chp4 │ │ ├── IxExP.cpp │ │ ├── catalan.cpp │ │ ├── combinationI.cpp │ │ ├── combinationII.cpp │ │ ├── combinationIII.cpp │ │ ├── combinationIV.cpp │ │ ├── divisorNum.cpp │ │ ├── divisorSum.cpp │ │ ├── euler.cpp │ │ ├── exgcd.cpp │ │ ├── filterprime.cpp │ │ ├── gauss.cpp │ │ ├── gcd.cpp │ │ ├── getdivisor.cpp │ │ ├── getprime.cpp │ │ ├── isprime.cpp │ │ ├── nim.cpp │ │ ├── nimsg.cpp │ │ ├── qmi.cpp │ │ └── qmiinverse.cpp │ ├── chp5 │ │ ├── bitscount.cpp │ │ ├── complete_Pack.cpp │ │ ├── hamilton.cpp │ │ ├── lcs.cpp │ │ ├── lis.cpp │ │ ├── mondrian.cpp │ │ ├── multiple_Pack.cpp │ │ ├── packetback_Pack.cpp │ │ ├── stoneMerging.cpp │ │ ├── test.cpp │ │ ├── triangle.cpp │ │ └── zero_one_Pack.cpp │ └── chp6 │ │ ├── exp.cpp │ │ ├── huffman.cpp │ │ ├── inequalityAbsolute.cpp │ │ ├── rangeL.cpp │ │ ├── rangeM.cpp │ │ ├── rangecover.cpp │ │ ├── rangediv.cpp │ │ └── sequenceInequality.cpp ├── 1advancedAlgorithm │ ├── README.md │ └── chpS1 │ │ └── test.cpp ├── 2internship │ ├── README.md │ ├── chp1-1.cpp │ ├── chp1-2.cpp │ ├── chp1-3.cpp │ ├── chp1-4.cpp │ ├── chp2-5.cpp │ └── chp2-6.cpp ├── 3offer │ ├── README.md │ └── week1 │ │ ├── 1.cpp │ │ ├── 2.cpp │ │ ├── 3.cpp │ │ ├── 4.cpp │ │ ├── 5.cpp │ │ └── 6.cpp ├── 4index │ ├── README.md │ ├── basis.cpp │ ├── ds.cpp │ ├── index.cpp │ └── quickSort.cpp └── README.md ├── C++ ├── README.md ├── docs │ ├── C++ConCurrency.md │ ├── Cache.md │ ├── DesignPatterns.md │ ├── EffectiveC++.md │ ├── InsideTheC++ObjectModel.md │ ├── MakeBuild.md │ ├── MemoryManagement.md │ ├── Pthread.md │ └── UnitTest.md └── src │ ├── C++ConCurrency │ ├── debug │ │ ├── test.dSYM │ │ │ └── Contents │ │ │ │ ├── Info.plist │ │ │ │ └── Resources │ │ │ │ └── DWARF │ │ │ │ └── test │ │ ├── thread │ │ └── thread.dSYM │ │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── thread │ ├── thread.cpp │ ├── thread_async.cpp │ ├── thread_atomic.cpp │ ├── thread_condition1.cpp │ ├── thread_condition2.cpp │ ├── thread_creat.cpp │ ├── thread_critical_region.cpp │ ├── thread_data_sharing.cpp │ ├── thread_future.cpp │ ├── thread_mutex.cpp │ ├── thread_para.cpp │ ├── thread_pool.cpp │ ├── thread_shared_future.cpp │ ├── thread_singleton.cpp │ └── thread_unique_lock.cpp │ ├── DesignPattern │ ├── AbstractFactory.cpp │ ├── Adapter.cpp │ ├── Bridge.cpp │ ├── Builder.cpp │ ├── ChainOfResponsibility.cpp │ ├── Command.cpp │ ├── Composite.cpp │ ├── Decorator.cpp │ ├── Facade.cpp │ ├── FactoryMethod.cpp │ ├── Flyweight.cpp │ ├── Interpreter.cpp │ ├── Iterator.cpp │ ├── Mediator.cpp │ ├── Memento.cpp │ ├── Observer.cpp │ ├── Prototype.cpp │ ├── Proxy.cpp │ ├── Singleton.cpp │ ├── State.cpp │ ├── Strategy.cpp │ ├── TemplateMethod.cpp │ └── Visitor.cpp │ ├── EffectiveC++ │ ├── Item1.cpp │ ├── Item2.cpp │ ├── Item3.cpp │ └── Item47.cpp │ ├── InsideTheC++ObjectModel │ ├── constructor.cpp │ ├── ctordtor.cpp │ ├── data.cpp │ ├── function.cpp │ ├── object.cpp │ └── runtime.cpp │ ├── MemoryManagement │ ├── 0primitives_index.cpp │ ├── 10pool_alloc.cpp │ ├── 11simplified_ver_std::alloc.cpp │ ├── 11std::alloc.cpp │ ├── 12alloc_test.cpp │ ├── 13heap_alloc_base.cpp │ ├── 13heap_alloc_dbg.cpp │ ├── 13heap_init.cpp │ ├── 13ioinit.cpp │ ├── 13sbh_alloc_block.cpp │ ├── 13sbh_heap_init.cpp │ ├── 14array_allocator.cpp │ ├── 14debug_allocator.cpp │ ├── 14test_allocator.cpp │ ├── 1primitives_new_delete.cpp │ ├── 2primitives_array_new_delete.cpp │ ├── 3primitives_replacement_new.cpp │ ├── 4override_operator_new.cpp │ ├── 5override_placement_new.cpp │ ├── 6per_class_allocator.cpp │ ├── 6per_class_allocator_advanced.cpp │ ├── 7static_allocator.cpp │ ├── 8macro_static_allocator.cpp │ ├── 9end.cpp │ └── debug │ │ ├── macro │ │ ├── macro.dSYM │ │ └── Contents │ │ │ ├── Info.plist │ │ │ └── Resources │ │ │ └── DWARF │ │ │ └── macro │ │ └── primitives.dSYM │ │ └── Contents │ │ ├── Info.plist │ │ └── Resources │ │ └── DWARF │ │ └── primitives │ └── Pthread │ ├── lab1 │ ├── Lab1.pdf │ ├── Node.c │ └── a.out │ ├── lab2 │ ├── Lab2.pdf │ ├── myCP │ └── myCP.c │ ├── lab3 │ ├── Lab3.pdf │ ├── myLS │ ├── myLS.c │ └── tags │ ├── lab4 │ ├── dir1 │ │ ├── copyfile.c │ │ ├── dir2 │ │ │ ├── Screen Shot 2018-12-20 at 2.48.26 PM.png │ │ │ ├── Screen Shot 2018-12-20 at 2.48.32 PM.png │ │ │ ├── dir3 │ │ │ │ └── test │ │ │ └── myCP │ │ ├── hi6220_spi.h │ │ ├── listdirectory.c │ │ ├── myLS │ │ ├── spi_simulate.c │ │ └── spi_simulate.h │ ├── lab4.pdf │ └── myLS.c │ ├── lab5 │ ├── Lab5.pdf │ ├── dir1 │ │ └── dir3 │ │ │ ├── haohao │ │ │ └── test │ ├── hi6220_spi.h │ ├── listdirectory.c │ ├── main.c │ ├── spi_simulate.h │ └── tags │ ├── lab6 │ ├── lab6.pdf │ ├── philosopher.c │ └── test │ └── lab7 │ ├── a.out │ ├── lab7.pdf │ └── producerAndconsumer.c ├── OImaterial ├── README.md ├── 动态规划 │ ├── DP入门_阮行止.pptx │ ├── DP总结_翁家翌.pdf │ ├── 动态规划_叶国平.ppt │ ├── 动态规划_李远韬.ppt │ ├── 动态规划_钱雨杰.pptx │ ├── 动态规划中的一些优化_孟泽楠.ppt │ ├── 动态规划优化_翁家翌 & 黄哲威.pdf │ ├── 动态规划入门_黄哲威.pdf │ ├── 动态规划及其应用(一)_杨志灿.ppt │ ├── 动态规划及其应用(二)_杨志灿.pptx │ ├── 动态规划引论_阮行止.pdf │ ├── 动态规划问题优化模式2_杨志灿.pptx │ ├── 基于连通性状态压缩的动态规划问题_陈丹琦.ppt │ ├── 树形动态规划_未知作者.ppt │ ├── 树形动态规划_朱全民.ppt │ ├── 树形数位动态规划_黄哲威.pdf │ ├── 状压与区间_黄哲威.pdf │ ├── 状态压缩类型动态规划_朱全民..ppt │ ├── 组合计数与动态规划_曹钦翔.ppt │ ├── 背包与树形_黄哲威.pdf │ └── 背包九讲.pdf ├── 图论 │ ├── 两极相通——浅析最大最小定理在信息学竞赛中的应用_周冬.ppt │ ├── 二分图与匹配_黄哲威.pdf │ ├── 倍增算法及其应用_未知作者.pptx │ ├── 分层图思想及其在信息学竞赛中的应用_肖天.ppt │ ├── 图的连通_黄哲威.pdf │ ├── 图论_李煜东.pptx │ ├── 图论专题之生成树_唐文斌.ppt │ ├── 图论入门与最短路_黄哲威.pdf │ ├── 图论复习_未知作者.ppt │ ├── 图论知识及其应用_刘弈.ppt │ ├── 树上倍增_黄哲威.pdf │ ├── 树分治_黄哲威.pdf │ ├── 树链剖分及其应用_蒋一瑶.pptx │ ├── 浅析二分图匹配在信息学竞赛中的应用_王俊.ppt │ ├── 浅谈一些树形问题_高胜寒.ppt │ ├── 浅谈信息学竞赛中的线性规划——简洁高效的单纯形法实现与应用_李宇骞.ppt │ ├── 生成树和拓扑排序_黄哲威.pdf │ ├── 由对称性解2-SAT问题_未知作者.ppt │ ├── 线性规划与网络流_曹钦翔.pptx │ ├── 网络流_周聿浩 & 黄哲威.pdf │ ├── 网络流_未知作者.ppt │ ├── 网络流_魏越闽.ppt │ └── 网络流建模_周尚彦.pdf ├── 基础算法 │ ├── C++模板与STL库介绍.ppt │ ├── C++的pb_ds库在OI中的应用_于纪平.pdf │ ├── NOIP归纳、贪心、二分、数论_未知作者.pptx │ ├── 万能的解题金钥匙——搜索_未知作者.ppt │ ├── 几道构造相关的题目_吉如一.pdf │ ├── 匹配算法在搜索问题中的巧用_楼天城.ppt │ ├── 复杂度理论、排序、模拟_阮行止.pdf │ ├── 搜索分治贪⼼暴⼒和构造_黄哲威.pdf │ ├── 搜索及其优化_杨志灿.pptx │ ├── 时间复杂度经典解说_未知作者.ppt │ ├── 普及组近5年NOIP试题分析_叶国平.ppt │ ├── 暴力出奇迹_翁家翌.pdf │ ├── 稍微进阶一点的套路_阮行止.pptx │ ├── 贪心数学普及_黄哲威.pdf │ ├── 贪心杂题选讲_黄哲威.pdf │ └── 贪心问题选讲_王天懿.ppt ├── 字符串 │ ├── HASH函数及其应用_朱全民.ppt │ ├── 后缀数组——处理字符串的有力工具_罗穗骞.ppt │ ├── 后缀自动机_陈立杰.pptx │ ├── 多串匹配算法及其启示_朱泽园.ppt │ └── 字符串算法选讲_金策.pdf ├── 数学 │ ├── ACM中的数学问题_林舒.ppt │ ├── Fourier transform_郭晓旭.pdf │ ├── IOI2013与博弈论_王康宁.pptx │ ├── OI 中的超现实数和不平等博弈问题_杜瑜皓.pdf │ ├── OI中的数学_金靖.pptx │ ├── OI中的概率_黄涛岸.ppt │ ├── 一些数学知识_王若松.ppt │ ├── 多项式及求和_杜瑜皓.pptx │ ├── 对拟阵的初步研究_刘雨辰.pdf │ ├── 数学_易健驰.pdf │ ├── 数学_翁家翌 & 黄哲威.pdf │ ├── 数学杂题选讲_黄哲威.pdf │ ├── 数论入门_阮行止.pptx │ ├── 数论及其应用_周聿浩 & 黄哲威.pdf │ ├── 最近一年 THU 命题组出过的数学题选讲_翁家翌.pdf │ ├── 概率与随机化算法_钟诚.pptx │ ├── 浅析非完美算法在信息学竞赛中的应用_胡伟栋.ppt │ ├── 离散数学在信息学竞赛中的运用_未知作者.ppt │ ├── 简单易懂的质数筛法_陈牧歌.pdf │ ├── 线性筛法与积性函数_贾志鹏.pptx │ ├── 组合数学——母函数与递推_朱全民.ppt │ ├── 组合游戏略述——浅谈SG游戏的若干拓展及变形_贾志豪.ppt │ ├── 莫比乌斯反演_王天懿.ppt │ ├── 计数与期望问题选讲_陈立杰.pdf │ └── 计数问题选讲_陈立杰.pdf ├── 数据结构 │ ├── K-D Tree_翁家翌.pdf │ ├── Splay树及其应用_朱全民.ppt │ ├── k-d tree在传统OI数据结构题中的应用_任之洲.pdf │ ├── 二叉树与其应用_朱全民.ppt │ ├── 分块算法_未知作者.pptx │ ├── 分块算法及简单扩展_黄哲威.pdf │ ├── 可并堆_王天懿.ppt │ ├── 基本数据结构在信息学竞赛中的应用_朱晨光.ppt │ ├── 左偏树的特点及其应用_黄源河.ppt │ ├── 平衡树_王天懿.pptx │ ├── 提高组数据结构_吉如一.pdf │ ├── 数据结构_黄哲威.pdf │ ├── 数据结构入门_许昊然.pdf │ ├── 普及数据结构_阮行止.pptx │ ├── 树链剖分_王天懿.ppt │ ├── 根号算法杂谈_杨志灿.ppt │ ├── 线段树_浙大ACM.ppt │ ├── 线段树_翁家翌 & 黄哲威.pdf │ ├── 线段树的合并_黄嘉泰.pptx │ ├── 统计的力量——线段树全接触_张昆玮.pptx │ ├── 莫队算法_刘孟博.pptx │ └── 解决动态统计问题的两把利刃——剖析线段树与矩形切割_薛矛.ppt └── 计算几何 │ ├── 二维计算几何相关算法与实战应用_徐明宽.pptx │ ├── 半平面交的新算法及其实用价值_朱泽园.ppt │ ├── 叉积的应用_卓亮.pdf │ ├── 计算几何_未知作者 2.ppt │ ├── 计算几何_未知作者.ppt │ ├── 计算几何_翁家翌 & 黄哲威.pdf │ ├── 计算几何_邓东.ppt │ ├── 计算几何_钟思思.ppt │ ├── 计算几何_黄涛岸.ppt │ ├── 计算几何及其应用_林厚从.ppt │ ├── 计算几何教程_北大ACM.pptx │ └── 计算几何讨论_胡伟栋.pptx └── README.md /Acwing/0primaryAlgorithm/chp1/bit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | 6 | //返回从个位开始的第一个1 7 | int lowbit(int x) { 8 | return (x & -x); 9 | } 10 | 11 | 12 | 13 | //x >> k & 1得到数x二进制表示下第k位是几 14 | 15 | 16 | int 17 | main() { 18 | 19 | int ans; 20 | cin >> ans; 21 | int num = 0; 22 | while(ans) { 23 | ans -= lowbit(ans); 24 | num++; 25 | } 26 | 27 | //得到每一个数二进制中的1的个数 28 | cout << num << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp1/bsearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int N = 1e6 + 10; 6 | int q[N]; 7 | int n, m; 8 | 9 | 10 | int 11 | main(void) { 12 | 13 | 14 | scanf("%d%d", &n, &m); 15 | for(int i = 0; i < n; i++) scanf("%d", &q[i]); 16 | 17 | while(m --) { 18 | int x; 19 | scanf("%d", &x); 20 | int l = 0, r = n - 1; 21 | while(l < r) { 22 | int mid = (l + r) >> 1; 23 | if(q[mid] >= x) r = mid; 24 | else l = mid + 1; 25 | } 26 | if(q[l] != x) cout << "-1 -1" << endl; 27 | else { 28 | cout << l << ' '; 29 | l = 0, r = n - 1; 30 | while(l < r) { 31 | int mid = (l + r + 1) >> 1; 32 | if(q[mid] <= x) l = mid; 33 | else r = mid - 1; 34 | } 35 | cout << r << endl; 36 | } 37 | 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp1/discretize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const int N = 1e6 + 10; 7 | typedef pair PII; 8 | int a[N], s[N]; 9 | int n, m; 10 | vector add, query; 11 | vector alls; //待离散化数组 12 | 13 | 14 | int find(int x) { 15 | 16 | int l = 0, r = alls.size() - 1; 17 | while(l < r) { 18 | int mid = (l + r) >> 1; 19 | if(alls[mid] >= x) r = mid; 20 | else l = mid + 1; 21 | } 22 | return r + 1; 23 | } 24 | 25 | //去重操作 使用双指针算法 体会算法的简洁和魅力!!! 26 | void my_unique(vector &q) { 27 | int j = 0; 28 | for(unsigned int i = 0; i < q.size(); i ++) { 29 | if(!i || q[i] != q[i-1]) { 30 | q[j++] = q[i]; 31 | } 32 | } 33 | cout << endl; 34 | 35 | for(int i = 0; i < j; i ++) { 36 | cout << q[i] << ' '; 37 | } 38 | } 39 | 40 | 41 | int unique_main(void) { 42 | 43 | vector q; 44 | q.push_back(1); 45 | q.push_back(2); 46 | q.push_back(2); 47 | q.push_back(3); 48 | q.push_back(4); 49 | q.push_back(5); 50 | q.push_back(5); 51 | q.push_back(6); 52 | q.push_back(6); 53 | 54 | my_unique(q); 55 | 56 | return 0; 57 | } 58 | int 59 | main(void) { 60 | 61 | cin >> n >> m; 62 | //各种操作 63 | for(int i = 0; i < n; i ++) { 64 | int x, c; 65 | cin >> x >> c; 66 | add.push_back(make_pair(x, c)); 67 | alls.push_back(x); 68 | } 69 | 70 | //查询 71 | for(int i = 0; i < m; i ++) { 72 | int l, r; 73 | cin >> l >> r; 74 | query.push_back(make_pair(l, r)); 75 | alls.push_back(l); 76 | alls.push_back(r); 77 | } 78 | 79 | //去重 80 | sort(alls.begin(), alls.end()); 81 | alls.erase(unique(alls.begin(), alls.end()), alls.end()); 82 | 83 | //插入操作 84 | for(auto item : add) { 85 | int x = find(item.first); 86 | a[x] += item.second; 87 | } 88 | 89 | //处理前缀和 90 | for(unsigned int i = 1; i <= alls.size(); i ++) s[i] = s[i-1] + a[i]; 91 | for(auto item : query) { 92 | int l = find(item.first); 93 | int r = find(item.second); 94 | int res = s[r] - s[l-1]; 95 | cout << res << endl; 96 | } 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp1/doublepointers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | //去重算法 8 | void duplicate(vector &q) { 9 | for(unsigned int i = 0; i < q.size(); i ++) { 10 | if(!i || q[i] != q[i-1]) 11 | cout << q[i]; 12 | } 13 | puts(""); 14 | 15 | return; 16 | } 17 | 18 | 19 | //按空格读单词 20 | void printfword(string s) { 21 | 22 | for(unsigned int i = 0; i < s.size(); i ++) { 23 | unsigned int j = i; 24 | while(j < s.size() && s[j] != ' ') j ++; 25 | for(unsigned int k = 0; k < j; k ++) cout << s[k]; 26 | cout << endl; 27 | i = j; 28 | } 29 | 30 | return ; 31 | } 32 | 33 | //注意思路的培养 34 | void subsequenceII() { 35 | 36 | const int N = 100010; 37 | int a[N], s[N]; 38 | int n; 39 | cin >> n; 40 | for(int i = 0; i< n; i ++) cin >> a[i]; 41 | 42 | int res = 0; 43 | for(int i = 0, j = 0; i < n; ++ i){ 44 | s[a[i]] ++; 45 | while (s[a[i]] > 1) { 46 | s[a[j]] --; 47 | j ++; 48 | } 49 | res = max(res, i - j + 1); 50 | } 51 | cout << res << endl; 52 | return; 53 | } 54 | 55 | 56 | //最长不重复子序列 57 | void subsequence(vector &q) { 58 | 59 | const int N = 1e6 + 10; 60 | int s[N]; 61 | /*int n;*/ 62 | //cin >> n; 63 | //for(int i = 0; i < n; i ++) { 64 | //int x; 65 | //cin >> x; 66 | //q.push_back(x); 67 | //} 68 | 69 | int res = 0; 70 | for(unsigned int i = 0, j = 0; i < q.size(); i ++) { 71 | s[q[i]]++; 72 | while(s[q[i]] > 1) { 73 | s[q[j]] --; 74 | j ++; 75 | } 76 | res = (res >= (int)(i - j + 1)) ? res : (i - j + 1); 77 | } 78 | cout << res << endl; 79 | return ; 80 | } 81 | 82 | int 83 | main(void) { 84 | vector q; 85 | string s; 86 | 87 | //cin >> q 88 | //duplicate(q); 89 | 90 | //cin >> s 91 | //printfword(s); 92 | 93 | q.push_back(1); 94 | q.push_back(2); 95 | q.push_back(3); 96 | q.push_back(3); 97 | q.push_back(4); 98 | q.push_back(5); 99 | q.push_back(6); 100 | subsequence(q); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp1/mergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const int N = 1e6 + 10; 6 | int q[N], tmp[N]; 7 | int n; 8 | 9 | void merge_sort(int q[], int l, int r) 10 | { 11 | if (l >= r) return; 12 | 13 | int mid = (l + r) >> 1; 14 | merge_sort(q, l, mid); 15 | merge_sort(q, mid + 1, r); 16 | 17 | int k = 0, i = l, j = mid + 1; 18 | while (i <= mid && j <= r) 19 | if (q[i] > q[j]) { 20 | tmp[k ++ ] = q[i ++ ]; 21 | }else { 22 | tmp[k ++ ] = q[j ++ ]; 23 | } 24 | while (i <= mid) { 25 | tmp[k ++ ] = q[i ++ ]; 26 | } 27 | while (j <= r) tmp[k ++ ] = q[j ++ ]; 28 | 29 | for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j]; 30 | } 31 | 32 | int 33 | main(void) { 34 | 35 | scanf("%d", &n); 36 | for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]); 37 | 38 | merge_sort(q, 0, n - 1); 39 | 40 | for(int j = 0; j < n; j ++ ) printf("%d ", q[j]); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp1/quickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | const int N = 1e6 + 10; 8 | int n; 9 | int q[N]; 10 | 11 | void quickSort(int q[], int l, int r) { 12 | 13 | if(l >= r)return; 14 | int x = q[l], i = l - 1, j = r + 1; 15 | while(i < j) { 16 | //!!!不能再q[i] <= x上加上等于号 (仔细思考边界问题如 1 2 3 4 5/ 5 4 3 2 1) 17 | do i++; while(q[i]x); 19 | if(i 2 | #include 3 | #include 4 | 5 | /*区间合并*/ 6 | using namespace std; 7 | typedef pair PII; 8 | vector segs; 9 | 10 | 11 | void merge(vector &q) { 12 | 13 | vector res; 14 | sort(q.begin(), q.end()); 15 | 16 | int st = 2e9, ed = 2e9; //添加哨兵 这个方式和快排的l - 1 && r + 1本质是一样的,很经典的算法 17 | for(auto seg : segs) { 18 | 19 | if(ed < seg.first) { 20 | if(st != 2e9) res.push_back({st, ed}); 21 | st = seg.first, ed = seg.second; 22 | }else { 23 | ed = max(seg.second, ed); 24 | } 25 | } 26 | if(st != 2e9) res.push_back({st, ed}); 27 | 28 | segs = res; 29 | } 30 | 31 | 32 | 33 | 34 | int 35 | main (void) { 36 | 37 | int n; 38 | cin >> n; 39 | for(int i = 0; i < n; i ++) { 40 | int l, r; 41 | cin >> l >> r; 42 | segs.push_back(make_pair(l, r)); 43 | } 44 | merge(segs); 45 | cout << segs.size() << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/advancedUnionfind.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 并查集在存储的过程中可以维护一些信息 来处理问题 3 | 到根节点的距离dist[]: 4 | mod 0: 和根节点是同类 5 | mod 1: 可以吃根节点 6 | mod 2: 可以被根节点吃 7 | */ 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | const int N = 50010; 13 | int p[N], dist[N]; 14 | int n, k; 15 | 16 | int find(int x) { 17 | if(x != p[x]) { 18 | int t = find(p[x]); 19 | dist[x] += dist[p[x]]; 20 | p[x] = t; 21 | } 22 | return p[x]; 23 | } 24 | 25 | int main(void) { 26 | 27 | int n, m; 28 | cin >> n >> m; 29 | for(int i = 1; i<= n; i++) p[i] = i; 30 | 31 | int res = 0; 32 | while (m --) { 33 | int op, x, y; 34 | scanf("%d%d%d", &op, &x, &y); 35 | 36 | if(x > n || y > n) res ++; 37 | else { 38 | int px = find(x) , py = find(y); 39 | if(op == 1) { 40 | if (px == py && (dist[x] - dist[y]) % 3) res ++; 41 | else if(px != py){ 42 | p[px] = py; 43 | dist[px] = dist[y] - dist[x]; 44 | } 45 | }else { 46 | if (px == py && (dist[x] - dist[y] - 1) % 3) res ++; 47 | else if(px != py){ 48 | p[px] = py; 49 | dist[px] = dist[y] + 1 - dist[x]; 50 | } 51 | } 52 | } 53 | } 54 | cout << res << endl; 55 | return 0; 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/advancedheap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | int h[N]; 8 | int size; 9 | 10 | int ph[N]; //第k个插入的点在堆中的下标 11 | int hp[N]; //堆里的点是第几个插入的点 12 | 13 | 14 | void swap_heap(int a, int b) { 15 | swap(ph[hp[a]], ph[hp[b]]); 16 | swap(hp[a], hp[b]); 17 | swap(h[a], h[b]); 18 | } 19 | 20 | 21 | void down(int x){ 22 | int u = x; 23 | if(x*2 <= size && h[x*2] < h[u]) u = x *2; 24 | if(x*2 + 1 <= size && h[x * 2 + 1] < h[u]) u = x * 2 + 1; 25 | if(u != x) { 26 | swap_heap(u, x); 27 | down(u); 28 | } 29 | 30 | return ; 31 | } 32 | 33 | void up(int x) { 34 | 35 | while(x/2 && h[x] < h[x / 2] ) { 36 | swap_heap(x, x/2); 37 | x /= 2; 38 | 39 | } 40 | 41 | return ; 42 | } 43 | 44 | int 45 | main(void) { 46 | 47 | int n, m = 0; 48 | cin >> n; 49 | while( n --) { 50 | char op[10]; 51 | int k, x; 52 | cin >> op; 53 | if(!strcmp(op, "I")) { 54 | scanf("%d", &x); 55 | size++; 56 | m ++; 57 | ph[m] = size,hp[size] = m; 58 | h[size] = x; 59 | up(size); 60 | }else if(!strcmp(op, "PM")) cout << h[1] << endl; 61 | else if(!strcmp(op, "DM")) { 62 | swap_heap(1, size); 63 | size--; 64 | down(1); 65 | }else if(!strcmp(op, "D")) { 66 | scanf("%d", &k); 67 | k = ph[k]; 68 | swap_heap(k, size); 69 | size--; 70 | down(k); 71 | up(k); 72 | }else { 73 | cin >> k >> x; 74 | k = ph[k]; 75 | h[k] = x; 76 | down(k); 77 | up(k); 78 | } 79 | } 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/doublelink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /*双链表 静态数组模拟 体会使用数组元素模拟双链表两个左右端点的方式*/ 4 | using namespace std; 5 | 6 | const int N = 10010; 7 | int l[N], r[N], e[N]; 8 | int idx; 9 | 10 | void init() { 11 | l[1] = 0; 12 | r[0] = 1; 13 | idx = 2; 14 | } 15 | 16 | 17 | void insert(int k, int x) { 18 | 19 | e[idx] = x; 20 | r[idx] = r[k]; 21 | l[idx] = k; 22 | l[r[k]] = idx; 23 | r[k] = idx; 24 | idx++; 25 | } 26 | 27 | void remove(int k) { 28 | 29 | l[r[k]] = l[k]; 30 | r[l[k]] = r[k]; 31 | 32 | } 33 | 34 | 35 | int 36 | main (void) { 37 | 38 | int M; 39 | cin >> M; 40 | init(); 41 | while(M --) { 42 | string op; 43 | int x, k; 44 | cin >> op; 45 | if(op == "L") { 46 | cin >> x; 47 | insert(0, x); 48 | }else if(op == "R") { 49 | cin >> x; 50 | insert(l[1], x); 51 | }else if(op == "IL") { 52 | cin >> k >> x; 53 | insert(l[k] + 1, x); 54 | }else if(op == "IR") { 55 | cin >> k >> x; 56 | insert(k + 1, x); 57 | }else if(op == "D") { 58 | cin >> k; 59 | remove(k + 1); 60 | 61 | } 62 | } 63 | for(int i = r[0]; i != 1; i = r[i]) cout << e[i] << ' '; 64 | cout << endl; 65 | return 0; 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/heap.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 100010; 4 | int h[N]; 5 | int size; 6 | 7 | void down(int x){ 8 | 9 | int u = x; 10 | if(x*2 <= size && h[x*2] < h[u]) u = x *2; 11 | if(x*2 + 1 <= size && h[x * 2 + 1] < h[u]) u = x * 2 + 1; 12 | if(u != x) { 13 | swap(h[u], h[x]); 14 | down(u); 15 | } 16 | 17 | return ; 18 | } 19 | 20 | void up(int x) { 21 | 22 | while(x/2 && h[x] < h[x / 2] ) { 23 | swap(h[x], h[x/2]); 24 | x /= 2; 25 | 26 | } 27 | 28 | return ; 29 | } 30 | 31 | int 32 | main(void) { 33 | 34 | int n, m; 35 | cin >> n >> m; 36 | for(int i = 1; i <= n; i ++) cin >> h[i]; 37 | size = n; 38 | 39 | for(int i = size/2; i; i --) down(i); 40 | 41 | while(m --) { 42 | printf("%d ", h[1]); 43 | h[1] = h[size]; 44 | size--; 45 | down(1); 46 | } 47 | 48 | 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/kmp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | /*时间复杂度 是O(n)*/ 7 | const int N = 10010, M = 100010; 8 | int n, m; 9 | char p[N], s[M]; 10 | int ne[N]; 11 | 12 | 13 | int 14 | main(void) { 15 | 16 | cin >> n >> (p + 1) >> m >> (s + 1); 17 | //求next过程 18 | for(int i = 2, j = 0; i <= n; i ++) { 19 | while(j && p[i] != p[j+1]) j = ne[j]; 20 | if(p[i] == p[j+1]) j ++; 21 | ne[i] = j; 22 | } 23 | 24 | 25 | //匹配过程 26 | for(int i = 1,j = 0; i <= m; i ++) { 27 | while(j && s[i]!=p[j+1]) j = ne[j]; 28 | if(s[i] == p[j+1]) j ++; 29 | if(j == n){ 30 | //匹配成功 31 | cout << i - n<< endl; 32 | j = ne[j]; 33 | } 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/monotonic.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N = 100010; 5 | 6 | /*体会算法的间接性 尽量快的熟练思路*/ 7 | 8 | /* 9 | 单调栈 10 | 从一个序列中找到每一个元素左边离它最近的最小值 11 | 3 4 2 7 5 12 | */ 13 | void monoticStack() { 14 | 15 | int stk[N]; 16 | int hh = 0; //模拟栈第零号不存储元素 stk[++hh] = x; 17 | 18 | int n; 19 | cin >> n; 20 | while(n --) { 21 | int x; 22 | cin >> x; 23 | while(hh && stk[hh] > x) hh--; // 维护一个单调的栈区间 24 | if(hh) cout << stk[hh] << ' '; 25 | else cout << "-1 "; 26 | stk[++hh] = x; 27 | } 28 | return ; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | //单调队列 36 | void monticQueue() { 37 | 38 | int q[N], a[N]; 39 | int hh = 0, tt = -1; 40 | int n, k; 41 | cin >> n >> k; 42 | for(int i = 0; i > a[i]; 43 | 44 | //求出滑动窗口的最小值 45 | for(int i = 0; i < n; i ++) { 46 | //判断队列是否已满 47 | if(hh<=tt && q[hh] < i - k + 1) hh ++; 48 | while(hh<=tt && a[q[tt]] >= a[i]) tt--; 49 | q[++tt] = i; 50 | if(i >= k - 1) cout << a[q[hh]] << ' '; 51 | } 52 | 53 | puts(""); 54 | 55 | //求出滑动窗口中的最大值z 56 | hh = 0, tt = -1; 57 | for(int i = 0; i < n; i ++) { 58 | if(hh<=tt && q[hh] < i - k + 1)hh ++; 59 | while(hh<=tt && a[q[tt]] <= a[i]) tt--; 60 | q[++tt] = i; 61 | if(i >= k -1) cout << a[q[hh]] << ' '; 62 | } 63 | 64 | 65 | 66 | return; 67 | } 68 | 69 | int 70 | main(void) { 71 | 72 | // monoticStack(); 73 | 74 | monticQueue(); 75 | return 0; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/singleLink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /*静态链表 数组模拟单链表*/ 3 | using namespace std; 4 | const int N = 100010; 5 | int e[N], ne[N]; 6 | int head, idx; 7 | 8 | 9 | void init() { 10 | head = -1; 11 | idx = 0; 12 | } 13 | 14 | void insert_head(int x) { 15 | e[idx] = x; 16 | ne[idx] = head; 17 | head = idx; 18 | idx ++; 19 | } 20 | 21 | void insert(int k, int x) { 22 | e[idx] = x; 23 | ne[idx] = ne[k]; 24 | ne[k] = idx; 25 | idx ++; 26 | } 27 | 28 | void remove(int k) { 29 | ne[k] = ne[ne[k]]; 30 | 31 | } 32 | 33 | 34 | int 35 | main(void) { 36 | 37 | int M; 38 | cin >> M; 39 | while(M --) { 40 | 41 | char op; 42 | int x, k; 43 | cin >> op; 44 | if(op == 'I') { 45 | cin >> k >> x; 46 | insert(k - 1, x); 47 | }else if(op == 'D') { 48 | cin >> k; 49 | if(!k) head = ne[head]; 50 | remove(k - 1); 51 | }else if(op == 'H') { 52 | cin >> x; 53 | insert_head(x); 54 | } 55 | } 56 | 57 | //输出 58 | for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' '; 59 | cout << endl; 60 | return 0; 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/stackqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | const int N = 100010; 5 | int q[N], tt = -1, hh; 6 | int stk[N],shh; 7 | 8 | void myfront(int x) { 9 | q[++tt] = x; 10 | } 11 | 12 | int mypop() { 13 | if(hh > tt) return -1; 14 | return (q[hh++]); 15 | 16 | } 17 | 18 | //空返回真 19 | bool empty() { 20 | return (hh > tt); 21 | } 22 | 23 | 24 | 25 | int 26 | queue_main(void) { 27 | 28 | myfront(1); 29 | myfront(2); 30 | myfront(3); 31 | myfront(4); 32 | myfront(5); 33 | 34 | while(!empty()) { 35 | cout << mypop() << endl; 36 | } 37 | 38 | return 0; 39 | 40 | } 41 | 42 | void my_spush(int x) { 43 | stk[++shh] = x; 44 | 45 | } 46 | 47 | //空返回真 48 | bool my_sempty() { 49 | return (shh <= 0); 50 | } 51 | 52 | 53 | int my_spop() { 54 | if(shh <= 0) return -1; 55 | return (stk[shh--]); 56 | } 57 | 58 | 59 | int 60 | main(void) { 61 | my_spush(1); 62 | my_spush(2); 63 | my_spush(3); 64 | my_spush(4); 65 | my_spush(5); 66 | my_spush(6); 67 | 68 | while(!my_sempty()) { 69 | cout << my_spop() << endl; 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/strhash.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 100010, P = 131; 4 | typedef unsigned long long ULL; 5 | 6 | 7 | int n, m; 8 | char str[N]; 9 | ULL h[N], p[N]; 10 | 11 | ULL get(int l, int r) { 12 | return h[r] - h[l-1] * p[r-l+1]; 13 | } 14 | 15 | int 16 | main(void){ 17 | scanf("%d%d%s", &n, &m, str+1); 18 | 19 | p[0] = 1; 20 | for(int i = 1; i <= n; i ++) { 21 | p[i] = p[i-1] * P; 22 | h[i] = h[i-1] * P + str[i]; 23 | } 24 | 25 | 26 | while(m --) { 27 | int l1, r1, l2, r2; 28 | scanf("%d%d%d%d", &l1, &r1, &l2, &r2); 29 | 30 | if(get(l1, r1) == get(l2, r2)) puts("Yes"); 31 | else puts("No"); 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/trie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 100010; 4 | int son[N][26], cnt[N], idx; 5 | 6 | void insert(char str[]){ 7 | int p = 0; 8 | for(int i = 1;str[i] ; i ++) { 9 | int u = str[i] - 'a'; 10 | if(!son[p][u]) son[p][u] = ++idx; 11 | p = son[p][u]; 12 | } 13 | 14 | cnt[p] ++; 15 | } 16 | 17 | 18 | int find(char str[]) { 19 | int p = 0; 20 | for(int i =0; str[i]; i++) { 21 | int u = str[i] - 'a'; 22 | if(!son[p][u]) return 0; 23 | p = son[p][u]; 24 | } 25 | 26 | return cnt[p]; 27 | } 28 | 29 | 30 | 31 | int 32 | main(void) { 33 | 34 | int n; 35 | cin >> n; 36 | char str[N]; 37 | cin >> str; 38 | 39 | 40 | while(n --) { 41 | char op; 42 | cin >> op >> str; 43 | if(op == 'I') { 44 | insert(str); 45 | }else if(op == 'Q') { 46 | cout << find(str) << endl; 47 | } 48 | 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/unionfind.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /*并查集 理解find函数的核心作用 压缩路径优化可以将复杂度降到O(1) 有时候并查集会存储一些额外的信息*/ 5 | const int N = 100010; 6 | int p[N]; 7 | 8 | 9 | int find(int x) { 10 | if(x != p[x]) p[x] = find(p[x]); 11 | return p[x]; 12 | } 13 | 14 | 15 | 16 | 17 | //基本的并查集 18 | int 19 | test0_main(void) { 20 | 21 | int n, m; 22 | cin >> n >> m; 23 | for(int i = 1; i <= n; i++) p[i] = i; 24 | 25 | char op; 26 | int a, b; 27 | while(m --) { 28 | cin >> op >> a >> b; 29 | if(op == 'M') { 30 | cin >> a >> b; 31 | if(find(a) == find(b)) continue; 32 | p[find(a)] = find(b); 33 | }else if(op == 'Q') { 34 | cin >> a >> b; 35 | if(find(a) == find(b)) cout << "Yes" << endl; 36 | else cout << "No" << endl; 37 | } 38 | 39 | } 40 | return 0; 41 | } 42 | 43 | 44 | //存储了额外元素的并查集 45 | int size[N]; //存放额外的集合元素个数的数组 46 | int 47 | test1_main(void) { 48 | 49 | int n, m; 50 | cin >> n >> m; 51 | for(int i = 1; i <= n; i ++) { 52 | p[i] = i; 53 | size[i] = 1; 54 | } 55 | 56 | char op[2]; 57 | int a, b; 58 | cin >> op; 59 | while(m --) { 60 | if(op[0] == 'C') { 61 | cin >> a >> b; 62 | if(find(b) == find(a)) continue; 63 | size[find(a)] += size[find(b)]; 64 | p[find(b)] = find(a); 65 | } else if(op[1] == '1') { 66 | cin >> a >> b; 67 | if(find(b) == find(a)) cout << "Yes" << endl; 68 | else cout << "No" << endl; 69 | 70 | }else if(op[1] == '2') { 71 | cin >> a; 72 | cout << size[find(a)] << endl; 73 | } 74 | } 75 | 76 | return 0; 77 | } 78 | 79 | 80 | int 81 | main(void) { 82 | 83 | test0_main(); 84 | test1_main(); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp2/xor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010, M = 31 * N; 7 | int n, idx; 8 | int a[N]; 9 | int son[M][2]; 10 | 11 | void insert(int x) { 12 | 13 | int p = 0; 14 | for(int i = 30; i >= 0; i --) { 15 | int u = x >> i & 1; 16 | if(!son[p][u]) son[p][u] = ++idx; 17 | p = son[p][u]; 18 | } 19 | } 20 | 21 | 22 | int query(int x) { 23 | int p = 0, res = 0; 24 | for(int i = 30; i >= 0; i --){ 25 | int u = x >> i & 1; 26 | if(son[p][!u]) { 27 | p = son[p][!u]; 28 | res = res * 2 + !u; 29 | } else { 30 | p = son[p][u]; 31 | res = res * 2 + u; 32 | } 33 | } 34 | return res; 35 | } 36 | 37 | 38 | int main(void) 39 | { 40 | 41 | cin >> n; 42 | for(int i = 0; i < n; i ++) cin >> a[i]; 43 | 44 | int res = 0; 45 | for(int i = 0; i < n; i++) { 46 | insert(a[i]); 47 | int t = query(a[i]); 48 | res = max(res, a[i] & t); 49 | } 50 | cout << res << endl; 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/Floyd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 210, INF = 1e9; 7 | int d[N][N]; 8 | int n, m, Q; 9 | 10 | /* 11 | 使用邻接矩阵存储 12 | 13 | 注意邻接矩阵在初始化的时候 14 | 15 | */ 16 | void floyd() { 17 | 18 | int i, j, k; 19 | for(k = 1; k <= n; k ++) 20 | for(i = 1; i <=n; i ++) 21 | for(j = 1; j <= n; j++) 22 | d[i][j] = min(d[i][j], d[i][k] + d[k][j]); 23 | } 24 | 25 | int 26 | main(void) { 27 | cin >> n >> m >> Q; 28 | //初始化 29 | for(int i = 1; i <= n; i ++) { 30 | for(int j = 1; j <= n; j ++) { 31 | if(i == j) d[i][j] = 0; 32 | else d[i][j] = INF; 33 | } 34 | } 35 | 36 | 37 | while(m --) { 38 | int a, b, w; 39 | cin >> a >> b >> w; 40 | d[a][b] = min(d[a][b], w); 41 | } 42 | 43 | floyd(); 44 | 45 | while(Q --) { 46 | int a, b; 47 | cin >> a>> b; 48 | //这里还是会遇到和spfa算法相同的判断方式 49 | if(d[a][b] > INF / 2) cout << "impossible" << endl; 50 | else cout << d[a][b] < 2 | #include 3 | #include 4 | 5 | //因为有负权边 6 | using namespace std; 7 | const int N = 510, M = 10010; 8 | 9 | struct Edge { 10 | int a, b, c; 11 | }edges[M]; 12 | 13 | int n, m, k; 14 | int dist[N]; 15 | int last[N]; 16 | 17 | void bellman_ford() { 18 | 19 | //数组初始化 20 | memset(dist, 0x3f, sizeof dist); 21 | dist[1] = 0; 22 | 23 | //不超过k条边 24 | for(int i = 0; i < k; i ++) { 25 | memcpy(last, dist, sizeof dist); 26 | for(int j = 0; j < m; j ++) { 27 | auto e = edges[j]; 28 | dist[e.b] = min(dist[e.b], last[e.a]+e.c); 29 | } 30 | } 31 | } 32 | 33 | int 34 | main(void) { 35 | cin >> n >> m >> k; 36 | 37 | for(int i = 0; i < m; i ++) { 38 | int a, b, c; 39 | cin >> a>> b >> c; 40 | edges[i].a = a; 41 | edges[i].b = b; 42 | edges[i].c = c; 43 | } 44 | bellman_ford(); 45 | 46 | if(dist[n] > 0x3f3f3f3f / 2) puts("impossible"); 47 | else printf("%d\n", dist[n]); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/bfsGraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | int e[N],ne[N], h[N], idx; 8 | int q[N], d[N]; 9 | int n, m; 10 | 11 | void add(int a, int b) { 12 | e[idx] = b; 13 | ne[idx] = h[a]; 14 | h[a] = idx ++; 15 | } 16 | 17 | int bfs() { 18 | 19 | int hh = 0, tt = 0; 20 | q[0] = 1; // 因为是从第一个点 21 | memset(d, -1, sizeof d); 22 | d[1] = 0; //从第一个点 23 | 24 | while(hh <= tt) { 25 | int t = q[hh++]; 26 | for(int i=h[t]; i != -1; i=ne[i]) { 27 | int j = e[i]; 28 | if(d[j] == -1) { 29 | d[j] = d[t] + 1; 30 | q[++tt] = j; 31 | } 32 | } 33 | } 34 | return d[n]; 35 | } 36 | 37 | int 38 | main(void) { 39 | 40 | cin >> n >> m; 41 | memset(h, -1, sizeof h); 42 | 43 | while(m --) { 44 | int a, b; 45 | cin >> a >> b; 46 | add(a, b); 47 | } 48 | 49 | cout << bfs() << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/bipartiteGraphI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010, M = 2 * N; 7 | int n, m; 8 | int h[N], e[M], ne[M], idx; //因为是无向图,点数相同,边数加倍 9 | int color[N]; 10 | 11 | void add(int a, int b) { 12 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++; 13 | } 14 | 15 | 16 | //具有深搜的性质。理解bool st数组和color的转换方式 17 | bool dfs(int u, int c) { 18 | color[u] = c; 19 | for(int i = h[u]; i != -1; i = ne[i]) { 20 | int j = e[i]; 21 | if(!color[j]){ 22 | if(!dfs(j, 3 - c)) return false; 23 | }else if(color[j] == c) return false; 24 | } 25 | return true; 26 | } 27 | 28 | int 29 | main(void) { 30 | 31 | cin >> n >> m; 32 | memset(h, -1, sizeof h); 33 | 34 | //建图 35 | while(m --) { 36 | int a, b; 37 | cin >> a >> b; 38 | add(a, b), add(b, a); 39 | } 40 | 41 | bool flag = true; 42 | for(int i = 1; i <= n; i ++) 43 | if(!color[i]) { 44 | if(!dfs(i, 1)) { 45 | flag = false; 46 | break; 47 | } 48 | } 49 | 50 | 51 | if(flag) puts("Yes"); 52 | else puts("No"); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/bipartiteGraphII.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 510, M = 100010; 8 | 9 | int n1, n2, m; 10 | int h[N], e[M], ne[M], idx; 11 | int match[N]; 12 | bool st[N]; 13 | 14 | void add(int a, int b) 15 | { 16 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ; 17 | } 18 | 19 | bool find(int x) 20 | { 21 | for (int i = h[x]; i != -1; i = ne[i]) 22 | { 23 | int j = e[i]; 24 | if (!st[j]) 25 | { 26 | st[j] = true; 27 | if (match[j] == 0 || find(match[j])) 28 | { 29 | match[j] = x; 30 | return true; 31 | } 32 | } 33 | } 34 | 35 | return false; 36 | } 37 | 38 | int main() 39 | { 40 | scanf("%d%d%d", &n1, &n2, &m); 41 | 42 | memset(h, -1, sizeof h); 43 | 44 | while (m -- ) 45 | { 46 | int a, b; 47 | scanf("%d%d", &a, &b); 48 | add(a, b); 49 | } 50 | int res = 0; 51 | for(int i = 1; i <= n1; i ++ ) 52 | { 53 | memset(st, false, sizeof st); 54 | if (find(i)) res ++ ; 55 | } 56 | 57 | printf("%d\n", res); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/dfsGraph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010, M = N * 2; 7 | int e[N], ne[N], h[N], idx; 8 | bool st[N]; 9 | 10 | void add (int a, int b) { 11 | e[idx] = b, ne[idx] = h[a], h[a] = idx ++; 12 | } 13 | 14 | void dfs(int u) { 15 | st[u] = false; 16 | 17 | for(int i = h[u]; i != -1; i=ne[i]) { 18 | int j = e[i]; 19 | if(!st[i]) dfs(j); 20 | } 21 | } 22 | 23 | int 24 | main(void) { 25 | 26 | memset(h, -1, sizeof h); 27 | 28 | dfs(1); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/dijkstraList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 510, M = 100010; 7 | const int INF = 0x3f3f3f3f; 8 | int e[M], ne[M], h[N], w[M], idx; 9 | int dist[N]; 10 | bool st[N]; 11 | int n, m; 12 | void add(int a, int b, int c) { 13 | e[idx] = b; 14 | ne[idx] = h[a]; 15 | w[idx] = c; 16 | h[a] = idx ++; 17 | } 18 | 19 | int Dijkstra() { 20 | 21 | memset(dist, 0x3f, sizeof dist); 22 | dist[1] = 0; 23 | 24 | for(int i = 0; i < n; i ++) { 25 | int t = -1; 26 | for(int j = 1; j <= n; j ++) 27 | if(!st[j] && (t==-1 || dist[t] > dist[j])) t = j; 28 | 29 | st[t] = true; 30 | for(int j = h[t]; j != -1; j =ne[j]) { 31 | int k = e[j]; 32 | dist[k] = min(dist[k], dist[t] + w[j]); 33 | } 34 | } 35 | 36 | if(dist[n] == INF) return -1; 37 | return dist[n]; 38 | } 39 | 40 | int 41 | main(void) { 42 | 43 | cin >> n >> m; 44 | memset(h, -1, sizeof h); 45 | while(m --) { 46 | int a, b, w; 47 | cin >> a>> b >> w; 48 | add(a, b, w); 49 | } 50 | 51 | int t = Dijkstra(); 52 | 53 | cout << t << endl; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/dijkstraMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //可以尝试使用邻接表 来实现以下朴素版本的Dijkstra 6 | using namespace std; 7 | const int N = 510; 8 | int n, m; 9 | int g[N][N]; //邻接矩阵 10 | int dist[N]; //最短距离 11 | bool st[N]; //是否已经被收录 12 | 13 | 14 | 15 | //注意理解Dijkstra算法的精髓:若路径是按照递增的顺序生成的,那真正的最短路一定只经过S中的顶点 16 | int dijkstra() { 17 | 18 | memset(dist, 0x3f, sizeof dist); 19 | dist[1] = 0; 20 | 21 | 22 | for(int i = 0; i < n; i ++) { 23 | int t = -1; 24 | //未收录(!st[i])定点中距离最小的定点 25 | for(int j = 1; j <= n; j ++) 26 | if(!st[j] && (t==-1 || dist[j] 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 100010; 8 | int e[N], ne[N], h[N], w[N], idx; 9 | int dist[N]; 10 | bool st[N]; 11 | int n, m; 12 | typedef pair PII; 13 | 14 | 15 | 16 | 17 | void add(int a, int b ,int c) { 18 | e[idx] = b; 19 | ne[idx] = h[a]; 20 | w[idx] = c; 21 | h[a] = idx ++; 22 | } 23 | 24 | 25 | int dijkstra() { 26 | memset(dist, 0x3f, sizeof dist); 27 | dist[1] = 0; 28 | 29 | priority_queue, greater> heap; 30 | heap.push({0, 1}); 31 | 32 | while(heap.size()){ 33 | auto t = heap.top(); 34 | heap.pop(); 35 | int ver = t.second, distance = t.first; 36 | 37 | for(int i = h[ver]; i != -1; i = ne[i]) { 38 | int j = e[i]; 39 | if(dist[j] > distance + w[i]) { 40 | dist[j] = distance + w[i]; 41 | heap.push({dist[j], j}); 42 | } 43 | } 44 | } 45 | 46 | if(dist[n] == 0x3f3f3f3f) return -1; 47 | return dist[n]; 48 | } 49 | 50 | 51 | int 52 | main(void) { 53 | 54 | cin >> n >> m; 55 | memset(h, -1, sizeof h); 56 | while(m --) { 57 | int a, b, c; 58 | cin >> a >> b >> c; 59 | add(a, b, c); 60 | } 61 | 62 | cout << dijkstra() << endl; 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/kruskal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010, M = 200010, INF = 0x3f3f3f3f; 7 | int n, m; 8 | int p[N]; 9 | 10 | struct Edge{ 11 | 12 | int a, b, w; 13 | bool operator < (const Edge &W) const { 14 | return w < W.w; 15 | } 16 | }edges[M]; 17 | 18 | int find(int x) { 19 | if(p[x] != x) p[x] = find(p[x]); return p[x]; } 20 | 21 | /* 22 | 本质是 23 | 将所有有序边都遍历一遍,然后选择 24 | (遍历的边数) && (是否在构造图中 是否连通) 25 | */ 26 | int kruskal() { 27 | sort(edges, edges + m); 28 | //注意并查集的初始化 29 | //注意理解图中 点和边的关系 点是从1开始的 30 | for(int i = 1; i <= n; i ++) p[i] = i; 31 | 32 | int res = 0, cnt = 0; 33 | 34 | for(int i = 0; i < m; i ++) { 35 | 36 | int a = edges[i].a, b = edges[i].b, w = edges[i].w; 37 | a = find(a), b = find(b); 38 | if(a != b) { 39 | p[a] = b; 40 | res += w; 41 | cnt ++; 42 | } 43 | } 44 | if(cnt < n - 1) return INF; 45 | return res; 46 | } 47 | 48 | 49 | int 50 | main(void){ 51 | 52 | cin >> n >> m; 53 | for(int i = 0; i < m; i ++) { 54 | int a,b,w; 55 | cin >> a >> b >> w; 56 | edges[i].a = a; 57 | edges[i].b = b; 58 | edges[i].w = w; 59 | } 60 | 61 | int t = kruskal(); 62 | if(t == INF) puts("impossible"); 63 | else printf("%d\n", t); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 110; 7 | int g[N][N]; 8 | int d[N][N]; 9 | int n, m; 10 | 11 | //用来存储坐标的队列 12 | typedef pair PII; 13 | PII q[N]; 14 | int hh , tt; 15 | int bfs(){ 16 | 17 | q[0] = make_pair(0, 0); 18 | memset(d, -1, sizeof d); 19 | d[0][0] = 0; 20 | 21 | int dx[4] = {-1, 0, 1, 0}; 22 | int dy[4] = {0, 1, 0, -1}; 23 | while(hh <= tt) { 24 | auto t = q[hh ++]; 25 | for(int i = 0; i < 4; i ++) { 26 | int x = t.first + dx[i], y = t.second + dy[i]; 27 | if(x>=0 && x=0 && y> n >> m; 41 | for(int i = 0; i < n; i ++) 42 | for(int j = 0; j < m ;j++) 43 | cin >> g[i][j]; 44 | 45 | cout << bfs() << endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/permutation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | const int N = 100010; 6 | int path[N], n; 7 | bool stk[N]; 8 | 9 | //注意其中变量u和i是最重要的理解DFS的方式,path[u]只是存储方式,i的变化才是重点 10 | void dfs(int u) { 11 | if(u == n) { 12 | for(int i = 0; i < n; i ++) cout << path[i] << ' '; 13 | puts(""); 14 | return ; 15 | } 16 | 17 | for(int i = 1; i <= n; i ++) { 18 | if(!stk[i]) { 19 | path[u] = i; 20 | stk[i] = true; 21 | dfs(u + 1); 22 | stk[i] = false; 23 | } 24 | } 25 | } 26 | 27 | int 28 | main(void) { 29 | 30 | cin >> n; 31 | dfs(0); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/prim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 510, INF = 0x3f3f3f3f; 7 | 8 | int g[N][N]; 9 | int dist[N]; 10 | bool st[N]; 11 | int n, m; 12 | 13 | 14 | int prim() { 15 | memset(dist, 0x3f, sizeof dist); 16 | 17 | int res = 0; 18 | for(int i = 0; i < n; i ++) { 19 | int t = -1; 20 | for(int j = 1; j <= n; j ++) 21 | if(!st[j] && (t==-1 || dist[t] > dist[j])) t = j; 22 | 23 | if(i && dist[t] == INF) return INF; 24 | if(i) res += dist[t]; 25 | 26 | 27 | for(int i = 1; i <= n; i ++) dist[i] = min(dist[i], g[t][i]); 28 | st[t] = true; 29 | } 30 | 31 | return res; 32 | } 33 | 34 | int 35 | main(void) { 36 | cin >> n >> m; 37 | memset(g, 0x3f, sizeof g); 38 | while(m --){ 39 | int a, b, c; 40 | cin >> a >> b >> c; 41 | g[a][b] = g[b][a] = min(g[a][b], c); 42 | } 43 | 44 | int t = prim(); 45 | 46 | if(t == INF) cout << "impossible" << endl; 47 | else cout << t << endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/queen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int N = 100010; 4 | bool col[N], dg[N], udg[N]; 5 | char g[N][N]; 6 | int n; 7 | 8 | 9 | //使用全排列的思路解决问题 10 | void dfs(int u) { 11 | if(u == n) { 12 | for(int i = 0; i < n; i ++) puts(g[i]); 13 | puts(""); 14 | return; 15 | } 16 | 17 | 18 | for(int i = 0; i < n; i ++) { 19 | if(!col[i] && !dg[u + i] && !udg[n - u + i]){ 20 | col[i] = dg[u+i] = udg[n-u+i] = true; 21 | g[u][i] = 'Q'; 22 | dfs(u + 1); 23 | col[i] = dg[u+i] = udg[n-u+i] = false; 24 | g[u][i] = '.'; 25 | } 26 | } 27 | } 28 | 29 | 30 | 31 | int 32 | main(void) { 33 | cin >> n; 34 | 35 | for(int i =0; i < n; i ++) 36 | for(int j = 0; j < n; j ++) 37 | g[i][j] = '.'; 38 | 39 | dfs(0); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/spfa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | /*既可以解决负权边,又可以根据代替Dijkstra解决正权边问题*/ 8 | /*不过时间复杂度可能有时候会很高*/ 9 | using namespace std; 10 | const int N = 100010; 11 | int dist[N]; 12 | int e[N], ne[N], h[N], w[N], idx; 13 | bool st[N]; 14 | 15 | int n, m; 16 | 17 | void add(int a, int b, int c) { 18 | e[idx] = b; 19 | ne[idx] = h[a]; 20 | w[idx] = c; 21 | h[a] = idx ++; 22 | } 23 | 24 | 25 | int spfa() { 26 | 27 | memset(dist, 0x3f, sizeof dist); 28 | dist[1] = 0; 29 | st[1] = true; 30 | 31 | queue q; 32 | q.push(1); 33 | 34 | while(q.size()) { 35 | int t = q.front(); 36 | q.pop(); 37 | st[t] = false; 38 | for(int i = h[t]; i != -1; i =ne[i]){ 39 | int j = e[i]; 40 | if(dist[j] > dist[t] + w[i]) { 41 | dist[j] = dist[t] + w[i]; 42 | if(!st[j]) { 43 | q.push(j); 44 | st[j] = true; 45 | } 46 | } 47 | } 48 | } 49 | if(dist[n] == 0x3f3f3f3f) return -1; 50 | return dist[n]; 51 | } 52 | 53 | 54 | int 55 | main(void) { 56 | 57 | cin >> n >> m; 58 | memset(h, -1, sizeof h); 59 | while(m --) { 60 | int a, b, c; 61 | cin >>a >> b >> c; 62 | add(a, b, c); 63 | } 64 | 65 | if(spfa() == -1) cout << "impossible" << endl; 66 | else cout << dist[n] << endl; 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/spfanNegetive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | const int N = 100010; 8 | int e[N], ne[N], h[N], w[N], idx; 9 | int dist[N], cnt[N]; 10 | int n, m; 11 | bool st[N]; 12 | 13 | 14 | void add(int a, int b, int c) { 15 | e[idx] = b; 16 | ne[idx] = h[a]; 17 | w[idx] = c; 18 | h[a] = idx ++; 19 | } 20 | 21 | 22 | 23 | bool spfa() { 24 | 25 | queue q; 26 | for(int i = 1; i <= n; i ++) { 27 | st[i] = true; 28 | q.push(i); 29 | } 30 | 31 | while(q.size()) { 32 | int t = q.front(); 33 | q.pop(); 34 | st[t] = false; 35 | 36 | for(int i = h[t]; i != -1; i = ne[i]) { 37 | int j = e[i]; 38 | if(dist[j] > dist[t]+w[i]) { 39 | dist[j] = dist[t] + w[i]; 40 | cnt[j] = cnt[t] + 1; 41 | if(cnt[j] >= n) return true; 42 | if(!st[j]) { 43 | st[j] = true; 44 | q.push(j); 45 | } 46 | } 47 | } 48 | } 49 | return false; 50 | } 51 | 52 | 53 | int 54 | main(void) { 55 | 56 | cin >> n >> m; 57 | memset(h, -1, sizeof h); 58 | while (m --) { 59 | int a, b, c; 60 | cin >>a >> b >> c; 61 | add(a, b, c); 62 | } 63 | if(spfa()) cout << "Yes" << endl; 64 | else cout << "No" << endl; 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp3/topsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | int n, m; const int N = 100010; 7 | int e[N], ne[N], h[N], idx; 8 | int q[N], d[N]; 9 | 10 | 11 | /* 12 | 注意在图的存储过程中 13 | 因为有节点和边两个变量 所以一定要注意数组下表是从1开始还是从0开始 14 | 数组可以作为额外存储信息的很好的存储介质 本题中作为存储入度的介质 15 | */ 16 | 17 | void add(int a, int b) { 18 | e[idx] = b; 19 | ne[idx] = h[a]; 20 | h[a] = idx ++; 21 | } 22 | 23 | bool topsort() { 24 | int hh = 0, tt = -1; 25 | for(int i =1; i <= n; i++) //注意初始值是1 因为h数组是从1开始算的 26 | if(!d[i]) q[++tt] = i; 27 | 28 | while(hh <= tt) { 29 | int t = q[hh++]; 30 | for(int i =h[t]; i != -1; i=ne[i]) { 31 | int j = e[i]; 32 | d[j] --; 33 | if(!d[j]) q[++tt] = j; 34 | } 35 | 36 | } 37 | return (tt == n - 1); 38 | } 39 | 40 | int 41 | main(void) { 42 | 43 | cin >> n >> m; 44 | //注意在存储过程中一定要注意 初始化为-1 45 | memset(h, -1, sizeof h); 46 | while(m --) { 47 | int a, b; 48 | cin >> a >> b; 49 | add(a, b); 50 | d[b] ++; 51 | } 52 | 53 | if(topsort()) { 54 | for(int i = 0; i < n; i++) cout << q[i] << endl; 55 | }else cout << "-1" << endl; 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/IxExP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 容斥原理 3 | Inclusion-exclusion principle 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | const int N = 20; 11 | typedef long long LL; 12 | int p[N]; 13 | int n, m; 14 | 15 | 16 | 17 | int 18 | main(void) { 19 | cin >> n >> m; 20 | for(int i = 0; i < m; i ++) cin >> p[i]; 21 | 22 | int res = 0; 23 | //位运算的开始方式 24 | for(int i = 1; i < 1 << m; i++) { 25 | 26 | int cnt = 0, t = 1; 27 | for(int j = 0; j < m; j ++) { 28 | if(i >> j & 1) { 29 | cnt++; 30 | if(static_cast(t) * p[j] > n) { //类型强转置换的C++语法 31 | t = -1; 32 | break; 33 | } 34 | t *= p[j]; 35 | } 36 | } 37 | 38 | 39 | if(t != -1) { 40 | if(cnt % 2) res += n/t; 41 | else res -= n/t; 42 | } 43 | } 44 | 45 | cout << res << endl; 46 | 47 | return 0; 48 | } 49 | 50 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/catalan.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef long long LL; 7 | 8 | const int N = 100010, mod = 1e9 + 7; 9 | 10 | 11 | int qmi(int a, int k, int p) { 12 | int res = 1; 13 | while(k) { 14 | if(k & 1) res = (LL)res * a % p; 15 | a = (LL)a * a % p; 16 | k >>= 1; 17 | } 18 | 19 | return res; 20 | } 21 | 22 | 23 | int 24 | main(void) { 25 | 26 | int n; 27 | cin >> n; 28 | 29 | int res = 1; 30 | int a = 2 * n, b = n; 31 | for(int i = a; i>a - b; i --) res = (LL)res * i %mod; 32 | for(int i = 1; i <= b; i ++) res = (LL)res *qmi(i, mod -2, mod)% mod; 33 | res = (LL)res * qmi(n + 1, mod - 2, mod) % mod; 34 | cout << res << endl; 35 | return 0; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/combinationI.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 2010, mod = 1e9+7; 7 | int c[N][N]; 8 | int n; 9 | 10 | 11 | 12 | void init(){ 13 | 14 | for(int i = 0; i < N; i ++) { 15 | for(int j = 0; j <= i; j ++) 16 | if(!j) c[i][j] = 1; 17 | else c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod; 18 | } 19 | 20 | } 21 | 22 | 23 | int 24 | main(void) { 25 | 26 | cin >> n; 27 | init(); 28 | while(n --){ 29 | int a,b; 30 | cin >> a >> b; 31 | cout << c[a][b] << endl; 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/combinationII.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | const int N = 100010, mod = 1e9 + 7; 6 | int fact[N], infact[N]; 7 | typedef long long LL; 8 | 9 | LL qmi(int a, int b, int p) { 10 | 11 | LL res= 1; 12 | while(b) { 13 | if(b & 1) res = res * a % p; 14 | a = (LL) a * a % p; 15 | b >>= 1; 16 | } 17 | 18 | return res; 19 | } 20 | 21 | 22 | 23 | void init() { 24 | fact[0] = infact[0] = 1; 25 | 26 | for(int i = 1; i < N; i ++) { 27 | fact[i] = (LL)fact[i-1] * i % mod; 28 | infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod; 29 | } 30 | 31 | } 32 | 33 | 34 | 35 | 36 | int 37 | main(void) { 38 | 39 | int n; 40 | cin >> n; 41 | init(); 42 | while(n --) { 43 | int a, b; 44 | cin >> a >> b; 45 | cout << (LL)fact[a] * infact[a - b] %mod * infact[b] % mod; 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/combinationIII.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | typedef long long LL; 7 | 8 | int qmi(int a,int b, int p) { 9 | int res = 1; 10 | while(b) { 11 | if(b & 1) res = (LL)res * a %p; 12 | a = (LL)a * a % p; 13 | b >>= 1; 14 | } 15 | return res; 16 | } 17 | 18 | 19 | int C(int a, int b, int p) { 20 | if(b > a) return 0; 21 | 22 | int res = 1; 23 | for(int i = 1, j = a; i <= b; i ++, j --) { 24 | res = (LL)res *j %p; 25 | res = (LL)res * qmi(i, p-2, p)%p; 26 | } 27 | 28 | return res; 29 | } 30 | 31 | 32 | 33 | 34 | 35 | int lucas(LL a, LL b, int p) { 36 | 37 | if(a < p && b < p) return C(a, b, p); 38 | return (LL)C(a%p, b%p, p) * lucas(a/p, b/p, p)%p; 39 | 40 | } 41 | 42 | int 43 | main(void) { 44 | 45 | int n; 46 | cin >> n; 47 | while(n --) { 48 | 49 | LL a, b; 50 | int p; 51 | cin >> a >> b >> p; 52 | cout << lucas(a, b, p) << endl; 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/combinationIV.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | const int N = 5010; 9 | int primes[N], cnt; 10 | int sum[N]; 11 | bool st[N]; 12 | 13 | /* 14 | 分解因式 15 | 高精度乘法 16 | */ 17 | 18 | void get_primes(int n) { 19 | for(int i = 2; i <= n; i ++) { 20 | if(!st[i]) primes[cnt ++] = i; 21 | for(int j = 0; primes[j] <= n/i; j ++) { 22 | st[primes[j] * i] = true; 23 | if(i % primes[j] == 0) break; 24 | } 25 | } 26 | } 27 | 28 | 29 | int get(int n, int p) { 30 | 31 | int res = 0; 32 | while(n) { 33 | res += n /p; 34 | n /= p; 35 | } 36 | return res; 37 | } 38 | 39 | 40 | vector mul(vector a, int b) { 41 | 42 | vector c; 43 | int t = 0; 44 | for(size_t i = 0; i < a.size(); i ++) { 45 | t += a[i] * b; 46 | c.push_back(t % 10); 47 | t /= 10; 48 | } 49 | 50 | while(t) { 51 | c.push_back(t % 10); 52 | t /= 10; 53 | } 54 | return c; 55 | } 56 | 57 | int 58 | main(void) { 59 | 60 | int a, b; 61 | cin >> a >> b; 62 | 63 | get_primes(a); 64 | for(int i = 0; i < cnt; i ++) { 65 | int p = primes[i]; 66 | sum[i] = get(a, p) - get(b, p) - get(a - b, p); 67 | } 68 | 69 | vector res; 70 | res.push_back(1); 71 | 72 | for(int i = 0; i < cnt; i ++) 73 | for(int j = 0; j< sum[i]; j ++) 74 | res = mul(res, primes[i]); 75 | 76 | for(int i = res.size() - 1; i >= 0; i --) printf("%d", res[i]); 77 | puts(""); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/divisorNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | using namespace std; 9 | 10 | typedef long long LL; 11 | const int N = 110, mod = 1e9 + 7; 12 | 13 | int 14 | main() { 15 | 16 | int n; 17 | cin >> n; 18 | 19 | unordered_map primes; 20 | 21 | 22 | while(n --) { 23 | int x; 24 | cin >> x; 25 | 26 | for(int i = 2; i <= x/i; i ++) 27 | while(x % i == 0){ 28 | x /= i; 29 | primes[i] ++; 30 | } 31 | if(x > 1) primes[x] ++; 32 | } 33 | 34 | LL res = 1; 35 | 36 | for(auto p : primes) res = res * (p.second + 1) % mod; 37 | cout << res << endl; 38 | 39 | return 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/divisorSum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long LL; 9 | const int N = 110, mod = 1e9 + 7; 10 | 11 | int 12 | main() { 13 | 14 | int n; 15 | cin >> n; 16 | 17 | unordered_map primes; 18 | 19 | while(n --) { 20 | int x; 21 | cin >> x; 22 | 23 | for(int i = 2; i <= x/i; i ++) 24 | while(x % i == 0){ 25 | x /= i; 26 | primes[i] ++; 27 | } 28 | if(x > 1) primes[x] ++; 29 | } 30 | 31 | LL res = 1; 32 | for(auto p : primes) { 33 | LL a = p.first, b = p.second; 34 | LL t = 1; 35 | while(b --) t = (t * a + 1) % mod; 36 | res = res * t % mod; 37 | } 38 | 39 | cout << res << endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/euler.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef long long LL; 8 | const int N = 1000010; 9 | 10 | int primes[N], cnt; 11 | int euler[N]; 12 | int phi[N]; 13 | bool st[N]; 14 | 15 | //注意会爆int 16 | void get_eulers(int n) { 17 | euler[1] = 1; 18 | 19 | 20 | for(int i = 2; i <= n; i ++) { 21 | if(!st[i]) { 22 | primes[cnt++] = i; 23 | euler[i] = i - 1; 24 | } 25 | for(int j = 0; primes[j] <= n; j ++) { 26 | int t = primes[j] * i; 27 | st[t] = true; 28 | if(i % primes[j] == 0) { 29 | euler[t] = euler[i] * primes[j]; 30 | break; 31 | } 32 | euler[t] = euler[i] * (primes[j] - 1); 33 | } 34 | } 35 | } 36 | 37 | 38 | LL acwing_geteuler(int n) { 39 | phi[1] = 1; 40 | 41 | int cnt = 0; 42 | LL res = 0; 43 | for(int i = 2; i <= n; i++) { 44 | if(!st[i]) { 45 | primes[cnt ++] = i; 46 | phi[i] = i - 1; 47 | } 48 | //关键是理解线性筛法中的最小质因子 49 | for(int j = 0; primes[j] <= n/i;j ++) { 50 | st[primes[j] * i] = true; 51 | if(i % primes[j] == 0) { 52 | phi[primes[j] * i] = primes[j] * phi[i]; 53 | break; 54 | }else { 55 | phi[primes[j] * i] = (primes[j] - 1) * phi[i]; 56 | } 57 | } 58 | } 59 | 60 | for(int i = 1; i<= n; i++) res += phi[i]; 61 | return res; 62 | } 63 | 64 | int 65 | main(void) { 66 | 67 | int n; 68 | cin >> n; 69 | 70 | get_eulers(n); 71 | 72 | LL res = 0; 73 | for(int i = 1; i <= n; i++) res += euler[i]; 74 | cout << res << endl; 75 | return 0; 76 | } 77 | 78 | //欧拉函数 79 | int 80 | test_euler_main(void) { 81 | int n; 82 | cin >> n; 83 | 84 | while(n--) { 85 | int a; 86 | cin >> a; 87 | 88 | int res = a; 89 | for(int i = 2; i <= a/i; i ++) 90 | if(a%i == 0) { 91 | res = res / i * (i - 1); 92 | while(a%i==0) a/=i; 93 | } 94 | if(a > 1) res = res / a * (a - 1); 95 | cout << res << endl; 96 | } 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/exgcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | typedef long long LL; 5 | 6 | int gcd(int a, int b) { 7 | return (b ? gcd(b, a % b) : a); 8 | } 9 | 10 | int exgcd(int a, int b, int &x, int &y) { 11 | if(!b) { 12 | x = 1, y = 0; 13 | return a; 14 | } 15 | int d = exgcd(b, a % b, y, x); 16 | y -= a / b * x; 17 | return d; 18 | } 19 | 20 | //求线性同余方程 21 | int exgcdII(int a, int b, int &x, int &y) { 22 | if(!b) 23 | { 24 | x = 1, y = 0; 25 | return a; 26 | } 27 | 28 | int d = exgcdII(b, a % b, y, x); 29 | y -= a / b * x; 30 | return d; 31 | } 32 | 33 | 34 | int test_exgcdII(void) { 35 | 36 | int n; 37 | cin >> n; 38 | while(n --){ 39 | int a, b, m, x, y; 40 | cin >> a >> b >> m; 41 | 42 | int d = exgcdII(a, m, x, y); 43 | if(b%d) cout << "impossible" << endl; 44 | else cout << (LL)x*(b/d)%m << endl; 45 | 46 | } 47 | return 0; 48 | } 49 | 50 | int 51 | main(void) { 52 | 53 | int n; 54 | cin >> n; 55 | while(n --) { 56 | int a, b, x, y; 57 | cin >> a >> b; 58 | exgcd(a, b, x, y); 59 | cout << x << ' ' << y << endl; 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/filterprime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | bool st[N]; 8 | int primes[N]; 9 | 10 | 11 | /* 12 | 体会数组st的使用 13 | 在图论算法中和数论中常见的判断数组st 14 | */ 15 | //朴素版本 16 | int filter_prime_normal(int n) { 17 | int cnt = 0; 18 | for(int i = 2; i <= n; i++){ 19 | if(!st[i]) primes[cnt ++] = i; 20 | for(int j = i; j <= n; j +=i) st[j] = true; 21 | } 22 | return cnt; 23 | } 24 | 25 | //对朴素版本的优化 26 | //从j = i开始比较好容易理解 27 | int filter_prime_ai(int n) { 28 | 29 | int cnt = 0; 30 | for(int i = 2; i <= n; i++) { 31 | if(!st[i]) { 32 | primes[cnt ++] = i; 33 | for(int j = i; j <= n; j += i) st[j] = true; 34 | } 35 | } 36 | return cnt; 37 | } 38 | 39 | 40 | //n只会被最小质因子筛掉 和从小到大枚举的性质是一样的 41 | int filter_prime_linear(int n) { 42 | 43 | int cnt = 0; 44 | for(int i = 2; i <= n; i ++) { 45 | if(!st[i]) primes[cnt++] = i; 46 | for(int j = 0; primes[j] <= n/i; j ++) { //prime[j] * i <= n的意思 47 | st[primes[j] * i] = true; 48 | if(i % primes[j] == 0) break; //prime[cnt++] = i; 该句子的意思是到头了 49 | } 50 | } 51 | return cnt; 52 | } 53 | 54 | 55 | int 56 | main(void) { 57 | 58 | int n; 59 | cin >> n; 60 | cout << filter_prime_normal(n) << endl; 61 | cout << filter_prime_ai(n) << endl; 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/gauss.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; const int N = 110; 6 | const double eps = 1e-6; 7 | double a[N][N]; 8 | int n; 9 | 10 | /* 11 | 高斯消元法 12 | */ 13 | int gauss() { 14 | 15 | int r, c; 16 | for(r = 0, c = 0; c < n; c ++) { 17 | //c列 最大值 18 | int t = r; 19 | for(int i = r; i < n + 1; i ++) 20 | if(fabs(a[i][c]) > fabs(a[t][c])) t = i; 21 | 22 | if(fabs(a[t][c]) < eps) continue; 23 | 24 | //换到第一行 列是n + 1列!! 25 | for(int i = c; i < n + 1; i++) swap(a[r][i], a[t][i]); 26 | //第一个值赋值1 27 | for(int i = n; i >= c; i --) a[r][i] /= a[r][c]; 28 | 29 | //清零 30 | for(int i = r + 1; i < n; i ++) 31 | if(fabs(a[i][c]) > eps) 32 | for(int j = n; j >= c; j --) 33 | a[i][j] -= a[r][j] * a[i][c]; 34 | r ++; 35 | } 36 | 37 | if(r < n) { 38 | for(int i = r; i < n; i ++) 39 | if(fabs(a[i][n])) return 2; 40 | return 1; 41 | } 42 | 43 | for(int i = n - 1; i >= 0; i --) 44 | for(int j = i + 1; j < n; j ++) 45 | a[i][n] -= a[j][n] * a[i][j]; 46 | 47 | return 0; 48 | } 49 | 50 | 51 | int 52 | main(void) { 53 | 54 | cin >> n; 55 | for(int i = 0; i < n; i++) 56 | for(int j = 0; j < n + 1; j ++) 57 | cin >> a[i][j]; 58 | 59 | int t = gauss(); 60 | 61 | if(t == 0) { 62 | for(int i = 0; i < n; i ++) printf("%.2lf\n", a[i][n]); 63 | }else if(t == 1) cout << "Infinite group solutions" << endl; 64 | else cout << "No solution" << endl; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | //理解公式的由来 其中a 这b 的出现顺序 7 | int gcd(int a, int b) { 8 | return b ? gcd(b, a % b) : a; 9 | } 10 | 11 | int 12 | main(void) { 13 | 14 | int n; 15 | cin >> n; 16 | while(n --) { 17 | int a, b; 18 | cin >> a >> b; 19 | cout << gcd(a, b) << endl; 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/getdivisor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | vector get_divisor(int x) { 9 | 10 | vector res; 11 | for(int i = 1; i <= x/i; i ++) 12 | if(x % i == 0){ 13 | res.push_back(i); 14 | if(i != x/i) res.push_back(x / i); 15 | } 16 | 17 | sort(res.begin(), res.end()); 18 | return res; 19 | } 20 | 21 | 22 | int 23 | main() { 24 | 25 | int n; 26 | cin >> n; 27 | 28 | while(n --) 29 | { 30 | int x; 31 | cin >> x; 32 | auto res = get_divisor(x); 33 | for(auto x : res) cout << x << ' '; 34 | cout << endl; 35 | } 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/getprime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | 从小到大的枚举使得每一个都是质因子 7 | n中最多包含一个大于sart(x)的质因子 8 | */ 9 | void get_prime(int x) { 10 | for(int i = 2; i <= x / i; i ++) { 11 | if(x % i == 0) { 12 | int s = 0; 13 | while(x%i==0) { 14 | s ++; 15 | x/=i; 16 | } 17 | cout << i << ' ' << s << endl;; 18 | } 19 | } 20 | 21 | //最后一个质因子 22 | if(x > 1) cout << x << ' ' << 1 << endl; 23 | puts(""); 24 | } 25 | 26 | int 27 | main(void) { 28 | 29 | int n; 30 | cin >> n; 31 | while(n --) { 32 | int m; 33 | cin >> m; 34 | get_prime(m); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/isprime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | bool isPrime(int x) { 8 | 9 | if(x < 2) return false; 10 | for(int i = 2; i <= x / i ; i ++) { 11 | if(x % i == 0) return false; 12 | } 13 | return true; 14 | } 15 | 16 | 17 | int 18 | main(void){ 19 | 20 | int n, m; 21 | cin >> n; 22 | while(n --) { 23 | cin >> m; 24 | if(isPrime(m)) cout << "Yes" << endl; 25 | else cout << "No" << endl; 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/nim.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | /* 6 | ^是位运算!!! 7 | 理解上有偏差 8 | */ 9 | 10 | int 11 | main(void) { 12 | 13 | int n, res = 0; 14 | cin >> n; 15 | while(n --) { 16 | int t; 17 | cin >> t; 18 | res ^= t; 19 | } 20 | 21 | if(res) cout << "Yes" << endl; 22 | else cout << "No" << endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/nimsg.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 110, M = 10010; 8 | int n,m; 9 | int s[N], f[M]; //s表示石子个数 f表示sg函数的值 10 | 11 | //记忆化搜索 12 | int sg(int x) { 13 | 14 | if(f[x] != -1) return f[x]; 15 | 16 | unordered_set S; 17 | for(int i = 0; i < m; i ++) { 18 | int sum = s[i]; 19 | if(x >= sum) S.insert(sg(x - sum)); //递归搜索 20 | } 21 | 22 | for(int i =0; ; i++) 23 | if(!S.count(i)) 24 | return f[x] = i; 25 | } 26 | 27 | 28 | int 29 | main(void) { 30 | 31 | cin >> m; 32 | for(int i = 0; i< m; i ++) cin >> s[i]; //初始化石子数量 33 | memset(f, -1, sizeof f); 34 | cin >> n; 35 | int res = 0; 36 | while(n --) { //直接计算每一堆石子的sg函数值 37 | int x; 38 | cin >> x; 39 | res ^= sg(x); 40 | } 41 | 42 | if(res) cout << "Yes" << endl; 43 | else cout << "No" << endl; 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/qmi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | typedef long long LL; 6 | 7 | LL qmi(int a, int b, int p) { 8 | LL res = 1; 9 | while(b) { 10 | if(b >> 1) res = res * a % p; 11 | a = a * (LL)a % p; 12 | b >>= 1; 13 | } 14 | 15 | return res; 16 | } 17 | 18 | int 19 | main(void) { 20 | 21 | int n; 22 | cin >> n; 23 | while(n --) { 24 | int a, b, p; 25 | cin >> a >> b >> p; 26 | 27 | cout << qmi(a, b, p) << endl; 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp4/qmiinverse.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | typedef long long LL; 5 | 6 | 7 | int qmi(int a, int b, int p) { 8 | 9 | LL res = 1; 10 | while(b) { 11 | if(b & 1) res = res * a % p; 12 | a = a * (LL)a % p; 13 | b >>= 1; 14 | } 15 | return res; 16 | } 17 | 18 | int 19 | main(void) { 20 | 21 | int n; 22 | cin >> n; 23 | while(n --) { 24 | int a, p; 25 | cin >> a >> p; 26 | if(a % p == 0) cout << "impossible" << endl; 27 | else cout << qmi(a, p - 2, p) << endl; 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/bitscount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | const int n = 100010; 8 | 9 | 10 | //截取v[l]~v[r] 11 | int get(vector num, int l, int r) { 12 | int res = 0; 13 | for(int i = l; i >= r; i --) res = res*10 + num[i]; 14 | return res; 15 | } 16 | 17 | int power10(int x) { 18 | int res = 1; 19 | while(x --) res *= 10; 20 | return res; 21 | } 22 | 23 | 24 | 25 | //从1~n中判断x出现的次数 26 | int counts(int n, int x) { 27 | 28 | if(!n) return 0; 29 | std::vector v; 30 | while (n) { 31 | v.push_back(n % 10); 32 | n /= 10; 33 | } 34 | 35 | n = v.size(); 36 | int res = 0; 37 | for(int i = n - 1 - !x; i >= 0; i--){ //从最高位开始枚举 38 | 39 | //第一种情况 40 | if(i < n - 1) { 41 | res += get(v, n - 1, i + 1) * power10(i); 42 | if(!x) res -= power10(i); //特殊情况x = 0 43 | } 44 | 45 | //第二种情况 46 | if (v[i] == x) { 47 | res += get(v, i - 1, 0) + 1; 48 | } else if(v[i] > x){ 49 | res += power10(i); 50 | } 51 | } 52 | return res; 53 | } 54 | 55 | 56 | int main(void) 57 | { 58 | 59 | int a,b; 60 | while (cin >> a >> b, a || b) { 61 | if(a > b) swap(a, b); 62 | for(int i = 0; i <= 9; i ++) 63 | cout << counts(b, i) - counts(a - 1, i) << ' '; 64 | puts(""); 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/complete_Pack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | const int N = 1010; 6 | int v[N], w[N]; 7 | int n, m; 8 | 9 | 10 | //朴素版本 11 | void testIII() { 12 | int f[N][N]; 13 | cin >> n >> m; 14 | 15 | for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i]; 16 | 17 | 18 | for(int i = 1; i <= n; i ++) 19 | for(int j = 0; j <= m; j ++) 20 | for(int k = 0; k * v[i] <= j; k ++) 21 | f[i][j] = max(f[i][j], f[i-1][j-v[i] * k] + w[i] * k); 22 | 23 | cout << f[n][m] << endl; 24 | return ; 25 | } 26 | 27 | 28 | 29 | //状态转移方程的二维优化 30 | void testII() { 31 | 32 | int f[N][N]; 33 | cin >> n >> m; 34 | 35 | for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i]; 36 | 37 | for(int i = 1; i <= n; i ++) 38 | for(int j = 0; j <= m; j ++) 39 | { 40 | f[i][j] = f[i-1][j]; 41 | if( j>= v[i]) f[i][j] = max(f[i][j], f[i][j-v[i]] + w[i]); 42 | } 43 | 44 | cout << f[n][m] << endl; 45 | return ; 46 | } 47 | 48 | 49 | //滚动数组一维优化 50 | void testI() { 51 | 52 | int f[N]; 53 | cin >> n >> m; 54 | 55 | for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i]; 56 | 57 | for(int i = 1; i <= n; i++) 58 | for(int j = v[i]; j <= m; j ++) 59 | f[j] = max(f[j], f[j-v[i]] + w[i]); 60 | 61 | cout << f[m] << endl; 62 | return ; 63 | } 64 | 65 | int 66 | main(void) { 67 | 68 | testI(); 69 | testII(); 70 | testIII(); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/hamilton.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 21; 7 | int f[1 << 20][N]; 8 | int w[N][N]; 9 | int n; 10 | 11 | int main(void) 12 | { 13 | 14 | cin >> n; 15 | for(int i = 0; i < n; i++) 16 | for(int j = 0; j < n; j ++) 17 | cin >> w[i][j]; 18 | 19 | memset(f, 0x3f, sizeof f); 20 | f[1][0] = 0; //起点不需要代价 21 | for(int i = 1; i< (1 << n); i ++) // 方案集合 二进制状态表示路径 22 | for(int j = 0; j < n; j ++) //表示第几点 23 | if(i >> j & 1) 24 | for(int k = 0; k < n; k++) 25 | f[i][j] = min(f[i][j], f[i^(1 << j)][k] + w[k][j]); 26 | cout << f[(1 << n) - 1][n-1] << endl; 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/lcs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | longest common subsequence 3 | */ 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | const int N = 1010; 11 | char a[N], b[N]; 12 | int f[N][N]; 13 | int n, m; 14 | 15 | 16 | 17 | int 18 | main(void) { 19 | 20 | scanf("%d%d", &n, &m); 21 | scanf("%s%s", a+1, b+1); 22 | 23 | for(int i = 1; i <= n; i++) { 24 | for(int j = 1; j <= m; j ++) { 25 | f[i][j] = max(f[i-1][j], f[i][j-1]); 26 | if(a[i] == b[j]) f[i][j] = max(f[i][j], f[i-1][j-1] + 1); 27 | } 28 | } 29 | 30 | 31 | cout << f[n][m] << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/lis.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Longest Increasing Subsequence 3 | */ 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | const int N = 10010; 10 | int a[N]; 11 | int f[N]; 12 | int n; 13 | 14 | 15 | int 16 | main(void) { 17 | 18 | cin >> n; 19 | for(int i = 1; i <= n; i ++) cin >> a[i]; 20 | 21 | 22 | for(int i = 1; i <= n; i ++) { 23 | f[i] = 1; //只有a[i]一个数 24 | for(int j = 1; j < i; j ++) 25 | if(a[j] < a[i]) 26 | f[i] = max(f[i], f[j] + 1); 27 | 28 | } 29 | 30 | int res = 0; 31 | for(int i = 1; i <= n; i ++) 32 | res = max(res, f[i]); 33 | 34 | cout << res; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/mondrian.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 12, M = 1 << N; 8 | bool st[M]; 9 | long long f[N][M]; 10 | int n, m; 11 | 12 | 13 | 14 | int main(void) 15 | { 16 | while (cin >> n >> m, n || m) { 17 | 18 | //预处理 19 | for(int i = 0; i < 1 << n; i ++){ //n列 20 | int cnt = 0; 21 | st[i] = true; 22 | 23 | for(int j = 0; j < n; j ++) 24 | if(i >> j & 1) { 25 | if(cnt & 1) st[i] = false; 26 | cnt = 0; 27 | }else cnt++; 28 | if(cnt & 1) st[i] = false; 29 | } 30 | 31 | //状态压缩 32 | memset(f, 0, sizeof f); 33 | f[0][0] = 1; 34 | for(int i = 1; i<= m; i++) 35 | for(int j = 0; j < 1<< n; j ++) 36 | for(int k = 0; k < 1 << n; k ++) 37 | if((j & k) == 0 && st[j | k]) //st[j|k] ? 38 | f[i][j] += f[i-1][k]; 39 | cout << f[m][0] << endl; 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/multiple_Pack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 110; 7 | /* 8 | const int N = 25000, M = 2010; 优化版本的数据集 9 | 之所以开成25000的数组是因为 s[i]最多是2000 因此每一个s[i]最多开成log2000 = 12左右而共有1000类物品 10 | 因此2000*log2000 <= 25000 11 | */ 12 | int v[N], w[N], s[N]; 13 | int n, m; 14 | 15 | 16 | /* 17 | 多重背包朴素版本 18 | 状态转移方程: f[i][j] = max(f[i-1][j-v[i] * k] + w[i] * k); (k <= s[i]) 19 | */ 20 | void testIII() { 21 | 22 | int f[N][N] = {}; 23 | cin >> n >> m; 24 | for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i] >> s[i]; 25 | 26 | for(int i = 1; i <= n; i ++) 27 | for(int j = 0; j <= m; j ++) 28 | for(int k = 0; k <= s[i] && k * v[i] <= j; k ++) 29 | f[i][j] = max(f[i][j], f[i-1][j-v[i] * k] + w[i] * k); 30 | 31 | cout << f[n][m] << endl; 32 | return; 33 | } 34 | 35 | /* 36 | 多重背包优化版本 37 | 对s[i]拆分 然后采用01背包方式 38 | */ 39 | void testI() { 40 | 41 | int f[N] = {0}; 42 | cin >> n >> m; 43 | int cnt =0; 44 | 45 | for(int i = 1; i<= n; i++) { 46 | int a, b, s; 47 | cin >> a>> b >> s; 48 | 49 | int k = 1; 50 | while(k <= s) { 51 | cnt ++; 52 | v[cnt] = a * k; 53 | w[cnt] = b * k; 54 | s -= k; 55 | k <<= 1; 56 | } 57 | 58 | if(s > 0) { 59 | cnt ++; 60 | v[cnt] = a * s; 61 | w[cnt] = b * s; 62 | } 63 | } 64 | 65 | 66 | n = cnt; 67 | for(int i = 1; i <= n; i ++) 68 | for(int j = m; j >= v[i]; j --) 69 | f[j] = max(f[j], f[j-v[i]] + w[i]); 70 | 71 | 72 | cout << f[m] << endl; 73 | return; 74 | } 75 | 76 | int 77 | main(void) { 78 | 79 | // testIII(); 80 | testI(); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/packetback_Pack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 110; 7 | int n, m; 8 | int v[N][N], w[N][N], s[N]; 9 | int f[N]; 10 | 11 | 12 | 13 | int 14 | main(void) { 15 | 16 | cin >> n >> m; 17 | for(int i = 1; i <= n; i ++){ 18 | cin >> s[i]; 19 | for(int j = 0; j < s[i]; j ++) { 20 | cin >> v[i][j] >> w[i][j]; 21 | } 22 | } 23 | 24 | 25 | for(int i = 1; i <= n; i ++) 26 | for(int j = m; j >= 0; j --) 27 | for(int k = 0; k < s[i]; k ++) 28 | if(v[i][k] <= j) 29 | f[j] = max(f[j], f[j-v[i][k]] + w[i][k]); 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/stoneMerging.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | const int N = 310; 11 | int n; 12 | int s[N]; 13 | int f[N][N]; 14 | 15 | 16 | int 17 | main(void) { 18 | 19 | cin >> n; 20 | for(int i = 1; i <= n; i ++) cin >> s[i]; 21 | 22 | //处理前缀和 23 | for(int i = 1; i <= n; i ++) s[i] += s[i-1]; 24 | 25 | //枚举所有状态 26 | for(int len = 2; len <= n; len ++) //长度 27 | for(int i = 1; i +len -1 <= n; i ++) { //起点 28 | int l = i, r = i + len - 1; 29 | f[l][r] = 1e8; 30 | for(int k = l; k < r; k ++) //区间 31 | f[l][r] = min(f[l][r], f[l][k] + f[k+1][r] + s[r] - s[l-1]); 32 | } 33 | 34 | cout << f[1][n] << endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 10010; 8 | bool st[N]; 9 | int primes[N]; 10 | 11 | 12 | 13 | 14 | //线性筛法 15 | int get_primes(int x) { 16 | 17 | int cnt = 0; 18 | for(int i = 2; i <= x; i ++){ 19 | if(!st[i]) primes[cnt ++] = i; 20 | for(int j = 0; primes[j] <= x/i; j ++){ 21 | st[primes[j] * i] = true; 22 | if(i % primes[j] == 0) break; 23 | } 24 | } 25 | 26 | 27 | return cnt; 28 | } 29 | 30 | 31 | 32 | int get(vector num, int l, int r) { 33 | int res = 0; 34 | for(int i = l; i >= r; i --) res = res * 10 + num[i]; 35 | return res; 36 | } 37 | 38 | int power10(int x) { 39 | int res = 1; 40 | while (x --) res *= 10; 41 | return res; 42 | } 43 | 44 | int countbits(int x, int n) { 45 | std::vector v; 46 | 47 | while (n) { 48 | v.push_back(n % 10); 49 | n /= 10; 50 | } 51 | 52 | n = v.size(); 53 | int res = 0; 54 | for(int i = n - 1; i >= 0; i --){ 55 | 56 | if (i < n - 1) { 57 | res += get(v, n - 1, i + 1) * power10(i); 58 | if(!x) res -= power10(i); 59 | } 60 | 61 | if (v[i] == x) { 62 | res += get(v, i + 1, 0) + 1; 63 | } else if(v[i] > x){ 64 | res += power10(i); 65 | } 66 | } 67 | return res; 68 | } 69 | 70 | 71 | int test_main(void) 72 | { 73 | 74 | int a, b; 75 | while (cin >> a >> b, a || b) { 76 | 77 | if(a > b) swap(a, b); 78 | for(int i = 0; i <= 9; i ++) cout << countbits(b, i) - countbits(a - 1, i) << ' '; 79 | puts(""); 80 | } 81 | return 0; 82 | } 83 | 84 | 85 | 86 | long long f[N][N]; 87 | int n, m; 88 | int main(void) 89 | { 90 | while (cin >> n >> m, n || m) { 91 | for(int i = 0; i < 1 << n; i++){ 92 | int cnt = 0; 93 | st[i] = true; 94 | for(int j = 0; j < n; j ++) 95 | if(i >> j & 1) { 96 | if(cnt & 1) st[i] = false; 97 | cnt = 0; 98 | }else cnt ++; 99 | } 100 | 101 | memset(f, 0, sizeof f); 102 | 103 | } 104 | 105 | return 0; 106 | } 107 | 108 | 109 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 510, INF = 1e9 + 7; 7 | int f[N][N], a[N][N]; 8 | int n; 9 | 10 | 11 | //从底部到顶部的一种优化 12 | void down2up() { 13 | 14 | cin >> n ; 15 | 16 | for(int i = 1; i <= n; i++) 17 | for(int j = 1; j <= i; j ++) 18 | cin >> a[i][j]; 19 | 20 | for(int i = n- 1; i >= 1; i --) 21 | for(int j = 1; j <= i; j ++) 22 | a[i][j] = max(a[i+1][j] + a[i][j], a[i+1][j+1] + a[i][j]); 23 | 24 | cout << a[1][1] << endl; 25 | 26 | } 27 | 28 | void 29 | up2down(void) { 30 | 31 | cin >> n; 32 | for(int i = 1; i<= n; i ++) 33 | for(int j = 1; j <= i; j ++) 34 | cin >> a[i][j]; 35 | 36 | for(int i = 0; i<= n; i ++) 37 | for(int j = 0; j <= i + 1; j ++) 38 | f[i][j] = -INF; 39 | 40 | f[1][1] = a[1][1]; 41 | for(int i = 2; i<=n; i ++) 42 | for(int j = 1; j <= i; j ++) 43 | f[i][j] = max(f[i-1][j-1] + a[i][j], f[i-1][j] + a[i][j]); 44 | 45 | 46 | 47 | int res = -INF; 48 | for(int i = 1; i<= n; i ++) 49 | res = max(res, f[n][i]); 50 | 51 | cout << res << endl; 52 | } 53 | 54 | 55 | int 56 | main(void) { 57 | 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp5/zero_one_Pack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | const int N = 1010; 6 | int v[N], w[N]; 7 | int f[N][N]; 8 | int n, m; 9 | int ff[N]; 10 | 11 | //滚动数组优化版本 12 | int oneD_main(void) { 13 | 14 | cin >> n >> m; 15 | for(int i = 1; i<= n; i ++) cin >> v[i] >> w[i]; 16 | 17 | for(int i = 1; i <= n; i ++) 18 | for(int j = 0; j <= m; j ++) 19 | ff[j] = max(ff[j], ff[j-v[i]] + w[i]); 20 | 21 | cout << ff[m] << endl; 22 | return 0; 23 | } 24 | 25 | //朴素版本 26 | int 27 | twoD_main(void) { 28 | 29 | cin >> n >> m; 30 | for(int i = 1;i <= n; i++) cin >> v[i] >> w[i]; 31 | 32 | for(int i = 1; i<= n; i ++) 33 | for(int j = 0; j <= m; j ++){ 34 | f[i][j] = f[i-1][j]; 35 | if(j >= v[i]) f[i][j] = max(f[i-1][j], f[i-1][j-v[i]] + w[i]); 36 | } 37 | 38 | cout << f[n][m] << endl; 39 | return 0; 40 | } 41 | 42 | 43 | 44 | int 45 | main(void) { 46 | 47 | oneD_main(); 48 | twoD_main(); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/exp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 50010; 7 | int n; 8 | 9 | typedef pair PII; 10 | PII cow[N]; 11 | 12 | int main(void) 13 | { 14 | cin >> n ; 15 | int w, s; 16 | for(int i = 0; i < n; i++) { 17 | cin >> w >> s; 18 | cow[i] = {w, s}; 19 | } 20 | 21 | sort(cow, cow + n); 22 | int res = -2e9; 23 | int sum = 0; 24 | for(int i = 0; i < n; i++){ 25 | int w = cow[i].second, s = cow[i].first - w; 26 | res = max(res, sum - s); 27 | sum += w; 28 | } 29 | 30 | cout << res << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/huffman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 10010; 8 | 9 | 10 | int main(void) 11 | { 12 | int n; 13 | cin >> n; 14 | priority_queue, greater > heap; 15 | 16 | while(n --) { 17 | int x; 18 | cin >> x; 19 | heap.push(x); 20 | } 21 | 22 | int res = 0; 23 | while (heap.size() > 1) { 24 | int a = heap.top(); heap.pop(); 25 | int b = heap.top(); heap.pop(); 26 | res += a + b; 27 | heap.push(a + b); 28 | } 29 | 30 | cout << res << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/inequalityAbsolute.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | int n; 8 | int a[N]; 9 | 10 | int main(void) 11 | { 12 | 13 | cin >> n; 14 | 15 | for(int i = 0; i < n; i++) cin >> a[i]; 16 | 17 | sort(a, a + n); 18 | 19 | int res = 0; 20 | for(int i = 0; i < n; i++){ 21 | res += abs(a[i] - a[n/2]); 22 | } 23 | 24 | cout << res << endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/rangeL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | struct Range{ 8 | 9 | int l , r; 10 | bool operator < (const Range & W) const { 11 | return r < W.r; 12 | } 13 | }range[N]; 14 | 15 | 16 | 17 | 18 | 19 | int 20 | main(void) 21 | { 22 | int n; 23 | cin >> n; 24 | for(int i= 0; i> a >> b; 27 | range[i].l = a; 28 | range[i].r = b; 29 | } 30 | 31 | sort(range, range + n); 32 | 33 | int res = 0; 34 | int ed = -1e9; 35 | for(int i = 0; i < n; i++){ 36 | if (range[i].l > ed) { 37 | res ++; 38 | ed = range[i].r; 39 | } 40 | } 41 | 42 | 43 | cout << res << endl; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/rangeM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | const int N = 100010; 8 | int n; 9 | struct Range{ 10 | 11 | int l, r; 12 | bool operator < (const Range & W)const { 13 | // return l < W.l; 14 | return r < W.r; 15 | } 16 | 17 | }range[N]; 18 | 19 | 20 | 21 | int 22 | main(void) { 23 | 24 | cin >> n; 25 | for(int i = 0; i < n; i++) { 26 | int l, r;q 27 | cin >> l >> r; 28 | range[i].l = l; 29 | range[i].r = r; 30 | } 31 | 32 | sort(range, range + n); 33 | 34 | int res = 0; 35 | int ed = -1e9; 36 | for(int i = 0; i < n; i ++) { 37 | if(range[i].l > ed) { 38 | res ++; 39 | ed = range[i].r; 40 | } 41 | } 42 | 43 | cout << res << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/rangecover.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 100010; 7 | int n; 8 | struct Range{ 9 | 10 | int l, r; 11 | bool operator < (const Range & W) const { 12 | return l < W.l; 13 | } 14 | }range[N]; 15 | 16 | 17 | 18 | int 19 | main(void) { 20 | 21 | 22 | int st, ed; 23 | cin >> st >> ed; 24 | cin >> n; 25 | for(int i = 0; i < n; i++) { 26 | int l , r; 27 | cin >> l >> r; 28 | range[i] = {l, r}; 29 | } 30 | 31 | 32 | sort(range, range + n); 33 | int res = 0; 34 | bool success = false; 35 | 36 | for(int i = 0; i < n; i++) { 37 | int j = i, r = -2e9; 38 | while(j < n && range[j].l <= st) { 39 | r = max(r, range[j].r); 40 | j ++; 41 | } 42 | 43 | 44 | if(r < st) { 45 | res = -1; 46 | break; 47 | } 48 | res ++; 49 | if(r >= ed) { 50 | success = true; 51 | break; 52 | } 53 | 54 | st = r; 55 | j = j - 1; 56 | } 57 | 58 | 59 | if(!success) res = -1; 60 | cout << res << endl; 61 | return 0; 62 | } 63 | 64 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/rangediv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | const int N = 100010; 8 | int n; 9 | struct Range{ 10 | 11 | int l, r; 12 | bool operator < (const Range & W) const { 13 | return l < W.l; 14 | } 15 | 16 | }range[N]; 17 | 18 | 19 | int 20 | main(void) { 21 | 22 | cin >> n; 23 | for(int i = 0; i < n; i++) { 24 | int l, r; 25 | cin >> l >> r; 26 | range[i].l = l; 27 | range[i].r = r; 28 | } 29 | 30 | 31 | sort(range, range + n); 32 | priority_queue, greater > heap; 33 | 34 | for(int i = 0; i < n; i ++) { 35 | auto r = range[i]; 36 | if(heap.empty() || heap.top() >= r.l) heap.push(r.r); 37 | else { 38 | heap.pop(); 39 | heap.push(r.r); 40 | } 41 | } 42 | cout << heap.size() << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Acwing/0primaryAlgorithm/chp6/sequenceInequality.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | 排序不等式 7 | 可能爆int 8 | */ 9 | using namespace std; 10 | const int N =100010; 11 | int a[N]; 12 | 13 | 14 | int main(void) 15 | { 16 | 17 | int n; 18 | cin >> n; 19 | for(int i = 0; i < n; i++) { 20 | int x; 21 | cin >> x; 22 | a[i] = x; 23 | } 24 | 25 | sort(a, a + n); 26 | 27 | int res = 0; 28 | for(int i = 0; i < n; i ++){ 29 | res += a[i] * (n - i - 1); 30 | } 31 | 32 | cout << res << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Acwing/1advancedAlgorithm/README.md: -------------------------------------------------------------------------------- 1 | # 高级算法模板 2 | 3 | 1. 动态规划——从集合角度考虑DP问题 4 | 5 | 1.1 数字三角形模型 6 | 1.2 最长上升子序列模型 7 | 1.3 背包模型 8 | 1.4 状态机模型 9 | 1.5 状态压缩DP 10 | 1.6 区间DP 11 | 1.7 树形DP 12 | 1.8 数位DP 13 | 1.9 单调队列优化的DP问题 14 | 1.10 斜率优化的DP问题 15 | 16 | 2. 搜索 17 | 18 | 2.1 BFS 19 | 2.1.1 Flood Fill 20 | 2.1.2 最短路模型 21 | 2.1.3 多源BFS 22 | 2.1.4 最小步数模型 23 | 2.1.5 双端队列广搜 24 | 2.1.6 双向广搜 25 | 2.1.7 A* 26 | 2.2 DFS 27 | 2.2.1 连通性模型 28 | 2.2.2 搜索顺序 29 | 2.2.3 剪枝与优化 30 | 2.2.4 迭代加深 31 | 2.2.5 双向DFS 32 | 2.2.6 IDA* 33 | 34 | 35 | 36 | 3. 图论 37 | 38 | 3.1.1 单源最短路的建图方式 39 | 3.1.2 单源最短路的综合应用 40 | 3.1.3 单源最短路的扩展应用 41 | 3.2 floyd算法及其变形 42 | 3.3.1 最小生成树的典型应用 43 | 3.3.2 最小生成树的扩展应用 44 | 3.4 SPFA求负环 45 | 3.5 差分约束 46 | 3.6 最近公共祖先 47 | 3.7 有向图的强连通分量 48 | 3.8 无向图的双连通分量 49 | 3.9 二分图 50 | 3.10 欧拉回路和欧拉路径 51 | 3.11 拓扑排序 52 | 53 | 54 | 55 | 4. 高级数据结构 56 | 57 | 4.1 并查集 58 | 4.2 树状数组 59 | 4.3.1 线段树(一) 60 | 4.3.2 线段树(二) 61 | 4.4 可持久化数据结构 62 | 4.5 平衡树——Treap 63 | 4.6 AC自动机 64 | 65 | 66 | 5. 数学知识 67 | 68 | 数论:质数、约数、同余 69 | 组合数学:加法原理、乘法原理、排列数、组合数、二项式定理 70 | Lucas定理、Catalan数 71 | 矩阵乘法、高斯消元与线性空间 72 | 容斥原理与莫比乌斯函数 73 | 概率与数学期望 74 | 博弈论与SG函数 75 | 76 | 77 | 6. 其他算法 78 | 79 | 模拟、字符串处理 80 | 递推与递归 81 | 排序 82 | 贪心 -------------------------------------------------------------------------------- /Acwing/1advancedAlgorithm/chpS1/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | const int N = 100010; 8 | int e[N], ne[N], h[N], idx; 9 | int q[N], d[N]; 10 | int n, m; 11 | 12 | 13 | void add(int a, int b) { 14 | 15 | e[idx] = b; 16 | ne[idx] = h[a]; 17 | h[a] = idx ++; 18 | } 19 | 20 | 21 | int bfs() { 22 | 23 | int hh = 0, tt = 0; 24 | q[0] = 1; //因为是从第一个点 25 | memset(d, -1, sizeof d); 26 | d[1] = 0; //从第一个点 27 | 28 | 29 | while(hh <= tt) { 30 | int t = q[hh++]; 31 | for(int i = h[t]; i != -1; i = ne[i]) { 32 | int j = e[i]; 33 | if(d[j] == -1) { 34 | d[j] = d[t] + 1; 35 | q[++tt] = j; 36 | } 37 | } 38 | } 39 | 40 | return d[n]; 41 | } 42 | 43 | 44 | 45 | 46 | int main(void) 47 | { 48 | cin >> n >> m; 49 | memset(h, -1, sizeof h); 50 | 51 | while(m --) { 52 | int a,b; 53 | cin >> a >> b; 54 | add(a, b); 55 | } 56 | 57 | cout << bfs() << endl; 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Acwing/2internship/README.md: -------------------------------------------------------------------------------- 1 | # 笔试面试专题 2 | 3 | -------------------------------------------------------------------------------- /Acwing/2internship/chp1-1.cpp: -------------------------------------------------------------------------------- 1 | /*蛇形矩阵*/ 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | const int N = 110; 9 | int n, m; 10 | int res[N][N]; 11 | int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; 12 | 13 | 14 | int main(void) 15 | { 16 | cin >> n >> m; // n 行 m 列 17 | 18 | //填写 19 | for(int x = 0, y = 0, k = 1, d = 0; k <= n*m; k ++) { 20 | 21 | res[x][y] = k; 22 | int a = x + dx[d], b = y + dy[d]; 23 | if(a < 0 || a >= n || b < 0 || b >= m || res[a][b]) { //核心: 边界判断 24 | d = (d + 1) % 4; 25 | a = x + dx[d], b = y + dy[d]; 26 | } 27 | x = a, y = b; 28 | } 29 | 30 | 31 | //输出 32 | for(int x = 0; x < n; x ++) { 33 | for(int y = 0; y < m; y ++) { 34 | cout << res[x][y] << ' '; 35 | } 36 | cout << endl; 37 | } 38 | 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Acwing/2internship/chp1-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | //数组快排 9 | const int N = 100010; 10 | int a[N]; 11 | int n; 12 | void quickSort(int q[], int l, int r){ 13 | 14 | if(l >= r) return; 15 | 16 | int x = q[l], i = l - 1, j = r + 1; 17 | 18 | while(i < j) { 19 | do i ++; while(q[i] < x); 20 | do j --; while(q[j] > x); 21 | if(i < j) swap(a[i], a[j]); 22 | } 23 | 24 | quickSort(q, l, j); 25 | quickSort(q, j + 1, r); 26 | } 27 | void arrayTest() { 28 | cin >> n; 29 | for(int i = 0; i < n; i ++) cin >> a[i]; 30 | quickSort(a, 0, n - 1); 31 | for(int i = 0; i < n; i ++) cout << a[i] << ' '; 32 | return ; 33 | } 34 | 35 | /*链式快排*/ 36 | struct ListNode{ 37 | int val; 38 | ListNode *next; 39 | ListNode(int x) : val(x), next(NULL) {} 40 | }; 41 | 42 | class Solution 43 | { 44 | public: 45 | Solution (); 46 | virtual ~Solution (); 47 | 48 | ListNode* get_tail(ListNode *head) { 49 | while(head->next) head = head->next; 50 | return head; 51 | } 52 | 53 | ListNode* quickSort(ListNode *head) { 54 | 55 | if(!head || !head->next) return head; 56 | 57 | auto left = new ListNode(-1), mid = new ListNode(-1), right = new ListNode(-1); 58 | auto ltail = left, mtail = mid, rtail = right; 59 | int val = head->val; 60 | for(auto p = head; p; p = p->next){ 61 | if(p->val < val) ltail = ltail->next = p; 62 | else if(p->val == val) mtail = mtail->next = p; 63 | else rtail = rtail->next = p; 64 | } 65 | 66 | ltail->next = mtail->next = rtail->next = NULL; 67 | 68 | left->next = quickSort(left->next); 69 | right->next = quickSort(right->next); 70 | 71 | //拼接 72 | get_tail(left)->next = mid->next; 73 | get_tail(left)->next = right->next; 74 | return left->next; 75 | } 76 | 77 | 78 | }; 79 | 80 | 81 | int main(void) 82 | { 83 | arrayTest(); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /Acwing/2internship/chp1-3.cpp: -------------------------------------------------------------------------------- 1 | /*寻找矩阵的极小值*/ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | const int N = 100010; 9 | int res[N][N]; 10 | 11 | 12 | class Solution 13 | { 14 | public: 15 | Solution (); 16 | virtual ~Solution (); 17 | 18 | int query(int x, int y) { 19 | return res[x][y]; 20 | } 21 | 22 | 23 | vector getMinmumValue(int n) { 24 | 25 | const int INF = 1e8; 26 | int l = 0, r = n - 1; 27 | while(l < r){ 28 | int mid = (l + r) >> 1; 29 | int min = INF, k; 30 | for(int i = 0; i < n; i ++){ 31 | int x = this->query(i, mid); 32 | if(min > x){ 33 | min = x; 34 | k = i; 35 | } 36 | } 37 | 38 | int left = mid ? this->query(k, mid - 1) : INF; 39 | int right = mid + 1 < n ? this->query(k , mid + 1) : INF; 40 | 41 | if(left > min && right > min) return {k, mid}; 42 | if(left < min) r = mid - 1; 43 | else l = mid + 1; 44 | } 45 | 46 | int min = INF, k; 47 | for(int i = 0; i < n; i++) { 48 | int x = this->query(i, l); 49 | if(min > x) { 50 | min = x; 51 | k = i; 52 | } 53 | } 54 | return {k, l}; 55 | 56 | } 57 | 58 | }; 59 | -------------------------------------------------------------------------------- /Acwing/2internship/chp1-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | const int N = 110, M = 11; 7 | int n, m; 8 | int f[N][M]; 9 | 10 | /*动态规划 11 | f[i][j]存的是次数 12 | O(n^2m) 13 | */ 14 | int DP1(void) 15 | { 16 | while(cin >> n >> m){ 17 | 18 | for(int i = 1; i <= n; i ++) f[i][1] = i; 19 | for(int i = 1; i <= m; i ++) f[1][i] = 1; 20 | 21 | for(int i = 2; i <= n; i++) 22 | for(int j = 2; j <= m; j ++) { 23 | f[i][j] = f[i][j-1]; 24 | for(int k = 1; k <= i; k ++) 25 | f[i][j] = min(f[i][j], max(f[k-1][j-1], f[i-k][j]) + 1); 26 | } 27 | 28 | cout << f[n][m] << endl; 29 | } 30 | return 0; 31 | } 32 | 33 | 34 | 35 | /* 36 | 动态规划 37 | f[i][j]存的是长度 38 | O(nm) 39 | */ 40 | int DP2(void) { 41 | 42 | while(cin >> n >> m){ 43 | for(int i = 1; i <= n; i ++) { 44 | for(int j = 1; j <= m; j ++) f[i][j] = f[i-1][j] + f[i-1][j-1] + 1; 45 | if(f[i][m] >= n) { 46 | printf("%d\n", i); 47 | break; 48 | } 49 | } 50 | } 51 | return 0; 52 | } 53 | 54 | 55 | int main(void) 56 | { 57 | DP1(); 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /Acwing/2internship/chp2-5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | struct ListNode { 9 | int val; 10 | ListNode *next; 11 | ListNode(int x) : val(x), next(NULL) {} 12 | }; 13 | 14 | 15 | 16 | //迭代版本 17 | ListNode *reverseListN(ListNode *head) { 18 | ListNode *prev = NULL, *cur = head; 19 | while(cur) { 20 | auto next = cur->next; 21 | cur->next = prev; 22 | prev = cur; 23 | cur = next; 24 | } 25 | 26 | return prev; 27 | } 28 | 29 | 30 | 31 | //递归版本 32 | ListNode *reverseListR(ListNode *head) { 33 | 34 | if(!head || !head->next) return head; 35 | 36 | auto tail = reverseListR(head->next); 37 | head->next->next = head; 38 | head->next = nullptr; 39 | 40 | return tail; 41 | } 42 | 43 | 44 | int main(void) 45 | { 46 | 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Acwing/2internship/chp2-6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 8 | 单调栈 9 | 理解贪心的本质 及其与DP的区别 10 | 11 | */ 12 | 13 | int main(void) 14 | { 15 | 16 | string num; 17 | int k; 18 | cin >> num >> k; 19 | string res = "0"; 20 | for(unsigned i = 0; i < num.size(); i ++) { 21 | while(k && num[i] < res.back()) { 22 | res.pop_back(); 23 | k --; 24 | } 25 | res += num[i]; 26 | } 27 | 28 | while(k --) res.pop_back(); 29 | unsigned i = 0; 30 | while(i < res.size() && res[i] == '0') i++; 31 | 32 | if(i == res.size()) puts("00"); 33 | else cout << res.substr(i) << endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Acwing/3offer/README.md: -------------------------------------------------------------------------------- 1 | # 剑指offer专题 2 | 3 | ## [Week1](week1/) 4 | 5 | 6 | 7 | ## [Week2](week2/) 8 | 9 | 10 | 11 | ## [Week3](week3/) 12 | 13 | 14 | 15 | ## [Week4](week4/) 16 | 17 | 18 | 19 | ## [Week5](week5/) 20 | 21 | 22 | 23 | ## [Week6](week6/) 24 | 25 | 26 | 27 | ## [Week7](week7) 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | /* 1. 找出数组中重复的数字 */ 10 | 11 | const int N = 100010; 12 | int a[N]; 13 | int n; 14 | 15 | int main(void) 16 | { 17 | cin >> n; 18 | for(int i = 0;i < n; i ++) cin >> a[i]; 19 | 20 | //=== 21 | for(auto e : a) 22 | if(e>=n || e<0) 23 | return -1; 24 | 25 | 26 | for(int i = 0; i < n; i ++) { 27 | while(i != a[i] && a[a[i]] != a[i]) swap(a[i], a[a[i]]); 28 | if(i != a[i] && a[a[i]] == a[i]) return a[i]; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 2. 不修改数组找出重复的数字*/ 8 | 9 | const int N = 100010; 10 | int a[N], n; 11 | 12 | 13 | int main(void) 14 | { 15 | 16 | cin >> n; 17 | for(int i =0 ; i < n; i++)cin >> a[i]; 18 | 19 | 20 | int l = 1, r = n- 1; 21 | while(l < r) { 22 | int mid = (l + r) >> 1; //[l, mid] [mid + 1, r] 23 | int s = 0; 24 | for(auto x : a) s += x>=l && x<=mid; 25 | if(s > mid - l + 1) r = mid; 26 | else l = mid + 1; 27 | } 28 | cout << r << endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 3. 二维数组中的查找*/ 8 | const int N = 1010; 9 | int a[N][N]; 10 | int n, target; 11 | 12 | int main(void) 13 | { 14 | cin >> n >> target; 15 | for(int i = 0; i < n; i ++) 16 | for(int j = 0; j < n; j ++) 17 | cin >> a[i][j]; 18 | 19 | int i = 0, j = n - 1; 20 | while(i > n && j <= 0) { 21 | if(a[i][j] == target) { 22 | cout << "YES" << endl; 23 | return 0; 24 | } 25 | 26 | if(a[i][j] > target) j --; 27 | else i ++; 28 | } 29 | cout << "FALSE" << endl; 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 4. 替换空格*/ 8 | 9 | 10 | int main(void) 11 | { 12 | string res, ans; 13 | cin >> res; 14 | for(auto x : res) 15 | if(x == ' ') 16 | ans += "%20"; 17 | else 18 | ans += x; 19 | 20 | cout << ans << endl; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | /* 5. 从尾到头打印链表*/ 9 | const int N = 100010; 10 | int e[N], ne[N]; 11 | int head = -1, idx = 0; 12 | 13 | void insert_head(int x) { 14 | e[idx] = x; 15 | ne[idx] = head; 16 | head = idx++; 17 | } 18 | 19 | void insert(int k, int x) { 20 | e[idx] = x; 21 | ne[idx] = ne[k]; 22 | ne[k] = idx; 23 | idx ++; 24 | } 25 | 26 | void remove(int k) { 27 | ne[k] = ne[ne[k]]; 28 | } 29 | 30 | 31 | void print(){ 32 | for(int i = head; i != -1; i = ne[i]) cout << e[i] << ' '; 33 | puts(""); 34 | } 35 | 36 | void reversePrint() { 37 | std::vector res; 38 | for(int i = head; i != -1; i = ne[i]) res.push_back(i); 39 | reverse(res.begin(), res.end()); 40 | for(auto e : res) cout << e << ' '; 41 | puts(""); 42 | } 43 | 44 | int main(void) 45 | { 46 | insert_head(0); 47 | for(int i = 0; i <= 9; i++) 48 | insert(i, i+1); 49 | 50 | print(); 51 | reversePrint(); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /Acwing/3offer/week1/6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | /* 6. 重建二叉树*/ 9 | 10 | 11 | 12 | int main(void) 13 | { 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Acwing/4index/README.md: -------------------------------------------------------------------------------- 1 | # recursively citing algorithm learned. 2 | > 为复习基础算法设立,每复习结束一种新的算法,需要删掉之前的所有算法,重新再写一遍 3 | -------------------------------------------------------------------------------- /Acwing/4index/ds.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | using namespace std; 6 | /* 7 | 8 | * 单链表 9 | * 双链表 10 | * 栈 11 | * 队列 12 | * 单调栈 13 | * 单调队列 14 | * KMP 15 | */ 16 | 17 | //单链表 18 | const int N = 100010; 19 | int e[N], ne[N]; 20 | int head, idx; 21 | void init() { 22 | idx = 0; 23 | head = -1; 24 | } 25 | 26 | void insert_head(int x) { 27 | e[idx] = x; 28 | ne[idx] = head; 29 | head = idx; 30 | idx ++; 31 | } 32 | 33 | void insert(int k, int x) { 34 | e[idx] = x; 35 | ne[idx] = ne[k]; 36 | ne[k] = idx++; 37 | } 38 | 39 | void remove(int k) { 40 | ne[k] = ne[ne[k]]; 41 | } 42 | 43 | void print() { 44 | for(int i = head; i != -1; i = ne[i]) { 45 | cout << e[i] << ' '; 46 | } 47 | 48 | } 49 | 50 | 51 | //双链表 52 | int r[N], l[N]; 53 | void dinit() { 54 | l[1] = 0; 55 | r[0] = 1; 56 | idx = 2; 57 | } 58 | 59 | void dinsert(int k, int x) { 60 | e[idx] = x; 61 | r[idx] = r[k]; 62 | l[idx] = k; 63 | l[r[k]] = idx; 64 | r[k] = idx ++; 65 | } 66 | 67 | 68 | void dremove(int k) { 69 | l[r[k]] = l[k]; 70 | r[l[k]] = r[k]; 71 | } 72 | 73 | /*=================== 74 | * TRIE树 75 | * 并查集 76 | * 堆 77 | * 哈希表 78 | */ 79 | int son[N][N], cnt[N], tidx; 80 | void insert(char str[]) { 81 | int p = 0; 82 | for(int i = 0; str[i] ; i ++) { 83 | int u = str[i] - 'a'; 84 | if(!son[p][u]) son[p][u] = ++idx; 85 | p = son[p][u]; 86 | } 87 | cnt[p]++; 88 | 89 | } 90 | 91 | int query(char str[]) { 92 | int p = 0; 93 | for(int i = 0; str[i]; i ++) { 94 | int u = str[i] - 'a'; 95 | if(!son[p][u]) return 0; 96 | p = son[p][u]; 97 | } 98 | 99 | return cnt[p]; 100 | } 101 | 102 | 103 | /* 104 | 并查集: 105 | 1. 将两个集合合并 106 | 2. 询问两个元素是否在一个集合中 107 | */ 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | /* 116 | 哈希表 117 | 存储结构 118 | 开放寻址法 119 | 拉链法 120 | 字符串哈希方式 121 | */ 122 | int q[N][26]; 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | int main(void) 137 | { 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /Acwing/4index/quickSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | const int N = 100010; 6 | int a[N], n; 7 | 8 | void quickSort(int a[], int l, int r) { 9 | 10 | if(l >= r) return; 11 | 12 | int x = a[l], i = l - 1, j = r + 1; 13 | while(i < j) { 14 | do i ++; while(a[i] < x); 15 | do j --; while(a[j] > x); 16 | if(i < j) swap(a[i] , a[j]); 17 | } 18 | 19 | quickSort(a, l, j); 20 | quickSort(a, j + 1, r); 21 | } 22 | 23 | 24 | int main(void) 25 | { 26 | cin >> n; 27 | for(int i = 0; i < n; i ++) cin >> a[i]; 28 | 29 | quickSort(a, 0, n - 1); 30 | 31 | for(int i = 0; i < n; i ++)cout << a[i] << ' '; 32 | puts(""); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Acwing/README.md: -------------------------------------------------------------------------------- 1 | # [1. 基础算法](0primaryAlgorithm/) 2 | 3 | 4 | 5 | # [2. 高级算法](1advancedAlgorithm/) 6 | 7 | 8 | 9 | # [3. 笔试面试专题](2internship/) 10 | 11 | 12 | 13 | # [4. 剑指offer](3offer/) 14 | 15 | 16 | 17 | # [5. 训练平台](4index/) 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # [1. 改善程序与设计的55个具体做法](docs/EffectiveC++.md) 4 | 5 | 6 | 7 | # [2. 深度探索C++对象模型](docs/InsideTheC++ObjectModel.md) 8 | 9 | 10 | 11 | # [3. 内存管理](docs/MemoryManagement.md) 12 | 13 | 14 | 15 | # [4. 设计模式](docs/DesignPatterns.md) 16 | 17 | 18 | 19 | # [5. 项目构建](docs/MakeBuild.md) 20 | 21 | 22 | 23 | # [6. 单元测试](docs/UnitTest.md) 24 | 25 | 26 | 27 | # [6. 高速缓存技术](docs/Cache.md) 28 | 29 | 30 | 31 | # [7. 并发编程](docs/C++ConCurrency.md) 32 | 33 | 34 | 35 | # [8.Linux系统编程](docs/Pthread.md) 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /C++/docs/C++ConCurrency.md: -------------------------------------------------------------------------------- 1 | # C++并发基础 2 | 3 | ### 参考资料 4 | 5 | * [C++并发编程实战](https://book.douban.com/subject/27036085/) 6 | 7 | * [[video](https://www.bilibili.com/video/BV1Yb411L7ak?from=search&seid=9741727401638476396)] 8 | 9 | ### Index 10 | 11 | * [[thread.cpp](../src/C++ConCurrency/thread.cpp)] 12 | * [[thread_creat.cpp](../src/C++ConCurrency/thread_creat.cpp)] 13 | * [[thread_para.cpp](../src/C++ConCurrency/thread_para.cpp)] 14 | * [[thread_data_sharing.cpp](../src/C++ConCurrency/thread_data_sharing.cpp)] 15 | * [[thread_mutex.cpp](../src/C++ConCurrency/thread_mutex.cpp)] 16 | * [[thread_unique_lock](../src/C++ConCurrency/thread_unique_lock.cpp)] 17 | * [[thread_singleton](../src/C++ConCurrency/thread_singleton.cpp)] 18 | * [[thread_condition1](../src/C++ConCurrency/thread_condition1.cpp)] 19 | * [[thread_condition2](../src/C++ConCurrency/thread_condition2.cpp)] 20 | * [[thread_async](../src/C++ConCurrency/thread_async.cpp)] 21 | * [[thread_future](../src/C++ConCurrency/thread_future.cpp)] 22 | * [[thread_shared_future](../src/C++ConCurrency/thread_shared_future.cpp)] 23 | * [[thread_atomic](../src/C++ConCurrency/thread_atomic.cpp)] 24 | * [[thread_critical_region](../src/C++ConCurrency/thread_critical_region.cpp)] 25 | * [[thread_pool](../src/C++ConCurrency/thread_pool.cpp)] 26 | 27 | 28 | 29 | # 多核并发 30 | 31 | ### 参考资料 32 | 33 | * [多核编程的艺术](https://book.douban.com/subject/3901836/) 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /C++/docs/Cache.md: -------------------------------------------------------------------------------- 1 | # 高速缓存技术 2 | 3 | 4 | 5 | ## 参考资料 6 | 7 | - [现代体系结构上的UNIX系统——内核程序员的**对称多处理**和**缓存技术**](https://book.douban.com/subject/26290762/) 8 | 9 | - Cache漫谈——高速缓存体系结构详解 10 | 11 | 12 | 13 | ## 目标 14 | 15 | > 为了能让操作系统在采用了**高速缓存**和**多处理器**技术的现代计算机系统上运行。 16 | 17 | 18 | 19 | ## Index 20 | 21 | * [[chp1. 回顾UNIX内核原理](../src/Cache/chp1.md)] 22 | * 23 | 24 | -------------------------------------------------------------------------------- /C++/docs/EffectiveC++.md: -------------------------------------------------------------------------------- 1 | # 改善程序与设计的55个具体做法 2 | 3 | ## 参考资料 4 | 5 | - [C++ Reference](http://www.cplusplus.com/reference/) 6 | 7 | - [Effective C++](https://book.douban.com/subject/5387403/) 8 | 9 | - [C++ Primer](https://book.douban.com/subject/25708312/) 10 | 11 | 12 | 13 | ## Index 14 | 15 | - [[Item1](../src/EffectiveC++/Item1.cpp)]: 视`C++`为一个语言联邦 16 | - [[Item2](../src/EffectiveC++/Item2.cpp)]: 尽量以`const`,`enum`, `inline`替换`#define` 17 | - [[Item3](../src/EffectiveC++/Item3.cpp)]: 尽可能使用`const` 18 | - 19 | 20 | 21 | 22 | - [[Item47](../src/EffectiveC++/Item47.cpp)]: 请使用`traits classes`表现类型信息 23 | - 24 | 25 | -------------------------------------------------------------------------------- /C++/docs/InsideTheC++ObjectModel.md: -------------------------------------------------------------------------------- 1 | # C++对象模型 2 | 3 | ## 参考资料 4 | 5 | * [Inside the C++ Object Model](https://book.douban.com/subject/10427315/) 6 | 7 | 8 | 9 | ## Index 10 | 11 | * [[对象模型语意学](../src/InsideTheC++ObjectModel/object.cpp)] 12 | * [[构造函数语意学](../src/InsideTheC++ObjectModel/constructor.cpp)] 13 | * [[Data语意学](../src/InsideTheC++ObjectModel/data.cpp)] 14 | * [[Function语意学](../src/InsideTheC++ObjectModel/function.cpp)] 15 | * [[构造-析构-拷贝语意学](../src/InsideTheC++ObjectModel/ctordtor.cpp)] 16 | 17 | * [[执行期语意学](../src/InsideTheC++ObjectModel/runtime.cpp)] 18 | 19 | -------------------------------------------------------------------------------- /C++/docs/Pthread.md: -------------------------------------------------------------------------------- 1 | # Linux系统编程 2 | 3 | ## 参考资料 4 | 5 | * [UNIX环境高级编程](https://book.douban.com/subject/1788421/) 6 | 7 | ### 8 | 9 | ## Index 10 | 11 | #### Lab1 [链表操作-GDB调试测试](../src/Pthread/lab1/Node.c) 12 | #### Lab2 [实现简单`cp`命令](../src/Pthread/lab2/myCP.c) 13 | #### Lab3 [实现简单`ls -al`命令](../src/Pthread/lab3/myLS.c) 14 | #### Lab4 [父子进程实现复杂`cp`命令](../src/Pthread/lab4/myLS.c) 15 | #### Lab5 [并发线程实现复杂`cp`命令](../src/Pthread/lab5/main.c) 16 | #### Lab6 [信号量实现**哲学家进餐问题**](../src/Pthread/lab6/philosopher.c) 17 | #### Lab7 [信号量/共享内存实现**生产者消费者问题**](../src/Pthread/lab7/producerAndconsumer.c) 18 | 19 | -------------------------------------------------------------------------------- /C++/docs/UnitTest.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/docs/UnitTest.md -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/debug/test.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.test 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/debug/test.dSYM/Contents/Resources/DWARF/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/C++ConCurrency/debug/test.dSYM/Contents/Resources/DWARF/test -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/debug/thread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/C++ConCurrency/debug/thread -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/debug/thread.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.thread 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/debug/thread.dSYM/Contents/Resources/DWARF/thread: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/C++ConCurrency/debug/thread.dSYM/Contents/Resources/DWARF/thread -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/thread.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | 10 | /* 11 | 经典进程同步互斥问题总结 12 | 1. 基本概念 13 | 14 | 临界资源: 某些资源在同一时间只能被一个进程所占用。 15 | 16 | 互斥: 互斥是进程之间的间接制约关系。当一个进程进入临界区使用临界资源的时候,另一个进程必须等待。只有当使用临界资源的进程 17 | 退出后,这个进程才会接触阻塞状态。 18 | 19 | 同步: 同步是进程之间的直接制约关系。为了完成任务的两个或者多个线程,这个线程需要在某些位置上协调他们的工作次序而等待, 20 | 传递信息所产生的制约关系。进程之间的额直接制约关系来自于他们之间的合作。 21 | 22 | 23 | 实现临界区互斥的基本方式: 24 | a. 关中断 25 | b. 利用Test_and_Set指令实现互斥 26 | c. 利用Swap指令实现互斥 27 | 28 | 信号量实现方式: PV操作,通过设置一个资源个数的信号量S, 通过对信号量S的P和V操作来实现进程的互斥。 29 | P和V分别代表占有和释放,属于操作系统原语,意味着具有原子性。 30 | P操作首先减少信号量,表示有一个进程将占用或等待资源,然后检测S是否小于0,如果小于0则阻塞,如果大于0 31 | 则占有资源继续执行。 32 | V操作是和P操作相反的操作,首先增加信号量,表示占用或者等待资源的进程少了1个。然后检测S是否小于0,如果小于0 33 | 则唤醒等待使用S资源的其他进程。 34 | 35 | 36 | 2. 经典问题总结 37 | 38 | 生产者和消费者问题 39 | 40 | */ 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/thread_condition2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | /* 10 | C++条件变量的生产者-消费者模型 11 | 12 | */ 13 | 14 | 15 | class condition{ 16 | 17 | std::deque my_queue; 18 | std::condition_variable my_cond; 19 | std::mutex my_mutex; 20 | 21 | 22 | public: 23 | void producer(){ //生产者 24 | 25 | int cout = 10; 26 | while(cout > 1) { 27 | 28 | std::unique_lock locker(my_mutex); 29 | my_queue.push_front(cout); 30 | locker.unlock(); 31 | my_cond.notify_one(); 32 | std::this_thread::sleep_for(std::chrono::seconds(1)); 33 | cout --; 34 | } 35 | 36 | return; 37 | } 38 | 39 | void consumer() { 40 | int data = 0; 41 | while(data != 1) { 42 | std::unique_lock locker(my_mutex); 43 | while(my_queue.empty()) { 44 | my_cond.wait(locker); 45 | } 46 | data = my_queue.back(); 47 | my_queue.pop_back(); 48 | locker.unlock(); 49 | std::cout << "consumer get a value from producerL " << data << endl; 50 | } 51 | 52 | } 53 | 54 | }; 55 | 56 | 57 | int main(void) 58 | { 59 | 60 | condition cc; 61 | std::thread ft1(&condition::producer, &cc); 62 | std::thread ft2(&condition::consumer, &cc); 63 | 64 | ft1.join(); 65 | ft2.join(); 66 | 67 | 68 | return 0; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/thread_creat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | /* 10 | chp1. 线程启动,结束,创建线程多种方法 11 | 12 | 13 | 1. 演示线程运行的开始和结束 14 | 15 | 创建线程对象(子线程开始执行): thread my(_addr); //其中_addr表示可调用对象(函数,仿函数) 16 | 线程运行(阻塞主线程,等待子线程执行完毕): my.join(); //main进程(主线程--->my线程---->主线程) 17 | 线程运行(主线程和子线程各自运行,子线程相等于被运行时库(CRT)接管,守护进程): my.detach(); 18 | 判断是否可以成功使用join()或者detach(): joinable(); 19 | 20 | 21 | 2. 其他创建线程方式 22 | 23 | 2.1 使用仿函数作为地址创建线程 24 | 调用两次拷贝构造函数 25 | 当使用detach的时候,因为主线程会提前结束,所以不能使用main函数中的地址空间(比如引用) 26 | 但是作为参数的对象本身会被赋值到线程中 27 | 28 | 29 | 2.2 用lamba表达式创建现场那 30 | 31 | */ 32 | 33 | 34 | 35 | void my_print() { 36 | 37 | cout << "my thread Start! " << endl; 38 | 39 | 40 | cout << "my thread End!" << endl; 41 | } 42 | 43 | 44 | class th{ 45 | 46 | public: 47 | th() {} 48 | th(int _mem) : mem(_mem) { 49 | cout << this->mem << "ctor start" << endl; 50 | } 51 | 52 | th(const th& _th) : mem(_th.mem + 1) { 53 | cout << this->mem << "copy ctor start" << endl; 54 | } 55 | 56 | ~th() { 57 | cout << this->mem << "dtor start" << endl; 58 | } 59 | 60 | 61 | void operator()() { 62 | cout << "my thread Start!" << endl; 63 | 64 | 65 | cout << "my thread End!" << endl; 66 | 67 | } 68 | 69 | private: 70 | int mem = 0; 71 | }; 72 | 73 | 74 | int main(void) //主线程从main返回,整个进程执行完毕 75 | { 76 | 77 | //1. 使用函数 78 | /*thread mythread1(my_print);*/ // 被定义之后,线程立即开始执行 79 | // mythread1.join(); //主线程一定最后退出 80 | //mythread1.detach(); //主线程可能先退出 81 | //cout << mythread1.joinable() << endl; 82 | 83 | 84 | 85 | //2. 使用仿函数 86 | th _th(0); 87 | thread mythread2(_th); //这里不能使用临时对象 88 | mythread2.detach(); 89 | //mythread2.join(); 90 | 91 | 92 | 93 | /* //3. 使用lambda表达式*/ 94 | //auto lamthread = [] { 95 | //cout << "my thread Start" << endl; 96 | 97 | 98 | //cout << "my thread End" << endl; 99 | //}; 100 | //thread mythread3(lamthread); 101 | //mythread3.join(); 102 | //mythread3.detach(); 103 | 104 | 105 | 106 | cout << "Main Thread End!" << endl; 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /C++/src/C++ConCurrency/thread_future.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | /* 11 | 12 | chp9. std::future其他成员函数, shared_future,atomic 13 | 14 | 1. std::future其他成员函数 15 | std::future_status的三种状态 16 | ready 17 | timeout 18 | deferred 19 | 20 | 21 | 3. 原子操作std::atmoic 22 | 23 | 3.1 原子操作概念引出范例 24 | 25 | 26 | 3.2 基本的std::atomic用法范例 27 | 28 | */ 29 | 30 | int mythread() { 31 | 32 | cout << "mythread() Start---ID: " << std::this_thread::get_id() << endl; 33 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 34 | cout << "mythread End---ID: " << std::this_thread::get_id() << endl; 35 | return 5; 36 | } 37 | 38 | 39 | 40 | int main(void) 41 | { 42 | 43 | cout << "Main Start---ID: " << std::this_thread::get_id() << endl; 44 | std::future result = std::async(std::launch::deferred, mythread); 45 | //cout << result.get() << endl; 46 | 47 | //枚举类型 48 | //wait_for 等待一定的时间 49 | //std::future_status status = result.wait_for(std::chrono::seconds(1)); 50 | std::future_status status = result.wait_for(std::chrono::seconds(6)); 51 | if(status == std::future_status::timeout) { //表示线程还没有执行完毕 52 | cout << "status: timeout!" << endl; 53 | }else if(status == std::future_status::ready){ //表示成功返回 54 | cout << "status: successfully returned" << endl; 55 | cout << result.get() << endl; 56 | }else if(status == std::future_status::deferred) { //如果async的第一个参数被设置为std::launch::defered,则成立 57 | cout << "status: delayed!" << endl; 58 | //cout << result.get() << endl; //入口函数会在主线程中执行 59 | } 60 | 61 | cout << "Main End!" << endl; 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | 6 | 7 | /* 8 | 动机: 9 | 在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用,但是新环境要求的 10 | 接口是这些现存对象所不满足的。 11 | 12 | 如何应对这种“迁移的变化”?如何既能利用现有对象的良好实现,同时又能满足新的应用环境所要求的接口? 13 | */ 14 | //目标接口(新接口) 15 | class ITarget{ 16 | public: 17 | virtual void process() = 0; 18 | }; 19 | 20 | //遗留接口(老接口) 21 | class IAdaptee{ 22 | public: 23 | virtual void foo(int data) =0; 24 | virtual int bar() = 0; 25 | }; 26 | class OldClass : public IAdaptee{ 27 | public: 28 | virtual void foo(int data) { 29 | } 30 | 31 | virtual int bar() { 32 | 33 | return 0; 34 | } 35 | }; 36 | 37 | //对象适配器 38 | class Adapter_obj : public ITarget{ //继承 39 | protected: 40 | IAdaptee* pAdaptee; //组合 41 | 42 | public: 43 | Adapter_obj(IAdaptee *ia) : pAdaptee(ia){ 44 | } 45 | virtual void process() { 46 | int val = pAdaptee->bar(); 47 | pAdaptee->foo(val); 48 | } 49 | 50 | }; 51 | 52 | 53 | //类适配器(不推荐) 54 | class Adapter_class : public ITarget, protected IAdaptee{ 55 | 56 | 57 | 58 | }; 59 | 60 | int main(void) 61 | { 62 | IAdaptee* pAdaptee = new OldClass(); 63 | 64 | ITarget* pTarget = new Adapter_obj(pAdaptee); 65 | pTarget->process(); 66 | 67 | return 0; 68 | } 69 | /* 70 | 定义: 71 | 将一个类的接口转化成客户希望的另一个接口。Adapter模式使得原本由于接口不兼容而不能一起 72 | 工作的那些类可以一起工作。 73 | */ 74 | 75 | 76 | /* 77 | 总结: 78 | Adapter模式主要应用于”希望复用一些现存的类,但是接口又与复用环境要求不一致的情况。“在遗留代码复用, 79 | 类库迁移等方面非常有用。 80 | 81 | GoF 23 定义了两种Adapter模式的实现结构;对象适配器和类适配器。但类适配器采用”多继承“的实现方式,一般 82 | 不推荐使用。对象适配器采用”对象组合“的方式,更符合松耦合精神。 83 | 84 | Adapter模式可以实现的非常灵活,不必拘泥于GoF23顶你故意的两种结构。例如:完全可以将Adapter模式中的”现存 85 | 对象“作为新的接口方法参数,来达到适配的目的。 86 | */ 87 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Command.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | using namespace std; 9 | 10 | /* 11 | 动机: 12 | 在软件构建过程中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些 13 | 场合——比如需要对行为进行”记录,撤销/重,事务“等处理,这种无法抵御变化的紧耦合是不合适的。 14 | 15 | 在这种情况下,如何将”行为请求者“和“行为实现者”解耦?将一组行为抽象为对象,可以实现 16 | 二者的松耦合。 17 | 18 | 设计模式是为了补充语言设计的不足而实现的 19 | Command:使用接口规范来实现,但是有性能损失 20 | 仿函数: 使用函数签名实现 编译时绑定 21 | */ 22 | class Command{ 23 | public: 24 | virtual void execute() = 0; 25 | }; 26 | 27 | class ConCreteCommand1 : public Command{ 28 | string arg; 29 | public: 30 | ConCreteCommand1(string a) : arg(a) {} 31 | void execute() override{ 32 | cout << "#1 process" << arg << endl; 33 | } 34 | 35 | }; 36 | 37 | class ConCreteCommand2 : public Command{ 38 | string arg; 39 | public: 40 | ConCreteCommand2(string a) : arg(a) {} 41 | void execute() override{ 42 | cout << "#2 process" << arg << endl; 43 | } 44 | }; 45 | 46 | class MmacroCommand: public Command{ 47 | std::vector commands; 48 | public: 49 | void addCommand(Command* c) { 50 | commands.push_back(c); 51 | } 52 | 53 | void execute() { 54 | 55 | for(auto &c : commands) { 56 | c->execute(); 57 | } 58 | } 59 | }; 60 | 61 | 62 | int main(void) 63 | { 64 | /* 65 | 命令和请求作为对象会有很大的松耦合 66 | 67 | */ 68 | ConCreteCommand1 command1(""); 69 | ConCreteCommand2 command2(""); 70 | 71 | MmacroCommand macro; 72 | macro.addCommand(&command1); 73 | macro.addCommand(&command2); 74 | macro.execute(); 75 | 76 | return 0; 77 | } 78 | 79 | /* 80 | 定义: 81 | 讲一个请求(行为)封装成为一个对象,从而使得你可以使用不同的请求对客户进行参数化, 82 | 对请求排队或记录请求日志,以及支持可撤销的操作。 83 | */ 84 | /* 85 | 总结: 86 | 87 | Command模式的根本目的在于将“行为请求者”和“行为实现者”解耦,在面向对象语言中, 88 | 常见的实现手段是“将行为抽象为对象” 89 | 90 | 实现Command接口的具体命令对象ConcreteCommand有时候根本需要可能会保存一些额外的状态信息, 91 | 通过使用Compoiste模式,可以将多个“命令”封装为一个“复古命令”MacroCommand 92 | 93 | Command模式与C++中的函数对象有些类似。但两者定义行为接口的规范有所区别: 94 | Command以面向对象中的“接口-实现”来定义行为接口规范,更严格,但有性能损失。 95 | C++函数对象以函数签名来定义行为接口规范,更灵活,性能更高; 96 | */ 97 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Facade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | /* 7 | 动机: 8 | A方案的问题在于组件的客户和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和各子系统的 9 | 演化,这种过多的耦合面临着很多变化的挑战。 10 | 11 | 如何简化外部客户程序和系统间的交互接口?如何将外部客户程序的演化和内部子系统的变化之间的依赖互相 12 | 解耦。 13 | */ 14 | 15 | 16 | 17 | 18 | 19 | /* 20 | 定义: 21 | 为子系统中的一组接口提供一个一致(稳定)的界面,facade模式定义了一个高层接口,这个接口使得 22 | 这一系统更加容易使用(复用)。 23 | */ 24 | 25 | 26 | 27 | 28 | 29 | /* 30 | 总结: 31 | 架构设计模式 32 | 从客户程序的角度来看,Facade模式简化了整个组件系统的接口,对于组件内部与外部客户来说,达到 33 | 了一种“解耦”的效果——内部子系统的任何变化不会影响到Facade接口的变化。 34 | 35 | Facade设计模式更注重从架构的层次来看整个系统,而不是单个类的层次。 36 | 37 | Facade设计模式并非一个集装箱,可以任意的放进任何多个对象。Facade模式中组件的内部应该是“相互耦合 38 | 关系比较大的一系列组件”,而不是一个简单的功能集合。 39 | */ 40 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Flyweight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | /* 7 | 动机: 8 | 在软件系统中,采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而 9 | 带来很高的运行代价——主要指的是内存需求方面的代价。 10 | 11 | 如何在避免大量细粒度对象问题的同时,让外部客户程序任然能够头命名你的适用面向对象的方式来 12 | 进行操作。 13 | */ 14 | 15 | 16 | 17 | /* 18 | 定义: 19 | 运用共享技术有效的支持大量细粒度的对象。 20 | 比如:字符串相关技术,线程池技术 21 | */ 22 | class Font{ 23 | private: 24 | //unique object key 25 | string key; 26 | 27 | //object state 28 | //.... 29 | 30 | public: 31 | Font(const string &key) { 32 | //... 33 | } 34 | }; 35 | 36 | class FontFactory{ 37 | private: 38 | map fontPool; 39 | 40 | public: 41 | Font* GetFont(const string& key) { 42 | map::iterator item = fontPool.find(key); 43 | if(item != fontPool.end()) { //和单例模式很相像 44 | return fontPool[key]; 45 | } 46 | else { 47 | 48 | Font* font = new Font(key); 49 | fontPool[key] = font; 50 | return font; 51 | } 52 | } 53 | 54 | void clear() { 55 | //.... 56 | } 57 | }; 58 | 59 | /* 60 | 总结: 61 | 面向对象很好的解决了抽象性的问题,但是作为一个运行在机器中的程序实体,我们需要考虑对象的 62 | 代价问题。Flyweight主要解决面向对象的代价问题,一般不触及面向对象的抽象问题。 63 | 64 | Flyweight采用对象共享的做法来降低系统中对象的个数,从而降低细粒度对象给系统带来的内存压力。 65 | 在具体实现上,要注意对象状态的处理。 66 | 67 | 对象的数量太大而导致对象内存开销加大——什么样的数量才算大?这需要自习根据具体应用情况进行评估,而不能凭空臆断。 68 | */ 69 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Iterator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | using namespace std; 8 | 9 | /* 10 | 动机: 11 | 有些过时 12 | 在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构 13 | 的同时,可以让外部客户代码透明的访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种 14 | 集合对象上进行操作”提供了可能。 15 | 16 | 使用面向对象技术将这种遍历机制抽象为“迭代器对象”为“应对变化中的集合对象”提供了一种优雅的方式。 17 | 18 | 模板:编译时多态 19 | 虚函数:运行时多态 20 | 面向对象的迭代器模式已经过时(性能损耗严重) ,使用泛型编程中的迭代器思想 21 | */ 22 | template 23 | class Iterator{ 24 | public: 25 | virtual void first() = 0; 26 | virtual void next() = 0; 27 | virtual bool isDone() = 0; 28 | virtual T& current() = 0; 29 | }; 30 | 31 | template 32 | class MyCollection{ 33 | public: 34 | Iterator GetIterator() { 35 | //... 36 | } 37 | 38 | }; 39 | 40 | template 41 | class CollectionIterator : public Iterator { 42 | MyCollection mc; 43 | public: 44 | CollectionIterator(const MyCollection& c) : mc(c) {} 45 | virtual void first() { 46 | } 47 | virtual void next(){ 48 | } 49 | virtual bool isDone() const { 50 | } 51 | virtual T& current() { 52 | } 53 | }; 54 | 55 | 56 | void MyAlgorithm() { 57 | 58 | MyCollection mc; 59 | Iterator iter = mc.GetIterator(); 60 | 61 | for(iter.first(); !iter.isDone(); iter.next()) { 62 | cout << iter.current() << endl; 63 | } 64 | } 65 | /* 66 | 定义: 67 | 提供一种方式顺序访问一个聚合对象中的各个元素,而又不暴露(稳定)该对象的内部表示。 68 | */ 69 | 70 | 71 | 72 | /* 73 | 总结: 74 | 迭代抽象: 访问一个聚合对象的内容而无需暴露它的内部表示 75 | 76 | 迭代多态:为遍历不同的集合结构提供一个统一个接口,从而支持同样的算法在不同的集合结构上进行操作。 77 | 78 | 迭代器的健壮性考虑: 遍历的同时更改迭代器所在的集合结构,会导致问题。 79 | */ 80 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Mediator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | using namespace std; 9 | /* 10 | 动机: 11 | 在软件构建过程中,经常会出现多个对象互相关联的情况,对象之间常常会维持一种复杂的引用关系。 12 | 如果遇到一些需求的更改,这种直接的引用关系将面临不断的变化。 13 | 14 | 在这种情况下,我们可使用一个”中介对象”来管理对象间的关联关系,避免互相交互的对象之间的紧耦合引用关系, 15 | 从而更好地抵御变化。 16 | */ 17 | 18 | 19 | 20 | 21 | 22 | 23 | /* 24 | 定义: 25 | 用一个中介对象来封装(封装变化)一系列的对象交互。中介者使各对象不需要显式的相互引用(编译时依赖——》 26 | 运行时依赖)。从而使其耦合松散(管理变化),而且可以独立的改变它们之间的交互。 27 | 28 | */ 29 | 30 | 31 | 32 | 33 | 34 | 35 | /* 36 | 总结: 37 | 将多个对象间复杂的关联关系解耦,Mediator模式将多个对象的控制逻辑进行几种呢管理, 38 | 变为“多个对象互相关联”为多个对象和一个中介者关联呢。简化了系统的维护,抵御了可能的变化。 39 | 40 | 随着控制逻辑的复杂化,Mediator具体对象的实现可能相当复杂。这时候可以对Mediator对象进行分解处理。 41 | 42 | Facade模式是解耦系统间(单向)的对象关联关系;Mediator模式是解耦系统内各个对象之间(双向)的关联关系。 43 | */ 44 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Memento.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | /* 8 | 动机: 9 | 10 | 在软件构建过程中,某些对象的状态在转换过程中,可能由于某种需要,要求程序能够回溯到对象之前处于 11 | 某个点时的状态。如果使用一些共有接口来让其他对象的状态,便会暴露对象的细节实现。 12 | 13 | 如何实现对象状态的良好保存和回复?但同时又不会因此而破坏对象本身的封装性。 14 | */ 15 | class Memento { 16 | string state; 17 | public: 18 | Memento(const string &s) : state(s) {} 19 | string getState() const { return state;} 20 | void setState(const string &s) {state = s;} 21 | }; 22 | 23 | class Originator { 24 | string state; 25 | 26 | public: 27 | Originator() {} 28 | Memento creatMomento() { 29 | Memento m(state); 30 | return m; 31 | } 32 | 33 | void setMomento(const Memento& m) { 34 | state = m.getState(); 35 | } 36 | }; 37 | /* 38 | 定义: 39 | 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以 40 | 将该对象恢复到原先保存的状态。 41 | */ 42 | int main(void) 43 | { 44 | Originator ori; 45 | 46 | //捕获对象状态——存储到备忘录 47 | Memento mem = ori.creatMomento(); 48 | 49 | //改变originator的状态 50 | 51 | //从备忘录中恢复 52 | ori.setMomento(mem); 53 | return 0; 54 | } 55 | /* 56 | 总结: 57 | 备忘录存储原发器(Originator)对象的内部状态,在需要时恢复原发器的状态。 58 | Mementor模式的核心是信息隐藏,即Originator需要向外界隐藏信息,保持其封装性。但同时 59 | 有需要将状态保持到外界。 60 | 61 | 由于现代语言运行时(如C#, Java等)具有相当的对象序列化支持,因此往往采用效率较高,又较容易实现的序列化 62 | 方案来实现Memento模式。 63 | */ 64 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Prototype.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | 动机: 5 | 在软件系统中,经常面临着“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈 6 | 的变化,但是它们却拥有比较稳定一致的接口。 7 | 8 | 如何应对这些变化?如何向“客户程序(使用这些对象的程序)”隔离出“这些易变对象”,从而使用“依赖这些易变对象” 9 | 的客户程序不随着需求改变而改变? 10 | 11 | */ 12 | 13 | /***Same Code as FactoryMethod**/ 14 | 15 | /* 16 | 定义: 17 | 使用原型实例指定创建对象的种类,然后通过拷贝(深拷贝)这些原型来创建新的对象。 18 | */ 19 | class ISplitter{ 20 | public: 21 | virtual void split() = 0; 22 | virtual ISplitter* clone() = 0; 23 | virtual ~ISplitter(){} 24 | }; 25 | class BinarySplitter : public ISplitter{ 26 | public: 27 | ~BinarySplitter() { 28 | } 29 | BinarySplitter(const BinarySplitter& bs) { 30 | } 31 | virtual ISplitter* clone() { 32 | return new BinarySplitter(*this); 33 | } 34 | }; 35 | class TxtSplitter : public ISplitter{ 36 | public: 37 | TxtSplitter(const TxtSplitter& ts) { 38 | } 39 | virtual ISplitter* clone() { 40 | return new TxtSplitter(*this); 41 | } 42 | }; 43 | class PictureSplitter : public ISplitter{ 44 | public: 45 | PictureSplitter(const PictureSplitter& ps) { 46 | 47 | } 48 | virtual ISplitter * clone() override{ 49 | return new PictureSplitter(*this); 50 | } 51 | }; 52 | class MainForm{ 53 | ISplitter* prototype; //原型对象 54 | 55 | MainForm(ISplitter* prototype) { 56 | this->prototype = prototype; 57 | } 58 | 59 | void Button(){ 60 | ISplitter* splitter = prototype->clone(); //克隆原型 61 | splitter->split(); 62 | } 63 | 64 | }; 65 | 66 | /* 67 | 总结: 68 | Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些“易变类” 69 | 拥有“稳定的接口”。 70 | 71 | Prototype模式对于“如何创建易变的实体对象”采用“原型克隆”的方法来做,它使得我们可以非常灵活地动态创建 72 | “某些稳定接口”的新对象——所需工作紧紧是注册一个新类的对象(即原型),然后在任何需要的地方Clone。 73 | 74 | Clone方法可以利用某些框架中的序列化来实现浅拷贝. 75 | */ 76 | -------------------------------------------------------------------------------- /C++/src/DesignPattern/Proxy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /* 6 | 动机: 7 | 面向对象系统中,有些对象由于某种原因(比如对象创建的开销很大,或者某些操作需要安全控制,或者 8 | 需要进程外的访问等),直接访问会给使用者,或者系统结构带来很多麻烦。 9 | 10 | 如何在不失去透明操作对象的同时来管理/控制这些对象特有的复杂性?增加一层间接层是软件开发中常见的解决办法。 11 | */ 12 | class ISubject{ 13 | public: 14 | virtual void process(); 15 | 16 | }; 17 | 18 | 19 | class RealSubject : public ISubject{ 20 | public: 21 | virtual void process() { 22 | //.... 23 | } 24 | }; 25 | 26 | class Client{ 27 | ISubject* subject; 28 | public: 29 | Client() { 30 | subject = new RealSubject; //由于安全,分布式等其他原因得不到new的返回值 31 | } 32 | void DoTask() { 33 | //... 34 | subject->process(); 35 | //... 36 | } 37 | }; 38 | 39 | /* 40 | 定义: 41 | 为其他对象提供一种代理以控制(隔离,使用接口)对这个对象的访问。 42 | */ 43 | //Proxy的设计 44 | class SubjuctProxy : public ISubject{ 45 | public: 46 | virtual void process() { 47 | //对RealSubject的一种间接访问 48 | } 49 | 50 | }; 51 | 52 | /* 53 | 总结: 54 | “增加一层间接层”是软甲系统中对许多复杂问题的一种常见解决方法。在面向对象系统中,直接使用某些对象会带来很多 55 | 问题,作为间接层的Proxy对象便是解决这一问题的常用手段。 56 | 57 | 具体Proxy设计模式的实现方法,实现粒度都相差很大,有些可能对单个对象做细粒度的控制,入copy-on-write技术, 58 | 有些可能对组件模块提供抽象代理层,在架构层次对对象做Proxy。 59 | 60 | Proxy并不一定要求保持接口完整的一致性,只要能够实现间接控制,有时候损及一些透明性是可以接受的。 61 | 分布式系统中大量使用Proxy设计模式 62 | */ 63 | -------------------------------------------------------------------------------- /C++/src/EffectiveC++/Item1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | 5 | Item1: 视C++为一个语言联邦 6 | 7 | * C 8 | * Object-Oriented C++ 9 | * Template C++ 10 | * STL 11 | 12 | 术语: 13 | 声明式 && 定义式 14 | 初始化 15 | default构造函数 16 | copy构造函数 && copy assignment操作符 17 | 18 | */ 19 | 20 | using namespace std; 21 | 22 | /*声明式(declaration)是告诉编译器某个东西的名称和类型(type), 但略去细节*/ 23 | 24 | extern int x; //对象声明式 这里的对象: 内置类型 && 用户自定义类型 25 | 26 | std::size_t numDigits(int number); //函数声明式子 27 | 28 | class Widge; //类声明式 29 | 30 | template //模板声明式 31 | class GraphNode; 32 | 33 | 34 | 35 | /* 36 | 定义式(Definition)的任务是提供编译器一些声明式所遗漏的细节。 37 | * 对对象而言,定义式是编译器为此对象拨发内存的地点; 38 | * 对function或function template而言, 定义式提供了代码本体; 39 | * 对class或者template而言,定义式列出它们的成员 40 | */ 41 | 42 | int x; //x的定义式 分配内存 43 | 44 | std::size_t numDigits(int number) { // 函数定义式 45 | 46 | std::size_t digitsSoFar = 1; 47 | 48 | while(number /= 10) { 49 | ++digitsSoFar; 50 | } 51 | 52 | return digitsSoFar; 53 | } 54 | 55 | class Widge{ //class的定义式 56 | public: 57 | Widge(); 58 | ~Widge(); 59 | }; 60 | 61 | template //template的定义式 62 | class GraphNode{ 63 | public: 64 | GraphNode(); 65 | ~GraphNode(); 66 | 67 | }; 68 | 69 | 70 | /* 71 | copy构造函数用来“以同型对象初始化自我对象” , 定义一个对象如何passed by value 72 | copy assignmeent 操作符用来“从另一个同型对象中拷贝其值到自我对象” 73 | 如果一个新对象被[定义],则一定会有一个构造函数被调用,否则是赋值操作运算符 74 | */ 75 | class Widget 76 | { 77 | public: 78 | Widget (); 79 | Widget(const Widget& rhs); 80 | Widget& operator=(const Widget& rhs); 81 | private: 82 | 83 | }; 84 | 85 | int main(void) 86 | { 87 | Widget w1; 88 | Widget w2(w1); //copy构造函数 89 | w1 = w2; //copy assignment操作符 90 | 91 | 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /C++/src/EffectiveC++/Item2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | /* 6 | 7 | Item2: 尽量以const, enum, inline替换#define 8 | 9 | 10 | */ 11 | using namespace std; 12 | -------------------------------------------------------------------------------- /C++/src/EffectiveC++/Item47.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | /* 3 | 4 | Item47: Traits 机制 5 | 6 | 通过实现advance函数: 将相应的迭代器(这里Traits必须保证内置类型入指针和迭代器都有效)移动某个给定的距离 7 | 8 | 在编译期利用overloading 技术 确定迭代器的类型 9 | 10 | 这里的迭代器和iterator_traits都是用户自定义 正式版本STL使用std:: 11 | */ 12 | 13 | 14 | 15 | //定义用户 五种迭代器 16 | struct input_iterator_tag {}; //输入迭代器 17 | struct output_iterator_tag {}; // 输出迭代器 18 | struct forward_iterator_tag: public input_iterator_tag { }; //前向迭代器 19 | struct bidirectional_iterator_tag : public forward_iterator_tag {}; //双向迭代器 20 | struct random_access_iterator_tag : public bidirectional_iterator_tag {}; // 随机迭代器 21 | 22 | 23 | 24 | //定义用户iterator_traits 25 | template 26 | struct iterator_traits{ 27 | typedef typename IterT::iterator_category iterator_category; 28 | }; 29 | //特化 指针类型 30 | template 31 | struct iterator_traits { 32 | 33 | typedef random_access_iterator_tag iterator_category; 34 | }; 35 | 36 | 37 | 38 | //overloading 函数 39 | 40 | template //用于重载random_access_iterator_tag 41 | void _advance(IterT& iter, DistT d, 42 | random_access_iterator_tag) { 43 | iter += d; 44 | } 45 | 46 | template //用于重载bidirectional_iterator_tag 47 | void _advance(IterT& iter, DistT d, 48 | bidirectional_iterator_tag) { 49 | if(d >= 0) { while(d--) ++iter;} 50 | else { while(d++) --iter;} 51 | } 52 | 53 | template //用于重载input_iterator_tag 54 | void _advance(IterT& iter, DistT d, 55 | input_iterator_tag) { 56 | if(d < 0){ 57 | throw std::out_of_range("Negative distance"); 58 | } 59 | while(d--) ++iter; 60 | } 61 | 62 | 63 | 64 | //最终实现的advance 65 | template 66 | void advance(IterT& iter, DistT d) { 67 | 68 | _advance(iter, d, 69 | typename iterator_traits::iterator_category()); 70 | 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /C++/src/InsideTheC++ObjectModel/constructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | /* 8 | chp2. 构造函数语意学 9 | 10 | Plus: 11 | 1. dynamic_cast 12 | 会进行运行时刻安全检查,一般引用类型转换失败报错 std::bad_cast 13 | 2. 智能指针 14 | shared_ptr/unique_ptr 15 | 初始化方式 16 | 拷贝/赋值方式 17 | 18 | */ 19 | class A{ 20 | public: 21 | A(std::string s): str(s){ 22 | } 23 | 24 | std::string str; 25 | }; 26 | class B : public A{ 27 | public: 28 | B(std::string s):A(s){ 29 | } 30 | }; 31 | class C : public B{}; 32 | class D : public B{}; 33 | class S { 34 | virtual ~S(){} 35 | }; 36 | 37 | 38 | int main(void) 39 | { 40 | /* A* pa = new C;*/ 41 | //B *pb = dynamic_cast(pa); 42 | 43 | //B* pb = new B; 44 | //S* ps = new S; 45 | //C *pc = dynamic_cast(ps); 46 | 47 | /* A *pa = new D;*/ 48 | //A *pb = dynamic_cast(pa); 49 | 50 | /* 智能指针初始化方式 */ 51 | //std::string *str1 = new string("foo"); 52 | //std::string *str2 = new string("idiot"); 53 | //std::unique_ptr uS1(str1); 54 | //std::unique_ptr uS2(str2); 55 | 56 | //std::cout << *uS1 << std::endl; 57 | //std::cout << *uS2 << std::endl; 58 | 59 | //uS1.reset(uS2.release()); 60 | //std::cout << *uS1 << std::endl; 61 | 62 | 63 | 64 | /*智能指针拷贝/赋值方式*/ 65 | A *a = new A("foo"); 66 | B *b = new B("idiot"); 67 | std::unique_ptr ss1(a); 68 | std::unique_ptr ss2(b); 69 | 70 | std::cout << ss1->str << std::endl; 71 | std::cout << ss2->str << std::endl; 72 | 73 | ss1.reset(ss2.release()); // 需要拷贝的双方一定满足传统的拷贝/赋值关系 74 | //ss2.reset(ss1.release()); // 操作失败 75 | std::cout << ss1->str << std::endl; 76 | 77 | return 0; 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /C++/src/InsideTheC++ObjectModel/ctordtor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | typedef struct { 9 | 10 | float x, y, z; 11 | 12 | }Point; 13 | 14 | Point global; 15 | Point foobar() { 16 | 17 | Point local; 18 | Point *heap = new Point; 19 | *heap = local; 20 | 21 | delete heap; 22 | return local; 23 | } 24 | -------------------------------------------------------------------------------- /C++/src/InsideTheC++ObjectModel/function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | 7 | /* 8 | chp4. Function语意学 9 | member function分为non-static static virtual三种函数 10 | Member各种调用方式 11 | Virtual Member Function 12 | 函数效能 13 | 指向Member Function的指针 14 | Inline Function 15 | 16 | */ 17 | 18 | /* 19 | 1. non-static 被内化成为non-member的形式 20 | Point3d Point3d::magnitude()----------->Point3d Point3d::magnitude(Point3d *const this); 21 | */ 22 | 23 | 24 | /* 25 | 2. static member function 26 | 其特性就是取消this指针,因此有以下特性: 27 | 不能够直接存取其class中的nonstatic class 28 | 不能够被声明为const, volatile或virtual 29 | 不需要经过class object调用 30 | */ 31 | 32 | 33 | /* 34 | 3. virtual-member-function 35 | 36 | */ 37 | 38 | class Point3d{ 39 | 40 | 41 | public: 42 | float magnitude() const { 43 | return res; 44 | } 45 | 46 | 47 | Point3d normalize()const { 48 | float mag = magnitude(); 49 | Point3d normal; 50 | 51 | 52 | normal._x = _x/mag; 53 | normal._y = _y/mag; 54 | normal._z = _z/mag; 55 | 56 | return normal; 57 | 58 | } 59 | 60 | protected: 61 | float res; 62 | float _x; 63 | float _y; 64 | float _z; 65 | }; 66 | 67 | 68 | 69 | 70 | static void Test_Point3d() { 71 | Point3d obj; 72 | Point3d *ptr = &obj; 73 | 74 | ptr->normalize(); 75 | 76 | 77 | 78 | } 79 | 80 | 81 | -------------------------------------------------------------------------------- /C++/src/InsideTheC++ObjectModel/object.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | /* chp1. 对象模型初探*/ 7 | /* 8 | 1. 对象模型的内存大小 9 | a. 其nonstatic data member的总和大小 10 | b. 任何由于alignmeent的需求而填补的空间 11 | c. 为了支持virtual机制而由内部产生的任何额外负担(overhead) 12 | 13 | */ 14 | 15 | 16 | 17 | /* 18 | 19 | 2. 面向对象模型初探(继承,多态) 20 | a. 多态的必要条件是指针和引用 21 | b. 多态经过一个共同的接口影响类型的封装,该接口就是virtual函数机制 其本质就是根据运行期object的真正类型来解析哪一个函数实例被调用 22 | c. 指针的类型解释了其所指对象的大小及内容 23 | d. 指针的类型只能够决定其本身调用的接口和access level, 而对象多态化的实例则取决于对象本身所存取的vptr中 24 | e. 继承对象之间的直接赋值只会造成slided 25 | */ 26 | 27 | class Library_mateials 28 | { 29 | public: 30 | virtual void check() { 31 | cout << "Library_mateials" << endl; 32 | } 33 | 34 | }; 35 | 36 | class Book : public Library_mateials 37 | { 38 | public: 39 | virtual void check() { 40 | cout << "Book" << endl; 41 | } 42 | 43 | }; 44 | static void Test_Polymorphism() 45 | { 46 | Book book; 47 | Library_mateials thing1 = book; 48 | thing1.check(); //输出Library_mateial 49 | 50 | Library_mateials &thing2 = book; 51 | thing2.check(); //输出Book 52 | 53 | return; 54 | } 55 | 56 | 57 | 58 | int main(void) 59 | { 60 | 61 | Test_Polymorphism(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /C++/src/InsideTheC++ObjectModel/runtime.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/InsideTheC++ObjectModel/runtime.cpp -------------------------------------------------------------------------------- /C++/src/MemoryManagement/0primitives_index.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | 7 | 8 | using namespace std; 9 | 10 | 11 | /* 12 | 13 | 1. Primitives 14 | 不同的OS和编译器有不同的实现 15 | malloc/free 16 | new/delete 17 | operator new/operator delete 18 | std::allocator/std::deallocate 19 | */ 20 | 21 | 22 | void primitives_sample() { 23 | 24 | 25 | 26 | //CRT运行时函数 27 | void *p1 = malloc(512); 28 | free(p1); 29 | 30 | 31 | //new 32 | complex* p2 = new complex; 33 | delete p2; 34 | 35 | //::operator new(全局函数,可被重载) 36 | void* p3 = ::operator new(sizeof(int)); //内部调用malloc 37 | ::operator delete(p3); //内部调用delete 38 | 39 | 40 | // allocator分配器(STL容器分配内存的方式) 41 | void* p4 = allocator().allocate(7); 42 | allocator().deallocate((int*)p4, 7); 43 | 44 | return; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | int main(void) 52 | { 53 | primitives_sample(); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/10pool_alloc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#include 4 | 5 | 6 | using namespace std; 7 | 8 | /* 9 | 测试对比GNU2.9和GNU4.9平台对cookie的优化情况: 10 | GNU2.9: 未优化 11 | GNU4.9: 优化 12 | */ 13 | template 14 | void cookie_test(Alloc alloc, size_t size) { 15 | typename Alloc::value_type* p1, *p2, *p3; 16 | p1 = alloc.allocate(size); 17 | p2 = alloc.allocate(size); 18 | p3 = alloc.allocate(size); 19 | 20 | cout << "p1 = " << p1 << '\t'; 21 | cout << "p2 = " << p2 << '\t'; 22 | cout << "p3 = " << p3 << '\t'; 23 | 24 | alloc.deallocate(p1, sizeof(typename Alloc::value_type)); 25 | alloc.deallocate(p2, sizeof(typename Alloc::value_type)); 26 | alloc.deallocate(p3, sizeof(typename Alloc::value_type)); 27 | } 28 | 29 | int main(void) 30 | { 31 | //GNU 4.9 32 | //cout << sizeof(__gnu_cxx::__pool_alloc(int)) << endl; 33 | //cookie_test(__gnu_cxx::__pool_alloc(), 1); 34 | 35 | 36 | //GNU2.9 37 | cout << sizeof(std::allocator) << endl; 38 | cookie_test(std::allocator(), 1); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/12alloc_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | //#include 6 | 7 | 8 | 9 | /* 10 | GNU4.9下 __pool_alloc测试 11 | */ 12 | static long long coutNew = 0; 13 | static long long timesNew = 0; 14 | 15 | void *myAlloc(size_t size) { 16 | return malloc(size); 17 | } 18 | 19 | void myFree(void* ptr) { 20 | return free(ptr); 21 | } 22 | 23 | void* operator new(size_t size) { 24 | coutNew += size; 25 | ++timesNew; 26 | return myAlloc(size); 27 | } 28 | 29 | void* operator new[](size_t size){ 30 | return myAlloc(size); 31 | } 32 | 33 | void operator delete(void* ptr, size_t) { 34 | myFree(ptr); 35 | } 36 | 37 | void operator delete[](void* ptr, size_t) { 38 | myFree(ptr); 39 | } 40 | 41 | 42 | 43 | /* 44 | 使用标准的分配器——使用::operator new/delete (malloc/free) 45 | */ 46 | void TEST_OPERATOR_NEW() { 47 | coutNew = 0; 48 | timesNew = 0; 49 | std::list lst; 50 | 51 | for(int i =0 ; i < 1000; ++i){ 52 | lst.push_back(i); 53 | } 54 | 55 | std::cout << "::contNew = " << ::coutNew << std::endl; 56 | std::cout << "::timesNew = " << ::timesNew << std::endl; 57 | 58 | return; 59 | } 60 | 61 | /* 62 | 使用新型分配器__gnun_cxx::__pool_alloc 63 | */ 64 | template 65 | using listPool = std::list>; 66 | void TEST_ALLOC() { 67 | 68 | coutNew = 0; 69 | timesNew = 0; 70 | listPool lst; 71 | for(int i = 0; i < 1000; i ++) { 72 | lst.push_back(i); 73 | } 74 | 75 | std::cout << "::countNew = " << ::coutNew << std::endl; 76 | std::cout << "::timeNew = " << ::timesNew << std::endl; 77 | return; 78 | } 79 | 80 | int main(void) 81 | { 82 | 83 | //TEST_OPERATOR_NEW(); 84 | TEST_ALLOC(); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13heap_alloc_base.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | //1016 + cookie(8Bytes) = 1024 如果是小区块的话则调用 3 | if(size <= _sbh_threshold){ 4 | pvReturn = __sbh_alloc_block(size); 5 | if(pvReturn) return pvReturn; 6 | } 7 | 8 | if(size == 0) size = 1; 9 | size = (size + ...)& ~(...) 10 | return HeapAlloc(_crtheap, 0, size); 11 | */ 12 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13heap_alloc_dbg.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | //nSize = 256Bytes nNoMansLandSize = 4Bytes 5 | 6 | #define nNoMansLandSize 4 7 | typedef struct _CrtMemBlockHeader{ //在debug模式下准备附加到所申请的内存之上的 所以在debug模式下分配的内存会比申请的内存大一些 8 | struct _CrtMemBlockHeader* pBlockHeaderNext; 9 | struct _CrtMemBlockHeader* pBlockHeaderPrev; 10 | char* szFileName; //指向被调试文件的文件名 11 | int nLine; //文件行 12 | size_t nDataSize; //记录真正分配的大小 13 | int nBlockUse; 14 | long IRequest; 15 | unsigned char gap[nNoMansLandSize]; //4个字符指针 16 | }_CrtMemBlockHeader; 17 | 18 | 19 | blockSize = sizeof(_CrtMemBlockHeader) + nSize + nNoMansLandSize; 20 | pHead = (_CrtMemBlockHeader*)_heap_alloc_base(blockSize); 21 | if(_pFirstBlock) //这两个指针将内存连接起来了 22 | _pFirstBlock->pBlockHeaderPrev = pHead; 23 | else 24 | pLastBlock = pHead; 25 | 26 | //初始化_CrtMmemmBlockHeader结构体 27 | 28 | //link blocks together 29 | _pFirstBlock = pHead; 30 | static unsigned char _bNoMansLandFill = 0xf0; 31 | //fill in gap before and after real block 32 | memset((void*)pHead->gap, _bNoMansLandFill, nNoMansLandSize); 33 | memset((void*)(pbData(pHead) + nSize), _bNoMansLandFill, nNoMansLandSize); 34 | 35 | //fill data with silly value(but no-zero) 36 | memset((void*)pbData(pHead), _bCleanLandFill, nSize); 37 | return (void*)pbData(pHead); 38 | 39 | //调试模式下,所有经过Malloc分配的内存块都会用指针登记起来 40 | //利用CRT提供的指针,可以模拟调试器的功能} 41 | */ 42 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13heap_init.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /*Windows API 的部分代码*/ 4 | /* 5 | int __cdecl _heap_init() { 6 | 7 | //initialize the "big-block" heap first 8 | if((_crtheap = HeapCreat(mtflag ? 0 : HEAP_NO_SERIALIZE, BYTES_PER_PAGE, 0)) == NULL) 9 | return 0; 10 | 11 | if(_sbh_heap_init() == 0) { 12 | HeapDestory(_crtheap); 13 | return 0; 14 | } 15 | return 1; 16 | } 17 | */ 18 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13ioinit.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | #ifndef _DEBUG //调试模式下 5 | #define _malloc_crt malloc 6 | #else 7 | #define _THISFILE _FILE 8 | #define _malloc_crt(s) _malloc_dbg \ 9 | (s, _CRT_BLOCK, _THISFILE, _LINE_) 10 | 11 | 12 | typedef struct{ 13 | long osfhnd; 14 | char osfile; 15 | char pipech; 16 | }ioinfo; 17 | 18 | void __cdecl ioinit(void) { 19 | 20 | //32*8 = 256Bytes 100h 21 | if((pio = malloc_crt(IOINFO_ARRAY_ELTS*sizeof(ioinfo))) == NULL) 22 | 23 | //... 24 | } 25 | */ 26 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13sbh_alloc_block.cpp: -------------------------------------------------------------------------------- 1 | //16的倍数 总共是130h 2 | //sizeEntry = (intSize + 2 * sizeof(int) + (BYTES_PER_PAGE - 1) & ~(BYTES_PER_PAGE - 1)) //ROUNDUP函数) 3 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/13sbh_heap_init.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | typedef struct tagHeader{ 5 | BITVEC bitEntryHi; 6 | BITVEC bitvEntryLo; 7 | BITVEC bitvCommit; 8 | void* pHeadpData; 9 | struct tagRegion* pRegion; 10 | }HEADER, *PHEADER; 11 | 12 | int __cdecl __sbh_heap_init(void) { 13 | 14 | if(!(__sbh_pHeaderList = HeapAlloc(_crtheap, 0, 16 * sizeof(HEADER)))) 15 | return false; 16 | 17 | __sbh_pHeaderScan = __sbh_pHeaderList; 18 | __sbh_pHeaderDefer = NULL; 19 | __sbh_cntHeaderList = 0; 20 | __sbh_sizeHeaderList = 16; 21 | 22 | return true; 23 | } 24 | */ 25 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/14array_allocator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | 7 | 该分配器直接使用静态数组进行分配,感觉很像placement new 的手法 8 | 测试程序并没有完成测试,缺少std::tr1库 9 | 10 | */ 11 | 12 | 13 | //数组分配器, 从数组中直接分配 14 | template> //底部是C++数组 静态数组(不需要释放)——所以deallocate()是空的 15 | class array_allocator : public array_allocator_base<_Tp> 16 | { 17 | public: 18 | typedef size_t size_type; 19 | typedef _Tp value_type; 20 | typedef _Array array_type; 21 | 22 | private: 23 | array_type* _M_array; //指向数组的指针 24 | size_type _M_used; 25 | 26 | public: 27 | array_allocator(array_type* __array = NULL)throw() : _M_array(_array), _M_used(size_type()){} 28 | } 29 | 30 | 31 | using namespace std; 32 | using namespace std::tr1; 33 | using namespace __gnu::cxx; 34 | 35 | int my[65535]; 36 | array_allocator> 37 | myalloc(&my); //初始化 38 | 39 | //分配空间 但是不会回收释放过的空间 40 | int* p1 = myalloc::allocate(1); 41 | 42 | int* p2 = myalloc.allocate(3); 43 | 44 | myalloc.deallocate(p1); 45 | myalloc.deallocate(p2); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/14debug_allocator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | /* 11 | debug_allocator 12 | 13 | 14 | */ 15 | //该分配器并不自己分配内存,仅仅是对申请的内存做一些处理(增)然后调用其他的分配进行分配 16 | /* 17 | template 18 | class debug_allocator{ 19 | private: 20 | size_type _M_extra; 21 | _Alloc _M_allocator; //自己包裹起来的分配器 22 | 23 | size_type _S_extra() { //计算extra(bytes)相当于几个元素 24 | const size_t __obj_size = sizeof(value_type); 25 | return(sizeof(size_type) + __obj_size - 1) / obj_size; 26 | } 27 | 28 | public: 29 | debug_allocator(const _Alloc& __a) : _M_allocator(__a), _M_extra(_S_extra){} 30 | 31 | 32 | pointer allocate(size_type __n) { 33 | Pointer _res = _M_allocate(n + _M_extra); 34 | size_type* __ps = reinterpret_cast(__res); 35 | *__ps = __n; 36 | return __res + _M_extra; 37 | } 38 | 39 | void deallocate(pointer __p, size_type _n) { 40 | ... 41 | } 42 | 43 | } 44 | 45 | */ 46 | int main(void) 47 | { 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/2primitives_array_new_delete.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | using namespace std; 7 | 8 | /* 9 | 10 | 1. new object[]/delete[] object(本质调用malloc/delete) 11 | 12 | malloc/free分配细节: 13 | 除了分配给定的内存之外,还会有cookie——用来记录分配数组的长度 14 | 查看cookie中的长度,然后调用适当的析构函数 15 | 16 | 所谓内存泄漏: 17 | 对于一个new object[],需要有相应的delete[] object 18 | 本质上cookie记录了free本身需要释放的内存大小,正的泄露发生在调用的析构函数次数上: 19 | 如果object是带指针的member,则意味着析构函数是nontrival,反之则是trival的 20 | 而析构函数的调用次数,则会影响指针指向的内存部分的泄露 21 | 22 | 23 | */ 24 | class Complex{ 25 | 26 | public: 27 | int id; 28 | Complex(int _id) : id(_id) { 29 | cout << "default ctor is. " << this << " id = " << id << endl; 30 | } 31 | Complex() : id(0) { 32 | } 33 | 34 | ~Complex() { 35 | cout << "dtor is. " << this << " id = " << id << endl; 36 | } 37 | }; 38 | 39 | 40 | static void sample() { 41 | 42 | /* 43 | 唤起3次ctor 44 | 无法藉由参数给予初值 45 | */ 46 | Complex* pca = new Complex[3]; 47 | 48 | //唤起3次dtor 49 | delete []pca; 50 | } 51 | 52 | 53 | static void TEST_ARRAY_NEW_DELETE(int size) { 54 | 55 | Complex* buf = new Complex[size]; //必须有默认构造函数 56 | Complex* tmp = buf; 57 | cout << "buf = " << buf << " tmp = " << tmp << endl; 58 | for(int i = 0 ; i < size; i ++) { 59 | new (tmp++)Complex(i); //replacemennt new 60 | } 61 | cout << "buf = " << buf << "tmp = " << tmp << endl; 62 | delete []buf; //次序逆反 63 | } 64 | 65 | int main(void) 66 | { 67 | TEST_ARRAY_NEW_DELETE(3); 68 | 69 | return 0; 70 | } 71 | 72 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/3primitives_replacement_new.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | 10 | /* 11 | placement new (调用operator new) 12 | 允许我们将对象建构在已经allocated的memory上 13 | 没有所谓的placement delete,因为placement new根本不分配内存! 14 | 不会在分配内存(定点内存) 15 | 16 | 17 | 18 | */ 19 | 20 | class Complex{ 21 | int a, b; 22 | public: 23 | Complex() {} 24 | Complex(int _a, int _b) : a(_a), b(_b){ 25 | } 26 | ~Complex() {} 27 | }; 28 | 29 | //重载operator new(本质上就是placement new) 30 | void* operator new(size_t size, char *buf) { 31 | return buf; 32 | } 33 | 34 | 35 | class primitives_relpacement_new { 36 | Complex* pc; 37 | public: 38 | 39 | //调用placement new 的编译器生成的代码 40 | void new_override(char *buf) { 41 | try { 42 | void *mem = operator new(sizeof(Complex), buf); //重载 43 | pc = static_cast(mem); 44 | //pc->Complex::Complex(1,2); //构造函数 45 | 46 | }catch(std::bad_alloc) { 47 | cout << "Bad Allocation" << endl; 48 | } 49 | } 50 | }; 51 | 52 | //演示 53 | static void sample(){ 54 | 55 | char *buf = new char[sizeof(Complex)*3]; 56 | Complex* pc = new(buf) Complex(1,2); 57 | 58 | delete [] buf; 59 | return; 60 | } 61 | 62 | int main(void) 63 | { 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/9end.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | /* 10 | 1. new_handler 11 | 当operator new没有能力分配出锁申请的memory,会抛出一个std::bad_alloc的exception或者返回0 12 | 13 | 编译器会在抛出异常之前,会不断调用一个可有client指定的handler: 14 | typedef void(*new_handler)(); 15 | new_handler set_new_handler(new_handler p) throw(); //标准库提供的函数原型 16 | 设计良好的new handler只有两个选择: 17 | 让更多内存可用&&调用abort()&&exit() 18 | 19 | 20 | 21 | 2. =default, =delete 22 | delete表示删除函数(不能生成函数实例) 23 | default函数的默认版本(拷贝构造,拷贝赋值,析构函数, operator new/new[], operator delete/delete[]) 24 | 25 | 26 | */ 27 | 28 | void noMoreMemory() { 29 | cout << "out of memory"; 30 | abort(); 31 | } 32 | 33 | /* 34 | 35 | set_new_handler VC6简略实现 36 | new_handler set_new_handler(new_handler new_p) { 37 | 38 | //cannot use stbu to register a new handler 39 | assert(new_p == 0); 40 | //remove current handler 41 | _set_new_handler(0); 42 | return 0; 43 | } 44 | 45 | */ 46 | int main(void) 47 | { 48 | set_new_handler(noMoreMemory); 49 | 50 | //胆小……不敢试:) 51 | /* 52 | int *p = new[10000]; 53 | assert(p); 54 | */ 55 | 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/debug/macro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/MemoryManagement/debug/macro -------------------------------------------------------------------------------- /C++/src/MemoryManagement/debug/macro.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.macro 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/debug/macro.dSYM/Contents/Resources/DWARF/macro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/MemoryManagement/debug/macro.dSYM/Contents/Resources/DWARF/macro -------------------------------------------------------------------------------- /C++/src/MemoryManagement/debug/primitives.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | //敬给,这些年一个人走过的路,吃过的屎,和错过的人 3 | 4 | 5 | 6 | CFBundleDevelopmentRegion 7 | English 8 | CFBundleIdentifier 9 | com.apple.xcode.dsym.primitives 10 | CFBundleInfoDictionaryVersion 11 | 6.0 12 | CFBundlePackageType 13 | dSYM 14 | CFBundleSignature 15 | ???? 16 | CFBundleShortVersionString 17 | 1.0 18 | CFBundleVersion 19 | 1 20 | 21 | 22 | -------------------------------------------------------------------------------- /C++/src/MemoryManagement/debug/primitives.dSYM/Contents/Resources/DWARF/primitives: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/MemoryManagement/debug/primitives.dSYM/Contents/Resources/DWARF/primitives -------------------------------------------------------------------------------- /C++/src/Pthread/lab1/Lab1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab1/Lab1.pdf -------------------------------------------------------------------------------- /C++/src/Pthread/lab1/Node.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | 6 | 7 | typedef struct Node { 8 | int data; 9 | struct Node *next; 10 | }*pNode; 11 | 12 | 13 | pNode creatNode() { 14 | 15 | pNode node = (pNode)malloc(sizeof(struct Node)); 16 | if(node == NULL) { 17 | printf("malloc allocating failed\n"); 18 | return NULL; 19 | } 20 | 21 | node->data = 0; 22 | node->next = NULL; 23 | 24 | printf("allocating successfully\n"); 25 | return node; 26 | } 27 | 28 | int InsertNode(pNode head, int pos, int data) { 29 | if(head == NULL) { 30 | printf("error! linked list not exists\n"); 31 | return -1; 32 | } 33 | pNode pointer = head; 34 | int i = 0; 35 | for( i=0; inext; 41 | } 42 | 43 | pNode ele = malloc(sizeof(struct Node)); 44 | if(ele == NULL) { 45 | printf("ele allicating failed]n"); 46 | return -1; 47 | } 48 | 49 | 50 | ele->data = data; 51 | ele->next = pointer->next; 52 | pointer->next = ele; 53 | printf("insert successfully\n"); 54 | return 1; 55 | 56 | } 57 | 58 | void TraverseNode(pNode head) { 59 | if(head == NULL) { 60 | printf("linked list head not exists\n"); 61 | return; 62 | } 63 | printf("Here comes Travering\n"); 64 | pNode link = head; 65 | int i=0; 66 | while(link->next != NULL) { 67 | printf("this is the %dth element: %d\n", i++, link->data); 68 | link = link->next; 69 | } 70 | return; 71 | } 72 | 73 | 74 | 75 | int main() { 76 | 77 | printf("Please choose the length of linkedlist:\n"); 78 | int length; 79 | pNode head = NULL; 80 | head = creatNode(); 81 | 82 | scanf("%d", &length); 83 | int i=0; 84 | for(; i 2 | #include 3 | #include 4 | #include 5 | #define BUF_SIZE 1024 6 | 7 | extern errorno; 8 | 9 | 10 | int main(int argc, char **argv) { 11 | 12 | char *des; 13 | char *src; 14 | 15 | src = argv[1]; 16 | des = argv[2]; 17 | 18 | /* 19 | Warning!!! 20 | The following code is for lab4 21 | src = argv[0]; 22 | des = arg[1]; 23 | */ 24 | int srcFd; 25 | int desFd; 26 | 27 | //buf 28 | unsigned char buf[BUF_SIZE]; 29 | int realRead; 30 | 31 | // printf("argv[0] is %s\n", src); 32 | // printf("argv[1] is %s\n", des); 33 | 34 | if((srcFd = open(src, O_RDONLY)) == -1) { 35 | printf("fatal! %s not exists\n", src); 36 | return -1; 37 | } 38 | 39 | if((desFd = open(des, O_RDWR | O_CREAT)) == -1) { 40 | printf("fatal! %s can't be opened\n", des); 41 | return -1; 42 | } 43 | 44 | 45 | while((realRead = read(srcFd, buf, BUF_SIZE)) > 0) { 46 | write(desFd, buf, realRead); 47 | } 48 | 49 | 50 | //printf(strerror(errno)); 51 | printf("\n"); 52 | close(srcFd); 53 | close(desFd); 54 | } 55 | -------------------------------------------------------------------------------- /C++/src/Pthread/lab3/Lab3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab3/Lab3.pdf -------------------------------------------------------------------------------- /C++/src/Pthread/lab3/myLS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab3/myLS -------------------------------------------------------------------------------- /C++/src/Pthread/lab3/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | DIR_SIZE myLS.c 8;" d file: 8 | main myLS.c /^int main(int argc, char **argv) {$/;" f 9 | -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/copyfile.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/copyfile.c -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/dir2/Screen Shot 2018-12-20 at 2.48.26 PM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/dir2/Screen Shot 2018-12-20 at 2.48.26 PM.png -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/dir2/Screen Shot 2018-12-20 at 2.48.32 PM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/dir2/Screen Shot 2018-12-20 at 2.48.32 PM.png -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/dir2/dir3/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/dir2/dir3/test -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/dir2/myCP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/dir2/myCP -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/hi6220_spi.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/hi6220_spi.h -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/listdirectory.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/listdirectory.c -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/myLS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/myLS -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/spi_simulate.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/spi_simulate.c -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/dir1/spi_simulate.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/dir1/spi_simulate.h -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/lab4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab4/lab4.pdf -------------------------------------------------------------------------------- /C++/src/Pthread/lab4/myLS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define DIR_SIZE 2014 14 | 15 | int main(int atgv, char **argv) { 16 | 17 | char currentDir[DIR_SIZE]; 18 | char *currentPtr = NULL; 19 | DIR *Dirptr = NULL; 20 | if((currentPtr = getcwd(currentDir, sizeof(currentDir))) == NULL) { 21 | printf("can't find directory\n"); 22 | return -1; 23 | }else if((Dirptr = opendir(currentDir)) == NULL) { 24 | printf("fatal: Can't open directory\n"); 25 | return -1; 26 | } 27 | 28 | struct dirent *dirPtr; 29 | struct stat statPtr; 30 | while((dirPtr = readdir(Dirptr)) != NULL) { 31 | 32 | if(lstat(dirPtr->d_name, &statPtr) == -1) { 33 | printf("fatal: file resovling error\n"); 34 | return -1; 35 | } 36 | //source directory string operation 37 | char src[DIR_SIZE]; 38 | strcpy(src, currentDir); 39 | strcat(src, "/"); 40 | char des[DIR_SIZE] = "/home/haotian/"; 41 | //not a dir... 42 | if(!S_ISDIR(statPtr.st_mode)) { 43 | int pid = fork(); 44 | if(pid > 0) { 45 | waitpid(pid, NULL, WNOHANG); 46 | }else if(pid == 0) { 47 | /* 48 | Warning!!! 49 | using myCP in ./dir1/dir2/myCP 50 | this version of myCP is slightly different from the one of Lab2: 51 | since the mechanism of main's parameter passing didn't match execl's in this lab 52 | main: arg[1] arg[2] 53 | execl arg[0] arg[1] 54 | so you have to recompile code of lab2 55 | */ 56 | if(execl("./dir1/dir2/myCP", strcat(src, dirPtr->d_name), strcat(des, dirPtr->d_name), NULL) == -1) { 57 | printf("exe error!\n"); 58 | return -1; 59 | } 60 | }else if(pid < 0) { 61 | printf("error fork!\n"); 62 | return -1; 63 | } 64 | 65 | 66 | }//if 67 | }//while 68 | }//main 69 | -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/Lab5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/Lab5.pdf -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/dir1/dir3/haohao: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/dir1/dir3/haohao -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/dir1/dir3/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/dir1/dir3/test -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/hi6220_spi.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/hi6220_spi.h -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/listdirectory.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/listdirectory.c -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/spi_simulate.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab5/spi_simulate.h -------------------------------------------------------------------------------- /C++/src/Pthread/lab5/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | BUF_SIZE main.c 12;" d file: 8 | DIR_SIZE main.c 11;" d file: 9 | main main.c /^int main(int argc, char **argv) {$/;" f 10 | myCP main.c /^int myCP(char *src, char *des) {$/;" f 11 | myLS main.c /^int myLS() {$/;" f 12 | -------------------------------------------------------------------------------- /C++/src/Pthread/lab6/lab6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab6/lab6.pdf -------------------------------------------------------------------------------- /C++/src/Pthread/lab6/philosopher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define N 5 6 | #define LEFT(i) i 7 | #define RIGHT(i) (i+1)%N 8 | #define THINKING 0 9 | #define HUNGRY 2 10 | #define EATING 3 11 | 12 | //state of each philosopher 13 | int state[N]; 14 | 15 | pthread_mutex_t mutex; 16 | pthread_mutex_t chop[N]; 17 | 18 | 19 | 20 | void testChop(int number) { 21 | if(state[number] == HUNGRY && 22 | state[LEFT(number)] != EATING && 23 | state[RIGHT(number)] != EATING) { 24 | 25 | state[number] = EATING; 26 | pthread_mutex_unlock(&chop[number]); 27 | } 28 | } 29 | 30 | 31 | 32 | void takeChop(int number) { 33 | 34 | pthread_mutex_lock(&mutex); 35 | state[number] = HUNGRY; 36 | testChop(number); 37 | pthread_mutex_unlock(&mutex); 38 | pthread_mutex_lock(&chop[number]); 39 | } 40 | 41 | 42 | void putChop(int number) { 43 | pthread_mutex_lock(&mutex); 44 | state[number] = THINKING; 45 | testChop(LEFT(number)); 46 | testChop(RIGHT(number)); 47 | pthread_mutex_unlock(&mutex); 48 | } 49 | 50 | 51 | void thinking(int num) { 52 | pthread_mutex_lock(&mutex); 53 | state[num] = THINKING; 54 | printf("philosophre %d is thinking \n", num); 55 | pthread_mutex_unlock(&mutex); 56 | sleep(2); 57 | } 58 | 59 | 60 | void philosopher(int *num) { 61 | int number = *num; 62 | while(1) { 63 | thinking(number); 64 | 65 | takeChop(number); 66 | 67 | printf("philosopher %d is eating \n", number); 68 | sleep(2); 69 | 70 | putChop(number); 71 | } 72 | } 73 | int main() { 74 | 75 | pthread_t tid[5]; 76 | int i; 77 | for(i=0; i<5; i++) { 78 | pthread_mutex_init(&chop[i], NULL); 79 | } 80 | 81 | 82 | int pa[5]; 83 | for(i=0; i<5; i++) { 84 | pa[i] = i+1; 85 | pthread_create(&tid[i], NULL, (void *)philosopher, &pa[i]); 86 | } 87 | 88 | for(i=0; i<5; i++) { 89 | pthread_join(tid[i], NULL); 90 | } 91 | 92 | for(i=0; i<5; i++) { 93 | pthread_mutex_destroy(&chop[i]); 94 | } 95 | return 0; 96 | } 97 | 98 | 99 | -------------------------------------------------------------------------------- /C++/src/Pthread/lab6/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab6/test -------------------------------------------------------------------------------- /C++/src/Pthread/lab7/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab7/a.out -------------------------------------------------------------------------------- /C++/src/Pthread/lab7/lab7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/C++/src/Pthread/lab7/lab7.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/DP入门_阮行止.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/DP入门_阮行止.pptx -------------------------------------------------------------------------------- /OImaterial/动态规划/DP总结_翁家翌.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/DP总结_翁家翌.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划_叶国平.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划_叶国平.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划_李远韬.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划_李远韬.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划_钱雨杰.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划_钱雨杰.pptx -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划中的一些优化_孟泽楠.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划中的一些优化_孟泽楠.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划优化_翁家翌 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划优化_翁家翌 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划入门_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划入门_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划及其应用(一)_杨志灿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划及其应用(一)_杨志灿.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划及其应用(二)_杨志灿.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划及其应用(二)_杨志灿.pptx -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划引论_阮行止.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划引论_阮行止.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/动态规划问题优化模式2_杨志灿.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/动态规划问题优化模式2_杨志灿.pptx -------------------------------------------------------------------------------- /OImaterial/动态规划/基于连通性状态压缩的动态规划问题_陈丹琦.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/基于连通性状态压缩的动态规划问题_陈丹琦.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/树形动态规划_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/树形动态规划_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/树形动态规划_朱全民.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/树形动态规划_朱全民.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/树形数位动态规划_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/树形数位动态规划_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/状压与区间_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/状压与区间_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/状态压缩类型动态规划_朱全民..ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/状态压缩类型动态规划_朱全民..ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/组合计数与动态规划_曹钦翔.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/组合计数与动态规划_曹钦翔.ppt -------------------------------------------------------------------------------- /OImaterial/动态规划/背包与树形_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/背包与树形_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/动态规划/背包九讲.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/动态规划/背包九讲.pdf -------------------------------------------------------------------------------- /OImaterial/图论/两极相通——浅析最大最小定理在信息学竞赛中的应用_周冬.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/两极相通——浅析最大最小定理在信息学竞赛中的应用_周冬.ppt -------------------------------------------------------------------------------- /OImaterial/图论/二分图与匹配_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/二分图与匹配_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/倍增算法及其应用_未知作者.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/倍增算法及其应用_未知作者.pptx -------------------------------------------------------------------------------- /OImaterial/图论/分层图思想及其在信息学竞赛中的应用_肖天.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/分层图思想及其在信息学竞赛中的应用_肖天.ppt -------------------------------------------------------------------------------- /OImaterial/图论/图的连通_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图的连通_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/图论_李煜东.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图论_李煜东.pptx -------------------------------------------------------------------------------- /OImaterial/图论/图论专题之生成树_唐文斌.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图论专题之生成树_唐文斌.ppt -------------------------------------------------------------------------------- /OImaterial/图论/图论入门与最短路_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图论入门与最短路_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/图论复习_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图论复习_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/图论/图论知识及其应用_刘弈.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/图论知识及其应用_刘弈.ppt -------------------------------------------------------------------------------- /OImaterial/图论/树上倍增_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/树上倍增_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/树分治_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/树分治_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/树链剖分及其应用_蒋一瑶.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/树链剖分及其应用_蒋一瑶.pptx -------------------------------------------------------------------------------- /OImaterial/图论/浅析二分图匹配在信息学竞赛中的应用_王俊.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/浅析二分图匹配在信息学竞赛中的应用_王俊.ppt -------------------------------------------------------------------------------- /OImaterial/图论/浅谈一些树形问题_高胜寒.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/浅谈一些树形问题_高胜寒.ppt -------------------------------------------------------------------------------- /OImaterial/图论/浅谈信息学竞赛中的线性规划——简洁高效的单纯形法实现与应用_李宇骞.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/浅谈信息学竞赛中的线性规划——简洁高效的单纯形法实现与应用_李宇骞.ppt -------------------------------------------------------------------------------- /OImaterial/图论/生成树和拓扑排序_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/生成树和拓扑排序_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/由对称性解2-SAT问题_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/由对称性解2-SAT问题_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/图论/线性规划与网络流_曹钦翔.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/线性规划与网络流_曹钦翔.pptx -------------------------------------------------------------------------------- /OImaterial/图论/网络流_周聿浩 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/网络流_周聿浩 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/图论/网络流_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/网络流_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/图论/网络流_魏越闽.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/网络流_魏越闽.ppt -------------------------------------------------------------------------------- /OImaterial/图论/网络流建模_周尚彦.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/图论/网络流建模_周尚彦.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/C++模板与STL库介绍.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/C++模板与STL库介绍.ppt -------------------------------------------------------------------------------- /OImaterial/基础算法/C++的pb_ds库在OI中的应用_于纪平.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/C++的pb_ds库在OI中的应用_于纪平.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/NOIP归纳、贪心、二分、数论_未知作者.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/NOIP归纳、贪心、二分、数论_未知作者.pptx -------------------------------------------------------------------------------- /OImaterial/基础算法/万能的解题金钥匙——搜索_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/万能的解题金钥匙——搜索_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/基础算法/几道构造相关的题目_吉如一.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/几道构造相关的题目_吉如一.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/匹配算法在搜索问题中的巧用_楼天城.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/匹配算法在搜索问题中的巧用_楼天城.ppt -------------------------------------------------------------------------------- /OImaterial/基础算法/复杂度理论、排序、模拟_阮行止.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/复杂度理论、排序、模拟_阮行止.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/搜索分治贪⼼暴⼒和构造_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/搜索分治贪⼼暴⼒和构造_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/搜索及其优化_杨志灿.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/搜索及其优化_杨志灿.pptx -------------------------------------------------------------------------------- /OImaterial/基础算法/时间复杂度经典解说_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/时间复杂度经典解说_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/基础算法/普及组近5年NOIP试题分析_叶国平.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/普及组近5年NOIP试题分析_叶国平.ppt -------------------------------------------------------------------------------- /OImaterial/基础算法/暴力出奇迹_翁家翌.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/暴力出奇迹_翁家翌.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/稍微进阶一点的套路_阮行止.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/稍微进阶一点的套路_阮行止.pptx -------------------------------------------------------------------------------- /OImaterial/基础算法/贪心数学普及_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/贪心数学普及_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/贪心杂题选讲_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/贪心杂题选讲_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/基础算法/贪心问题选讲_王天懿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/基础算法/贪心问题选讲_王天懿.ppt -------------------------------------------------------------------------------- /OImaterial/字符串/HASH函数及其应用_朱全民.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/字符串/HASH函数及其应用_朱全民.ppt -------------------------------------------------------------------------------- /OImaterial/字符串/后缀数组——处理字符串的有力工具_罗穗骞.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/字符串/后缀数组——处理字符串的有力工具_罗穗骞.ppt -------------------------------------------------------------------------------- /OImaterial/字符串/后缀自动机_陈立杰.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/字符串/后缀自动机_陈立杰.pptx -------------------------------------------------------------------------------- /OImaterial/字符串/多串匹配算法及其启示_朱泽园.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/字符串/多串匹配算法及其启示_朱泽园.ppt -------------------------------------------------------------------------------- /OImaterial/字符串/字符串算法选讲_金策.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/字符串/字符串算法选讲_金策.pdf -------------------------------------------------------------------------------- /OImaterial/数学/ACM中的数学问题_林舒.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/ACM中的数学问题_林舒.ppt -------------------------------------------------------------------------------- /OImaterial/数学/Fourier transform_郭晓旭.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/Fourier transform_郭晓旭.pdf -------------------------------------------------------------------------------- /OImaterial/数学/IOI2013与博弈论_王康宁.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/IOI2013与博弈论_王康宁.pptx -------------------------------------------------------------------------------- /OImaterial/数学/OI 中的超现实数和不平等博弈问题_杜瑜皓.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/OI 中的超现实数和不平等博弈问题_杜瑜皓.pdf -------------------------------------------------------------------------------- /OImaterial/数学/OI中的数学_金靖.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/OI中的数学_金靖.pptx -------------------------------------------------------------------------------- /OImaterial/数学/OI中的概率_黄涛岸.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/OI中的概率_黄涛岸.ppt -------------------------------------------------------------------------------- /OImaterial/数学/一些数学知识_王若松.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/一些数学知识_王若松.ppt -------------------------------------------------------------------------------- /OImaterial/数学/多项式及求和_杜瑜皓.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/多项式及求和_杜瑜皓.pptx -------------------------------------------------------------------------------- /OImaterial/数学/对拟阵的初步研究_刘雨辰.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/对拟阵的初步研究_刘雨辰.pdf -------------------------------------------------------------------------------- /OImaterial/数学/数学_易健驰.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/数学_易健驰.pdf -------------------------------------------------------------------------------- /OImaterial/数学/数学_翁家翌 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/数学_翁家翌 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数学/数学杂题选讲_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/数学杂题选讲_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数学/数论入门_阮行止.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/数论入门_阮行止.pptx -------------------------------------------------------------------------------- /OImaterial/数学/数论及其应用_周聿浩 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/数论及其应用_周聿浩 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数学/最近一年 THU 命题组出过的数学题选讲_翁家翌.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/最近一年 THU 命题组出过的数学题选讲_翁家翌.pdf -------------------------------------------------------------------------------- /OImaterial/数学/概率与随机化算法_钟诚.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/概率与随机化算法_钟诚.pptx -------------------------------------------------------------------------------- /OImaterial/数学/浅析非完美算法在信息学竞赛中的应用_胡伟栋.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/浅析非完美算法在信息学竞赛中的应用_胡伟栋.ppt -------------------------------------------------------------------------------- /OImaterial/数学/离散数学在信息学竞赛中的运用_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/离散数学在信息学竞赛中的运用_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/数学/简单易懂的质数筛法_陈牧歌.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/简单易懂的质数筛法_陈牧歌.pdf -------------------------------------------------------------------------------- /OImaterial/数学/线性筛法与积性函数_贾志鹏.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/线性筛法与积性函数_贾志鹏.pptx -------------------------------------------------------------------------------- /OImaterial/数学/组合数学——母函数与递推_朱全民.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/组合数学——母函数与递推_朱全民.ppt -------------------------------------------------------------------------------- /OImaterial/数学/组合游戏略述——浅谈SG游戏的若干拓展及变形_贾志豪.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/组合游戏略述——浅谈SG游戏的若干拓展及变形_贾志豪.ppt -------------------------------------------------------------------------------- /OImaterial/数学/莫比乌斯反演_王天懿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/莫比乌斯反演_王天懿.ppt -------------------------------------------------------------------------------- /OImaterial/数学/计数与期望问题选讲_陈立杰.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/计数与期望问题选讲_陈立杰.pdf -------------------------------------------------------------------------------- /OImaterial/数学/计数问题选讲_陈立杰.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数学/计数问题选讲_陈立杰.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/K-D Tree_翁家翌.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/K-D Tree_翁家翌.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/Splay树及其应用_朱全民.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/Splay树及其应用_朱全民.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/k-d tree在传统OI数据结构题中的应用_任之洲.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/k-d tree在传统OI数据结构题中的应用_任之洲.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/二叉树与其应用_朱全民.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/二叉树与其应用_朱全民.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/分块算法_未知作者.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/分块算法_未知作者.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/分块算法及简单扩展_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/分块算法及简单扩展_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/可并堆_王天懿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/可并堆_王天懿.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/基本数据结构在信息学竞赛中的应用_朱晨光.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/基本数据结构在信息学竞赛中的应用_朱晨光.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/左偏树的特点及其应用_黄源河.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/左偏树的特点及其应用_黄源河.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/平衡树_王天懿.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/平衡树_王天懿.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/提高组数据结构_吉如一.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/提高组数据结构_吉如一.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/数据结构_黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/数据结构_黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/数据结构入门_许昊然.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/数据结构入门_许昊然.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/普及数据结构_阮行止.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/普及数据结构_阮行止.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/树链剖分_王天懿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/树链剖分_王天懿.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/根号算法杂谈_杨志灿.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/根号算法杂谈_杨志灿.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/线段树_浙大ACM.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/线段树_浙大ACM.ppt -------------------------------------------------------------------------------- /OImaterial/数据结构/线段树_翁家翌 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/线段树_翁家翌 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/数据结构/线段树的合并_黄嘉泰.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/线段树的合并_黄嘉泰.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/统计的力量——线段树全接触_张昆玮.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/统计的力量——线段树全接触_张昆玮.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/莫队算法_刘孟博.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/莫队算法_刘孟博.pptx -------------------------------------------------------------------------------- /OImaterial/数据结构/解决动态统计问题的两把利刃——剖析线段树与矩形切割_薛矛.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/数据结构/解决动态统计问题的两把利刃——剖析线段树与矩形切割_薛矛.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/二维计算几何相关算法与实战应用_徐明宽.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/二维计算几何相关算法与实战应用_徐明宽.pptx -------------------------------------------------------------------------------- /OImaterial/计算几何/半平面交的新算法及其实用价值_朱泽园.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/半平面交的新算法及其实用价值_朱泽园.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/叉积的应用_卓亮.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/叉积的应用_卓亮.pdf -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_未知作者 2.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_未知作者 2.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_未知作者.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_未知作者.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_翁家翌 & 黄哲威.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_翁家翌 & 黄哲威.pdf -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_邓东.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_邓东.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_钟思思.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_钟思思.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何_黄涛岸.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何_黄涛岸.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何及其应用_林厚从.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何及其应用_林厚从.ppt -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何教程_北大ACM.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何教程_北大ACM.pptx -------------------------------------------------------------------------------- /OImaterial/计算几何/计算几何讨论_胡伟栋.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/haotianmichael/algorithm/ba7dbd3c23ea29c445a0cd4772aecc7993045a04/OImaterial/计算几何/计算几何讨论_胡伟栋.pptx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 算法/C++学习笔记整理 2 | 3 | --------------------------------------------------------------------------------