├── .hgignore ├── .hgtags ├── README.md ├── example └── 02-word-count │ ├── Tokenizer.h │ ├── aesop.txt │ ├── main.cpp │ ├── mobydick.txt │ └── test.txt ├── exercise ├── dq1_dense │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ └── 9.sol │ ├── dense.odt │ ├── dense.pdf │ ├── package │ │ ├── densegraph.h │ │ └── main_dense.cpp │ └── solution │ │ ├── densegraph.h │ │ └── main_dense.cpp ├── dq1_postfix │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ ├── eval.rb │ │ └── gen.rb │ ├── package │ │ ├── main.cpp │ │ ├── postfix.cbp │ │ ├── postfix.depend │ │ └── student.h │ ├── postfix.odt │ ├── postfix.pdf │ └── solution │ │ ├── main │ │ ├── main.cpp │ │ └── student.cpp ├── dq1_sbv │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ └── 9.sol │ ├── package │ │ ├── sbv.cbp │ │ ├── sbv.layout │ │ ├── stack.h │ │ ├── student.h │ │ └── test_stack.cpp │ ├── sbv.odt │ ├── sbv.pdf │ └── solution │ │ ├── stack.h │ │ ├── student.h │ │ ├── test_stack │ │ └── test_stack.cpp ├── dq1_sbv2 │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ └── 9.sol │ ├── package │ │ ├── sbv2.cbp │ │ ├── stack.h │ │ ├── student.h │ │ └── test_stack.cpp │ ├── sbv.odt │ ├── sbv.pdf │ └── solution │ │ ├── stack.h │ │ ├── student.h │ │ ├── test_stack │ │ └── test_stack.cpp ├── dq1_sparse │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ └── 9.sol │ ├── package │ │ ├── main_sparse.cpp │ │ ├── sparse.cbp │ │ └── sparsegraph.h │ ├── solution │ │ ├── a.out │ │ ├── main_sparse │ │ ├── main_sparse.cpp │ │ ├── sparsegraph.h │ │ ├── sparsegraph_map.h │ │ ├── sparsegraph_set.h │ │ └── sparsegraph_vector.h │ ├── sparse.odt │ └── sparse.pdf ├── dq1_vector_equal │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 11.in │ │ ├── 11.sol │ │ ├── 12.in │ │ ├── 12.sol │ │ ├── 13.in │ │ ├── 13.sol │ │ ├── 14.in │ │ ├── 14.sol │ │ ├── 15.in │ │ ├── 15.sol │ │ ├── 16.in │ │ ├── 16.sol │ │ ├── 17.in │ │ ├── 17.sol │ │ ├── 18.in │ │ ├── 18.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── package │ │ ├── main.cpp │ │ ├── student.h │ │ ├── vector.h │ │ └── vequal.cbp │ ├── solution │ │ ├── main │ │ ├── main.cpp │ │ ├── student.h │ │ └── vector.h │ ├── vequal.odt │ └── vequal.pdf ├── ds00_reverse │ ├── .main.cpp.swp │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── main.cpp │ ├── reverse.odt │ ├── reverse.pdf │ └── solution │ │ ├── main │ │ ├── main.cpp │ │ └── main.o ├── ds00_reverse2 │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── main.cpp │ ├── reverse2.odt │ ├── reverse2.pdf │ └── solution │ │ ├── main │ │ └── main.cpp ├── ds01_most_freq │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ ├── gen.rb │ │ └── mobydick.txt │ ├── most.odt │ ├── most.pdf │ └── solution │ │ ├── main │ │ └── main.cpp ├── ds01_pair_sum │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── pair_sum.odt │ ├── pair_sum.pdf │ └── sol │ │ ├── sol │ │ ├── sol-wrong │ │ ├── sol-wrong.cpp │ │ └── sol.cpp ├── ds02_intersection │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 11.in │ │ ├── 11.sol │ │ ├── 12.in │ │ ├── 12.sol │ │ ├── 13.in │ │ ├── 13.sol │ │ ├── 14.in │ │ ├── 14.sol │ │ ├── 15.in │ │ ├── 15.sol │ │ ├── 16.in │ │ ├── 16.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── intersection.odt │ ├── intersection.pdf │ └── solution │ │ └── main.cpp ├── ds02_is_reverse │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 11.in │ │ ├── 11.sol │ │ ├── 12.in │ │ ├── 12.sol │ │ ├── 13.in │ │ ├── 13.sol │ │ ├── 14.in │ │ ├── 14.sol │ │ ├── 15.in │ │ ├── 15.sol │ │ ├── 16.in │ │ ├── 16.sol │ │ ├── 17.in │ │ ├── 17.sol │ │ ├── 18.in │ │ ├── 18.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ ├── 9.sol │ │ └── gen.rb │ ├── main.cpp │ ├── reverse.odt │ ├── reverse.pdf │ └── solution │ │ └── main.cpp ├── ds03_heap_erase │ ├── data │ │ ├── 1.in │ │ ├── 1.sol │ │ ├── 10.in │ │ ├── 10.sol │ │ ├── 2.in │ │ ├── 2.sol │ │ ├── 3.in │ │ ├── 3.sol │ │ ├── 4.in │ │ ├── 4.sol │ │ ├── 5.in │ │ ├── 5.sol │ │ ├── 6.in │ │ ├── 6.sol │ │ ├── 7.in │ │ ├── 7.sol │ │ ├── 8.in │ │ ├── 8.sol │ │ ├── 9.in │ │ └── 9.sol │ ├── heap_erase.cpp │ ├── heap_erase.odt │ ├── heap_erase.pdf │ └── solution │ │ └── heap_erase.cpp └── ds03_heap_order │ ├── data │ ├── 1.in │ ├── 1.sol │ ├── 10.in │ ├── 10.sol │ ├── 2.in │ ├── 2.sol │ ├── 3.in │ ├── 3.sol │ ├── 4.in │ ├── 4.sol │ ├── 5.in │ ├── 5.sol │ ├── 6.in │ ├── 6.sol │ ├── 7.in │ ├── 7.sol │ ├── 8.in │ ├── 8.sol │ ├── 9.in │ ├── 9.sol │ ├── gen.rb │ └── name.txt │ ├── heap_order.cpp │ ├── order.odt │ ├── order.pdf │ └── solution │ ├── sol1.cpp │ ├── sol2.cpp │ └── sol3.cpp └── stl-cp ├── list.h ├── map_avl.h ├── map_bst.h ├── pair.h ├── priority_queue.h ├── queue.h ├── stack.h ├── test_list.cpp ├── test_map.cpp ├── test_pair.cpp ├── test_priority_queue.cpp ├── test_queue.cpp ├── test_stack.cpp ├── test_unordered_map.cpp ├── test_vector.cpp ├── unordered_map.h ├── unordered_map_oa.h └── vector.h /.hgignore: -------------------------------------------------------------------------------- 1 | syntax: glob 2 | 3 | *.zip 4 | *.o 5 | *.swp 6 | *.orig 7 | .*\# 8 | -------------------------------------------------------------------------------- /.hgtags: -------------------------------------------------------------------------------- 1 | a72e1ab26c20e8065be29d15685b92f4012a4aa4 vector 2 | 2fddb8f04dbbdd1c5516c7bb7efead43394a5d23 pair 3 | 7a451cc59251e36c559520658c6d56f50283e29a stack 4 | 7a451cc59251e36c559520658c6d56f50283e29a stack 5 | 3ba58ed793a8ba779bcc8c917b8329c33d5e06e3 stack 6 | c916ac71f5ba805cc2b5c56e5ecc5bfb5ca98ed7 queue 7 | e89a54845003973bca462483ba657b4c74363cd1 quiz1 8 | 3f0197b6fd2c3c792033cff81434b9201a5557ed BinaryHeap 9 | 3f0197b6fd2c3c792033cff81434b9201a5557ed BinaryHeap 10 | 50c027dd9878d2a6134c7b9ed2414a054c5db5d2 BinaryHeap 11 | 9aef48bce9e0f2af062be4e6bb494d2d500f5363 hash 12 | 50c027dd9878d2a6134c7b9ed2414a054c5db5d2 BinaryHeap 13 | 400251dfcdcc060e95c4d4e115aef7cdcf912bc9 BinaryHeap 14 | 400251dfcdcc060e95c4d4e115aef7cdcf912bc9 BinaryHeap 15 | 2e969fbdf29221d7d0fd82ac53671d8cca67dafb BinaryHeap 16 | f7acff1e3b8406879ebb0f83df958f71b57fb6ce list 17 | 2c36a3b0a016cb3ca94873da0ff2368e6c0cf09e bst 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # data-class # 2 | 3 | Repository สำหรับ sourcec code ที่ใช้ในวิชา 2110211 Introduction to Data Structure ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ จุฬาลงกรณ์มหาวิทยาลัย 4 | 5 | version ปัจจุบันคือ 0.4.2 6 | 7 | ### การใช้งาน ### 8 | 9 | ใน repository จะมี folder หลักอยู่ 2 folder ได้แก่ exercise และ stl-cp โดยที่ exercise นั้นจะเป็นการบ้าน พร้อมเฉลย โดยรูปแบบการใช้งานจะเป็นโจทย์สำหรับระบบ [grader](http:/nattee/cafe-grader-web) ส่วน stl-cp นั้นจะเป็น source-code สำหรับโครงสร้างข้อมูลต่าง ๆ พร้อมด้วยตัวอย่างการใช้งาน โดยที่ไฟล์ test_xxx.cpp นั้นจะเป็นตัวอย่างการใช้งานของไฟล์ xxx.h 10 | 11 | ### stl-cp ### 12 | 13 | STL-CP เป็นโครงสร้างข้อมูลในภาษา C++ ซึ่งเขียนขึ้นมาโดยมีวัตถุประสงค์เพื่อใช้ในการเรียนการสอน โดยพยายามให้สามารถใช้งานพื้นฐานได้เหมือนกับ standard template library (STL) ในภาษา C++ โดยใน STL-CP นั้นจะมีโครงสร้างข้อมูล vector, stack, queue, priority_queue, list, map, unordered_map ให้ใช้งาน 14 | 15 | การใช้งานนั้นจะต้อง compile ด้วย C++ เวอร์ชัน c++11 (ใช้ option -std=c++11 ในการ compile) และโครงสร้างข้อมูลทั้งหมดจะมีชื่อ class เหมือนกับ STL แต่อยู่ภายใต้ namespace CP 16 | 17 | ### ผู้เขียน ### 18 | 19 | เอกสาร, source code, ฯลฯ ใน repository นี้ถูกพัฒนาขึ้นโดย นัทที นิภานันท์, สมชาย ประสิทธิจูตระกูล, ณัฐพงศ์ ชินธเนศ และ วิษณุ โคตรจรัส รวมถึงคนอื่น ๆ อีกหลายคนที่ช่วยหา bug และข้อผิดพลาดต่าง ๆ สำหรับข้อผิดพลาดใด ๆ หรือข้อเสนอแนะ กรุณาแจ้งได้โดยตรงที่ [nattee/data-class](http:/nattee/data-class) -------------------------------------------------------------------------------- /example/02-word-count/Tokenizer.h: -------------------------------------------------------------------------------- 1 | #ifndef TOKENIZER_H 2 | #define TOKENIZER_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Tokenizer 11 | { 12 | public: 13 | Tokenizer(string fileName) { 14 | fin.open(fileName.c_str()); 15 | token = NULL; 16 | if (fin.is_open()) { 17 | if (getline(fin,line)) { 18 | token = strtok( (char*)line.c_str(), delims ); 19 | } 20 | } 21 | } 22 | string next() { 23 | string s = token; 24 | token = strtok( NULL, delims ); 25 | while (token == NULL) { 26 | if (getline(fin,line)) { 27 | token = strtok( (char*)line.c_str(), delims ); 28 | } else { 29 | break; 30 | } 31 | } 32 | return s; 33 | } 34 | bool hasNext() { 35 | return token != NULL; 36 | } 37 | void close() { 38 | fin.close(); 39 | } 40 | static const char *delims; 41 | 42 | private: 43 | char *token; 44 | ifstream fin; 45 | string line; 46 | 47 | }; 48 | 49 | const char *Tokenizer::delims = " \"\'[](){},.!@#$%^&|~-=<>?;:+-*_/\\\n"; 50 | 51 | 52 | #endif // TOKENIZER_H 53 | -------------------------------------------------------------------------------- /example/02-word-count/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "Tokenizer.h" 8 | 9 | using namespace std; 10 | 11 | void printUniqueWords1(string filename); 12 | void printUniqueWords2(string filename); 13 | void printUniqueWords3(string filename); 14 | void printUniqueWords4(string filename); 15 | void printWord(string filename); 16 | 17 | int main() { 18 | string filename = "mobydick.txt"; 19 | //printWord(filename); 20 | printUniqueWords1(filename); 21 | //printUniqueWords2(filename); 22 | //printUniqueWords3(filename); 23 | //printUniqueWords4(filename); 24 | return 0; 25 | } 26 | //------------------------------------------------------- 27 | bool search(string words[], int n, string w) { 28 | for (int i = 0; i < n; i++) { 29 | if (words[i] == w) return true; 30 | } 31 | return false; 32 | } 33 | 34 | //---------------------- base -------------------------- 35 | void printWord(string filename) { 36 | int n = 0; 37 | 38 | Tokenizer tokenizer(filename); 39 | while(tokenizer.hasNext()) { 40 | string token = tokenizer.next(); 41 | n++; 42 | } 43 | tokenizer.close(); 44 | cout << "A total of " << n << " words" << endl; 45 | } 46 | //------------------------------------------------------- 47 | // big enough array 48 | void printUniqueWords1(string filename) { 49 | string words[100000]; 50 | int n = 0; 51 | 52 | Tokenizer tokenizer(filename); 53 | while(tokenizer.hasNext()) { 54 | string token = tokenizer.next(); 55 | if (!search(words,n,token)) words[n++] = token; 56 | } 57 | tokenizer.close(); 58 | cout << "A total of " << n << " words" << endl; 59 | } 60 | //------------------------------------------------------- 61 | // expandable array 62 | void printUniqueWords2(string filename) { 63 | int cap = 1; 64 | string *words; 65 | words = new string[cap]; 66 | int n = 0; 67 | Tokenizer tokenizer(filename); 68 | while(tokenizer.hasNext()) { 69 | string token = tokenizer.next(); 70 | if (!search(words,n,token)) { 71 | if (n == cap) { 72 | string *a = new string[2*cap]; 73 | for (int i=0; i words; 88 | Tokenizer tokenizer(filename); 89 | while(tokenizer.hasNext()) { 90 | string token = tokenizer.next(); 91 | if (words.end() == find(words.begin(), words.end(), token)) 92 | words.push_back(token); 93 | } 94 | tokenizer.close(); 95 | cout << "A total of " << words.size() << " words" << endl; 96 | } 97 | //------------------------------------------------------- 98 | // set 99 | void printUniqueWords4(string filename) { 100 | set words; 101 | Tokenizer tokenizer(filename); 102 | while(tokenizer.hasNext()) { 103 | string token = tokenizer.next(); 104 | //if (words.end() == find(words.begin(), words.end(), token)) 105 | if (words.end() == words.find(token)) 106 | words.insert(token); 107 | } 108 | tokenizer.close(); 109 | cout << "A total of " << words.size() << " words" << endl; 110 | } 111 | //------------------------------------------------------- 112 | -------------------------------------------------------------------------------- /example/02-word-count/test.txt: -------------------------------------------------------------------------------- 1 | Fear leads to anger; anger leads to hatred; hatred leads to conflict; conflict leads to suffering -------------------------------------------------------------------------------- /exercise/dq1_dense/data/1.in: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/1.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/10.in: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/10.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/2.in: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/2.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/3.in: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/3.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/4.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/4.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/5.in: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/5.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/6.in: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/6.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/7.in: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/7.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/8.in: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/8.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/9.in: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/data/9.sol: -------------------------------------------------------------------------------- 1 | YES!$@* 2 | -------------------------------------------------------------------------------- /exercise/dq1_dense/dense.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_dense/dense.odt -------------------------------------------------------------------------------- /exercise/dq1_dense/dense.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_dense/dense.pdf -------------------------------------------------------------------------------- /exercise/dq1_dense/package/densegraph.h: -------------------------------------------------------------------------------- 1 | #ifndef __DENSE_GRAPH_H__ 2 | #define __DENSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | using namespace std; 7 | class DenseGraph{ 8 | public: 9 | DenseGraph() { 10 | // Your code here 11 | } 12 | 13 | DenseGraph(int n_in) { 14 | // Your code here 15 | } 16 | 17 | DenseGraph(const DenseGraph& G) { 18 | // Your code here 19 | } 20 | 21 | void AddEdge(int a, int b) { 22 | // Your code here 23 | } 24 | 25 | void RemoveEdge(int a, int b) { 26 | // Your code here 27 | } 28 | 29 | bool DoesEdgeExist(int a, int b) const { 30 | // Your code here 31 | } 32 | 33 | DenseGraph Transpose() const { 34 | // Your code here 35 | } 36 | 37 | protected: 38 | int n; 39 | // Your code here 40 | }; 41 | #endif // __DENSE_GRAPH_H__ 42 | -------------------------------------------------------------------------------- /exercise/dq1_dense/solution/densegraph.h: -------------------------------------------------------------------------------- 1 | #ifndef __DENSE_GRAPH_H__ 2 | #define __DENSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | using namespace std; 7 | class DenseGraph{ 8 | public: 9 | DenseGraph() { 10 | // Your code here 11 | n = 3; 12 | g.resize(n); 13 | for (int i =0; i < n; i++) { 14 | g[i].resize(n,0); 15 | } 16 | } 17 | 18 | DenseGraph(int n_in) { 19 | // Your code here 20 | n = n_in; 21 | g.resize(n); 22 | for (int i =0; i < n; i++) { 23 | g[i].resize(n,0); 24 | } 25 | } 26 | 27 | DenseGraph(const DenseGraph& G) { 28 | // Your code here 29 | n = G.n; 30 | g.resize(n); 31 | for (int i =0; i < n; i++) { 32 | g[i] = G.g[i]; 33 | } 34 | } 35 | 36 | DenseGraph& operator=(const DenseGraph& G) { 37 | // Your code here 38 | n = G.n; 39 | g.resize(n); 40 | for (int i =0; i < n; i++) { 41 | g[i] = G.g[i]; 42 | } 43 | return *this; 44 | } 45 | 46 | void AddEdge(int a, int b) { 47 | // Your code here 48 | g[a][b] = 1; 49 | } 50 | 51 | void RemoveEdge(int a, int b) { 52 | // Your code here 53 | g[a][b] = 0; 54 | } 55 | 56 | bool DoesEdgeExist(int a, int b) const { 57 | // Your code here 58 | return g[a][b] > 0; 59 | } 60 | 61 | DenseGraph Transpose() const { 62 | DenseGraph GT(n); 63 | // Your code here 64 | for (int i = 0; i < n; i++) { 65 | for (int j = 0; j < n; j++) { 66 | GT.g[i][j] = g[j][i]; 67 | } 68 | } 69 | return GT; 70 | } 71 | 72 | protected: 73 | int n; 74 | // Your code here 75 | 76 | vector > g; 77 | }; 78 | 79 | #endif // __DENSE_GRAPH_H__ 80 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/1.in: -------------------------------------------------------------------------------- 1 | 5 2 | 1 996 3 | 1 716 4 | 0 2 5 | 1 707 6 | 0 1 7 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/1.sol: -------------------------------------------------------------------------------- 1 | 712429 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/10.sol: -------------------------------------------------------------------------------- 1 | 84958 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/2.in: -------------------------------------------------------------------------------- 1 | 9 2 | 1 983 3 | 1 518 4 | 0 1 5 | 1 350 6 | 1 607 7 | 1 629 8 | 0 2 9 | 0 3 10 | 0 2 11 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/2.sol: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/3.in: -------------------------------------------------------------------------------- 1 | 19 2 | 1 480 3 | 1 212 4 | 1 705 5 | 0 3 6 | 0 1 7 | 1 440 8 | 0 0 9 | 1 956 10 | 0 3 11 | 1 112 12 | 1 780 13 | 1 24 14 | 0 0 15 | 0 1 16 | 0 1 17 | 1 911 18 | 1 296 19 | 0 3 20 | 0 1 21 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/3.sol: -------------------------------------------------------------------------------- 1 | 689 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/4.sol: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/5.sol: -------------------------------------------------------------------------------- 1 | -60908 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/6.sol: -------------------------------------------------------------------------------- 1 | -603984 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/7.sol: -------------------------------------------------------------------------------- 1 | -93124 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/8.sol: -------------------------------------------------------------------------------- 1 | 56478 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/9.sol: -------------------------------------------------------------------------------- 1 | -46890 2 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/eval.rb: -------------------------------------------------------------------------------- 1 | 2 | def eval_post(filename) 3 | s = [] 4 | File.open(filename,"r") do |f| 5 | n = f.gets.chomp.to_i 6 | n.times do 7 | a,b = f.gets.split.map{|x| x.to_i} 8 | if a == 0 9 | r = 0 10 | case b 11 | when 0 12 | r = s[-2] + s[-1] 13 | when 1 14 | r = s[-2] - s[-1] 15 | when 2 16 | r = s[-2] * s[-1] 17 | when 3 18 | r = (s[-2] / s[-1]).to_i 19 | end 20 | puts "#{s[-2]} #{b} #{s[-1]} result = #{r}" 21 | s.pop 22 | s.pop 23 | s << r 24 | else 25 | s << b 26 | end 27 | end 28 | puts "result = #{s[0]}" 29 | 30 | 31 | end 32 | end 33 | 34 | 35 | eval_post('3.in') 36 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename, n) 2 | n -= 1 if n.even? 3 | d = [] 4 | s = [] 5 | while (n > 0) do 6 | if (s.length >= 2 and rand(2) == 0) or (n < s.length) 7 | op,r = 0,0 8 | loop do 9 | op = rand(4) 10 | case op 11 | when 0 12 | r = s[-2] + s[-1] 13 | when 1 14 | r = s[-2] - s[-1] 15 | when 2 16 | r = s[-2] * s[-1] 17 | when 3 18 | next if s[-1] <= 0 19 | next if s[-2] < 0 20 | r = (s[-2] / s[-1]).to_i 21 | end 22 | break if r.abs < 1000000 23 | end 24 | #puts "#{s[-2]} #{op} #{s[-1]}" 25 | s.pop 26 | s[-1] = r 27 | d << [0,op] 28 | else 29 | d << [1,rand(1000)] 30 | s << d[-1][1] 31 | end 32 | n -= 1 33 | end 34 | puts "result = #{s[0]}" 35 | File.open(filename,"w") do |f| 36 | f.puts d.length 37 | d.each do |i| 38 | f.puts i.join ' ' 39 | end 40 | end 41 | end 42 | 43 | genOneFile('1.in',5) 44 | genOneFile('2.in',10) 45 | genOneFile('3.in',20) 46 | genOneFile('4.in',500) 47 | genOneFile('5.in',500) 48 | genOneFile('6.in',1000) 49 | genOneFile('7.in',5000) 50 | genOneFile('8.in',5000) 51 | genOneFile('9.in',5000) 52 | genOneFile('10.in',5000) 53 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/package/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #include "student.h" 8 | 9 | int main() { 10 | vector > v; 11 | int n,a,b; 12 | cin >> n; 13 | while (n--) { 14 | cin >> a >> b; 15 | v.push_back(make_pair(a,b)); 16 | } 17 | cout << eval_postfix(v) << endl; 18 | } 19 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/package/postfix.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 44 | 45 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/package/postfix.depend: -------------------------------------------------------------------------------- 1 | # depslib dependency file v1.0 2 | 1424794088 source:/dae/Teaching/Data Structure/2557 - b/quiz/dq1_postfix/package/main.cpp 3 | 4 | 5 | 6 | "student.cpp" 7 | 8 | 1424795320 /dae/Teaching/Data Structure/2557 - b/quiz/dq1_postfix/package/student.cpp 9 | 10 | 11 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/package/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H__ 2 | #define __STUDENT_H__ 3 | #include 4 | 5 | using namespace std; 6 | 7 | int eval_postfix(vector > v) { 8 | //WRITE YOUR CODE HERE 9 | //DON"T FORGET TO RETURN THE RESULT 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/postfix.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_postfix/postfix.odt -------------------------------------------------------------------------------- /exercise/dq1_postfix/postfix.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_postfix/postfix.pdf -------------------------------------------------------------------------------- /exercise/dq1_postfix/solution/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_postfix/solution/main -------------------------------------------------------------------------------- /exercise/dq1_postfix/solution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #include "student.cpp" 8 | 9 | int main() { 10 | vector > v; 11 | int n,a,b; 12 | cin >> n; 13 | while (n--) { 14 | cin >> a >> b; 15 | v.push_back(make_pair(a,b)); 16 | } 17 | cout << eval_postfix(v) << endl; 18 | } 19 | -------------------------------------------------------------------------------- /exercise/dq1_postfix/solution/student.cpp: -------------------------------------------------------------------------------- 1 | int eval_postfix(vector > v) { 2 | //WRITE YOUR CODE HERE 3 | stack s; 4 | for (auto &x : v) { 5 | if (x.first == 1) { 6 | s.push(x.second); 7 | } else { 8 | int a,b; 9 | if (s.empty()) { 10 | cout << "ERROR" << endl; 11 | exit(0); 12 | } 13 | b = s.top(); s.pop(); 14 | if (s.empty()) { 15 | cout << "ERROR" << endl; 16 | exit(0); 17 | } 18 | a = s.top(); s.pop(); 19 | if (x.second == 0) s.push(a+b); 20 | else if (x.second == 1) s.push(a-b); 21 | else if (x.second == 2) s.push(a*b); 22 | else if (x.second == 3) s.push(a/b); 23 | } 24 | } 25 | if (s.size() != 1) { 26 | cout << "END ERROR" << endl; 27 | exit(0); 28 | } 29 | //DON"T FORGET TO RETURN THE RESULT 30 | return s.top(); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/1.in: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/1.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 1 0 3 | 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/10.in: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/10.sol: -------------------------------------------------------------------------------- 1 | OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/2.in: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/2.sol: -------------------------------------------------------------------------------- 1 | 4 0 2 | nattee 3 | 3 0 4 | nuttapong 5 | 2 0 6 | vishnu 7 | 1 0 8 | somchai 9 | 0 1 10 | OK 11 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/3.in: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/3.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 0 1 3 | OK 4 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/4.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/4.sol: -------------------------------------------------------------------------------- 1 | OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/5.in: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/5.sol: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | 2 0 4 | 0 1 5 | OK 6 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/6.in: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/6.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 500 0499 3 | 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/7.in: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/7.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 500000 0499999 3 | 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/8.in: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/8.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 5000000 04999999 3 | 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/9.in: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/data/9.sol: -------------------------------------------------------------------------------- 1 | OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/package/sbv.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/package/sbv.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/package/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_STACK_INCLUDED_ 2 | #define _CP_STACK_INCLUDED_ 3 | 4 | #include 5 | #include 6 | #include 7 | //#pragma once 8 | 9 | namespace CP { 10 | 11 | template 12 | class stack 13 | { 14 | protected: 15 | std::vector v; 16 | 17 | public: 18 | //-------------- constructor ---------- 19 | 20 | // copy constructor 21 | stack(const stack& a) { 22 | } 23 | 24 | // default constructor 25 | stack() { } 26 | 27 | ~stack() { } 28 | 29 | //------------- capacity function ------------------- 30 | bool empty() const { 31 | return v.empty(); 32 | } 33 | 34 | //------------ to be implemented by student ---------- 35 | size_t size() const; 36 | const T& top() const; 37 | void push(const T& element); 38 | void pop(); 39 | }; 40 | 41 | } 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/package/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | #include "stack.h" 5 | #include 6 | 7 | template 8 | size_t CP::stack::size() const { 9 | //write your code here 10 | } 11 | 12 | template 13 | const T& CP::stack::top() const { 14 | //write your code here 15 | } 16 | 17 | template 18 | void CP::stack::push(const T& element) { 19 | //write your code here 20 | } 21 | 22 | template 23 | void CP::stack::pop() { 24 | //write your code here 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/sbv.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv/sbv.odt -------------------------------------------------------------------------------- /exercise/dq1_sbv/sbv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv/sbv.pdf -------------------------------------------------------------------------------- /exercise/dq1_sbv/solution/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_STACK_INCLUDED_ 2 | #define _CP_STACK_INCLUDED_ 3 | 4 | #include 5 | #include 6 | #include 7 | //#pragma once 8 | 9 | namespace CP { 10 | 11 | template 12 | class stack 13 | { 14 | protected: 15 | std::vector v; 16 | 17 | public: 18 | //-------------- constructor ---------- 19 | 20 | // copy constructor 21 | stack(const stack& a) { 22 | v = a.v; 23 | } 24 | 25 | // default constructor 26 | stack() { } 27 | 28 | ~stack() { } 29 | 30 | //------------- capacity function ------------------- 31 | bool empty() const { 32 | return v.empty(); 33 | } 34 | 35 | //------------ to be implemented by student ---------- 36 | size_t size() const; 37 | const T& top() const; 38 | void push(const T& element); 39 | void pop(); 40 | }; 41 | 42 | } 43 | 44 | #endif 45 | 46 | 47 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/solution/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | #include "stack.h" 5 | #include 6 | 7 | template 8 | size_t CP::stack::size() const { 9 | //write your code here 10 | return v.size(); 11 | } 12 | 13 | template 14 | const T& CP::stack::top() const { 15 | //write your code here 16 | return v.back(); 17 | } 18 | 19 | template 20 | void CP::stack::push(const T& element) { 21 | //write your code here 22 | v.push_back(element); 23 | } 24 | 25 | template 26 | void CP::stack::pop() { 27 | //write your code here 28 | v.pop_back(); 29 | } 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /exercise/dq1_sbv/solution/test_stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv/solution/test_stack -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/1.in: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/1.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 1 0 3 | 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/10.in: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/10.sol: -------------------------------------------------------------------------------- 1 | 192 182 165 149 148 142 125 109 108 102 85 69 68 62 45 29 28 22 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/2.in: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/2.sol: -------------------------------------------------------------------------------- 1 | 4 0 2 | nattee 3 | 3 0 4 | nuttapong 5 | 2 0 6 | vishnu 7 | 1 0 8 | somchai 9 | 0 1 10 | 4 0 11 | nattee 12 | 3 0 13 | nuttapong 14 | 2 0 15 | vishnu 16 | 1 0 17 | somchai 18 | 0 1 19 | 4 0 20 | nattee 21 | 3 0 22 | nuttapong 23 | 2 0 24 | vishnu 25 | 1 0 26 | somchai 27 | 0 1 28 | 4 0 29 | nattee 30 | 3 0 31 | nuttapong 32 | 2 0 33 | vishnu 34 | 1 0 35 | somchai 36 | 0 1 37 | 4 0 38 | nattee 39 | 3 0 40 | nuttapong 41 | 2 0 42 | vishnu 43 | 1 0 44 | somchai 45 | 0 1 46 | 4 0 47 | nattee 48 | 3 0 49 | nuttapong 50 | 2 0 51 | vishnu 52 | 1 0 53 | somchai 54 | 0 1 55 | 4 0 56 | nattee 57 | 3 0 58 | nuttapong 59 | 2 0 60 | vishnu 61 | 1 0 62 | somchai 63 | 0 1 64 | 4 0 65 | nattee 66 | 3 0 67 | nuttapong 68 | 2 0 69 | vishnu 70 | 1 0 71 | somchai 72 | 0 1 73 | 4 0 74 | nattee 75 | 3 0 76 | nuttapong 77 | 2 0 78 | vishnu 79 | 1 0 80 | somchai 81 | 0 1 82 | 4 0 83 | nattee 84 | 3 0 85 | nuttapong 86 | 2 0 87 | vishnu 88 | 1 0 89 | somchai 90 | 0 1 91 | OK 92 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/3.in: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/3.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 | 0 1 3 | OK 4 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/4.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/4.sol: -------------------------------------------------------------------------------- 1 | 99 2000 2 | 98 1999 3 | 97 1998 4 | 96 1997 5 | 95 1996 6 | 94 1995 7 | 93 1994 8 | 92 1993 9 | 91 1992 10 | 90 1991 11 | OK 12 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/5.in: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/5.sol: -------------------------------------------------------------------------------- 1 | 2 2 | 1 3 | 2 0 4 | 0 1 5 | OK 6 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/6.in: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/6.sol: -------------------------------------------------------------------------------- 1 | 1 0 1 2 | 501 0 1 3 | 1 0 1 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/7.in: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/7.sol: -------------------------------------------------------------------------------- 1 | 1 0 2 | 5000001 01 3 | 1 0 4 | OK 5 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/8.in: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/8.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 0 1 2 3 4 5 6 7 8 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/9.in: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/data/9.sol: -------------------------------------------------------------------------------- 1 | 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 18 16 14 12 10 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 OK 2 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/package/sbv2.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/package/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_STACK_INCLUDED_ 2 | #define _CP_STACK_INCLUDED_ 3 | 4 | #include 5 | #include 6 | #include 7 | //#pragma once 8 | 9 | namespace CP { 10 | 11 | template 12 | class stack 13 | { 14 | protected: 15 | std::vector v; 16 | 17 | public: 18 | //-------------- constructor ---------- 19 | 20 | // copy constructor 21 | stack(const stack& a) { 22 | v = a.v; 23 | } 24 | 25 | // default constructor 26 | stack() { } 27 | 28 | ~stack() { } 29 | 30 | //------------- capacity function ------------------- 31 | bool empty() const { 32 | return v.empty(); 33 | } 34 | 35 | //------------ to be implemented by student ---------- 36 | size_t size() const; 37 | const T& top() const; 38 | void push(const T& element); 39 | void pop(); 40 | void deep_push(const T& element, int depth); 41 | void multi_push(const std::vector &w); 42 | void pop_until(const T &e); 43 | }; 44 | 45 | } 46 | 47 | #endif 48 | 49 | 50 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/package/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | #include "stack.h" 5 | #include 6 | 7 | template 8 | size_t CP::stack::size() const { 9 | //write your code here 10 | } 11 | 12 | template 13 | const T& CP::stack::top() const { 14 | //write your code here 15 | } 16 | 17 | template 18 | void CP::stack::push(const T& element) { 19 | //write your code here 20 | } 21 | 22 | template 23 | void CP::stack::pop() { 24 | //write your code here 25 | } 26 | 27 | template 28 | void CP::stack::deep_push(const T& element, int depth) { 29 | //write your code here 30 | } 31 | 32 | template 33 | void CP::stack::multi_push(const std::vector &w) { 34 | //write your code here 35 | } 36 | 37 | template 38 | void CP::stack::pop_until(const T& e) { 39 | //write your code here 40 | } 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/sbv.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv2/sbv.odt -------------------------------------------------------------------------------- /exercise/dq1_sbv2/sbv.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv2/sbv.pdf -------------------------------------------------------------------------------- /exercise/dq1_sbv2/solution/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_STACK_INCLUDED_ 2 | #define _CP_STACK_INCLUDED_ 3 | 4 | #include 5 | #include 6 | #include 7 | //#pragma once 8 | 9 | namespace CP { 10 | 11 | template 12 | class stack 13 | { 14 | protected: 15 | std::vector v; 16 | 17 | public: 18 | //-------------- constructor ---------- 19 | 20 | // copy constructor 21 | stack(const stack& a) { 22 | v = a.v; 23 | } 24 | 25 | // default constructor 26 | stack() { } 27 | 28 | ~stack() { } 29 | 30 | //------------- capacity function ------------------- 31 | bool empty() const { 32 | return v.empty(); 33 | } 34 | 35 | //------------ to be implemented by student ---------- 36 | size_t size() const; 37 | const T& top() const; 38 | void push(const T& element); 39 | void pop(); 40 | void deep_push(const T& element, int depth); 41 | void multi_push(const std::vector &w); 42 | void pop_until(const T &e); 43 | }; 44 | 45 | } 46 | 47 | #endif 48 | 49 | 50 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/solution/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | #include "stack.h" 5 | #include 6 | 7 | template 8 | size_t CP::stack::size() const { 9 | //write your code here 10 | return v.size(); 11 | } 12 | 13 | template 14 | const T& CP::stack::top() const { 15 | //write your code here 16 | return v.back(); 17 | } 18 | 19 | template 20 | void CP::stack::push(const T& element) { 21 | //write your code here 22 | v.push_back(element); 23 | } 24 | 25 | template 26 | void CP::stack::pop() { 27 | //write your code here 28 | v.pop_back(); 29 | } 30 | 31 | template 32 | void CP::stack::deep_push(const T& element, int depth) { 33 | //write your code here 34 | if (depth > v.size()) depth = v.size(); 35 | v.insert(v.end()-depth,element); 36 | } 37 | 38 | template 39 | void CP::stack::multi_push(const std::vector &w) { 40 | //write your code here 41 | for (auto &x : w) 42 | v.push_back(x); 43 | } 44 | 45 | template 46 | void CP::stack::pop_until(const T& e) { 47 | //write your code here 48 | while (!v.empty() && v.back() != e) 49 | v.pop_back(); 50 | } 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /exercise/dq1_sbv2/solution/test_stack: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sbv2/solution/test_stack -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/1.in: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/1.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/10.in: -------------------------------------------------------------------------------- 1 | 10 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/10.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/2.in: -------------------------------------------------------------------------------- 1 | 2 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/2.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/3.in: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/3.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/4.in: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/4.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/5.in: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/5.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/6.in: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/6.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/7.in: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/7.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/8.in: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/8.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/9.in: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/data/9.sol: -------------------------------------------------------------------------------- 1 | YES and YES nuttapong *@R&!) 2 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/package/sparse.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 44 | 45 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/package/sparsegraph.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPARSE_GRAPH_H__ 2 | #define __SPARSE_GRAPH_H__ 3 | 4 | // Your code here 5 | 6 | using namespace std; 7 | 8 | class SparseGraph{ 9 | public: 10 | SparseGraph() { 11 | // Your code here 12 | } 13 | 14 | SparseGraph(int n_in) { 15 | // Your code here 16 | } 17 | 18 | SparseGraph(const SparseGraph& G) { 19 | // Your code here 20 | 21 | } 22 | 23 | void AddEdge(int a, int b) { 24 | // Your code here 25 | } 26 | 27 | void RemoveEdge(int a, int b) { 28 | // Your code here 29 | } 30 | 31 | bool DoesEdgeExist(int a, int b) const { 32 | // Your code here 33 | } 34 | 35 | SparseGraph Transpose() const { 36 | // Your code here 37 | } 38 | 39 | protected: 40 | // Your code here 41 | }; 42 | #endif // __SPARSE_GRAPH_H__ 43 | 44 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sparse/solution/a.out -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/main_sparse: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sparse/solution/main_sparse -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/sparsegraph.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPARSE_GRAPH_H__ 2 | #define __SPARSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class SparseGraph{ 11 | public: 12 | SparseGraph() { 13 | // Your code here 14 | n = 3; 15 | g.resize(n); 16 | } 17 | 18 | SparseGraph(int n_in) { 19 | n = n_in; 20 | // Your code here 21 | g.resize(n); 22 | } 23 | 24 | SparseGraph(const SparseGraph& G) { 25 | // Your code here 26 | n = G.n; 27 | g.resize(n); 28 | for (size_t i = 0; i < n; i++) g[i] = G.g[i]; 29 | 30 | } 31 | 32 | SparseGraph& operator=(const SparseGraph& G) { 33 | // Your code here 34 | n = G.n; 35 | g.resize(n); 36 | for (size_t i = 0; i < n; i++) g[i] = G.g[i]; 37 | return *this; 38 | } 39 | 40 | 41 | 42 | void AddEdge(int a, int b) { 43 | // Your code here 44 | if (find(g[a].begin(),g[a].end(),b) == g[a].end()) { 45 | g[a].push_back(b); 46 | } 47 | } 48 | 49 | void RemoveEdge(int a, int b) { 50 | // Your code here 51 | vector::iterator it = find(g[a].begin(),g[a].end(),b); 52 | if (it != g[a].end()) { 53 | g[a].erase(it); 54 | } 55 | } 56 | 57 | bool DoesEdgeExist(int a, int b) const { 58 | // Your code here 59 | return (find(g[a].begin(),g[a].end(),b) != g[a].end()); 60 | } 61 | 62 | SparseGraph Transpose() const { 63 | SparseGraph GT(n); 64 | // Your code here 65 | for (int i = 0; i < n; i++) { 66 | for (int j = 0; j < g[i].size(); j++) { 67 | GT.g[g[i][j]].push_back(i); 68 | } 69 | } 70 | return GT; 71 | } 72 | protected: 73 | int n; 74 | // Your code here 75 | vector > g; 76 | }; 77 | #endif // __SPARSE_GRAPH_H__ 78 | 79 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/sparsegraph_map.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPARSE_GRAPH_H__ 2 | #define __SPARSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class SparseGraph{ 13 | public: 14 | SparseGraph() { 15 | // Your code here 16 | n = 3; 17 | } 18 | 19 | SparseGraph(int n_in) { 20 | n = n_in; 21 | // Your code here 22 | } 23 | 24 | SparseGraph(const SparseGraph& G) { 25 | // Your code here 26 | n = G.n; 27 | s = G.s; 28 | } 29 | 30 | SparseGraph& operator=(const SparseGraph& G) { 31 | // Your code here 32 | n = G.n; 33 | s = G.s; 34 | return *this; 35 | } 36 | 37 | 38 | 39 | void AddEdge(int a, int b) { 40 | // Your code here 41 | s.insert(make_pair(a,b)); 42 | } 43 | 44 | void RemoveEdge(int a, int b) { 45 | // Your code here 46 | s.erase(make_pair(a,b)); 47 | } 48 | 49 | bool DoesEdgeExist(int a, int b) const { 50 | // Your code here 51 | return (s.find(make_pair(a,b)) != s.end()); 52 | } 53 | 54 | SparseGraph Transpose() const { 55 | SparseGraph GT(n); 56 | // Your code here 57 | for (auto &e : s) { 58 | GT.AddEdge(e.second,e.first); 59 | } 60 | return GT; 61 | } 62 | protected: 63 | int n; 64 | // Your code here 65 | set > s; 66 | }; 67 | #endif // __SPARSE_GRAPH_H__ 68 | 69 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/sparsegraph_set.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPARSE_GRAPH_H__ 2 | #define __SPARSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class SparseGraph{ 12 | public: 13 | SparseGraph() { 14 | // Your code here 15 | n = 3; 16 | } 17 | 18 | SparseGraph(int n_in) { 19 | n = n_in; 20 | // Your code here 21 | } 22 | 23 | SparseGraph(const SparseGraph& G) { 24 | // Your code here 25 | n = G.n; 26 | s = G.s; 27 | } 28 | 29 | SparseGraph& operator=(const SparseGraph& G) { 30 | // Your code here 31 | n = G.n; 32 | s = G.s; 33 | return *this; 34 | } 35 | 36 | 37 | 38 | void AddEdge(int a, int b) { 39 | // Your code here 40 | s.insert(make_pair(a,b)); 41 | } 42 | 43 | void RemoveEdge(int a, int b) { 44 | // Your code here 45 | s.remove(make_pair(a,b)); 46 | } 47 | 48 | bool DoesEdgeExist(int a, int b) const { 49 | // Your code here 50 | return (set.find(make_pair(a,b) != s.end()); 51 | } 52 | 53 | SparseGraph Transpose() const { 54 | SparseGraph GT(n); 55 | // Your code here 56 | for (auto &e : s) { 57 | GT.AddEdge(e.second,e.first); 58 | } 59 | return GT; 60 | } 61 | protected: 62 | int n; 63 | // Your code here 64 | set > s; 65 | }; 66 | #endif // __SPARSE_GRAPH_H__ 67 | 68 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/solution/sparsegraph_vector.h: -------------------------------------------------------------------------------- 1 | #ifndef __SPARSE_GRAPH_H__ 2 | #define __SPARSE_GRAPH_H__ 3 | 4 | // Your code here 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class SparseGraph{ 11 | public: 12 | SparseGraph() { 13 | // Your code here 14 | n = 3; 15 | g.resize(n); 16 | } 17 | 18 | SparseGraph(int n_in) { 19 | n = n_in; 20 | // Your code here 21 | g.resize(n); 22 | } 23 | 24 | SparseGraph(const SparseGraph& G) { 25 | // Your code here 26 | n = G.n; 27 | g.resize(n); 28 | for (size_t i = 0; i < n; i++) g[i] = G.g[i]; 29 | 30 | } 31 | 32 | SparseGraph& operator=(const SparseGraph& G) { 33 | // Your code here 34 | n = G.n; 35 | g.resize(n); 36 | for (size_t i = 0; i < n; i++) g[i] = G.g[i]; 37 | return *this; 38 | } 39 | 40 | 41 | 42 | void AddEdge(int a, int b) { 43 | // Your code here 44 | if (find(g[a].begin(),g[a].end(),b) == g[a].end()) { 45 | g[a].push_back(b); 46 | } 47 | } 48 | 49 | void RemoveEdge(int a, int b) { 50 | // Your code here 51 | vector::iterator it = find(g[a].begin(),g[a].end(),b); 52 | if (it != g[a].end()) { 53 | g[a].erase(it); 54 | } 55 | } 56 | 57 | bool DoesEdgeExist(int a, int b) const { 58 | // Your code here 59 | return (find(g[a].begin(),g[a].end(),b) != g[a].end()); 60 | } 61 | 62 | SparseGraph Transpose() const { 63 | SparseGraph GT(n); 64 | // Your code here 65 | for (int i = 0; i < n; i++) { 66 | for (int j = 0; j < g[i].size(); j++) { 67 | GT.g[g[i][j]].push_back(i); 68 | } 69 | } 70 | return GT; 71 | } 72 | protected: 73 | int n; 74 | // Your code here 75 | vector > g; 76 | }; 77 | #endif // __SPARSE_GRAPH_H__ 78 | 79 | -------------------------------------------------------------------------------- /exercise/dq1_sparse/sparse.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sparse/sparse.odt -------------------------------------------------------------------------------- /exercise/dq1_sparse/sparse.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_sparse/sparse.pdf -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/1.in: -------------------------------------------------------------------------------- 1 | 10 10 2 | 82192 47849 37066 86764 9247 74421 30209 49373 65853 23455 3 | 99617 58517 99558 83203 45212 70130 29458 72748 28298 73772 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/1.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/10.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/11.in: -------------------------------------------------------------------------------- 1 | 3 3 2 | 1 2 3 3 | 3 2 1 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/11.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/12.in: -------------------------------------------------------------------------------- 1 | 3 2 2 | 1 2 3 3 | 1 2 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/12.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/13.in: -------------------------------------------------------------------------------- 1 | 3 3 2 | 10 20 30 3 | 10 20 30 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/13.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/14.in: -------------------------------------------------------------------------------- 1 | 3 2 2 | 9 9 9 3 | 9 9 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/14.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/15.in: -------------------------------------------------------------------------------- 1 | 1 1 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/15.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/16.in: -------------------------------------------------------------------------------- 1 | 2 2 2 | 5 5 3 | 5 5 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/16.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/17.in: -------------------------------------------------------------------------------- 1 | 0 0 2 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/17.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/18.in: -------------------------------------------------------------------------------- 1 | 0 1 2 | 10 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/18.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/2.in: -------------------------------------------------------------------------------- 1 | 100 99 2 | 16942 43444 54428 15271 87629 96240 82550 73249 86804 30995 91975 39886 98430 80624 19526 89826 64484 62583 39925 31095 14948 32229 54834 34032 23363 41373 42812 90643 83535 17298 88727 17631 42992 61525 33380 46617 36219 75663 49414 48142 1090 26081 36675 12005 76236 94023 99816 77069 183 3666 84975 81264 18063 58302 77849 87634 4197 67967 67856 71895 3704 7771 75612 80879 29825 96364 45319 62007 81850 32399 50622 97136 34496 831 35106 12321 31419 39298 46753 95900 52758 5266 1130 67961 54888 10006 53597 2883 44500 57258 70551 33487 69093 22819 27144 97666 2070 75226 19778 48973 3 | 16942 43444 54428 15271 87629 96240 82550 73249 86804 30995 91975 39886 98430 80624 19526 89826 64484 62583 39925 31095 14948 32229 54834 34032 23363 41373 42812 90643 83535 17298 88727 17631 42992 61525 33380 46617 36219 75663 49414 48142 1090 26081 36675 12005 76236 94023 99816 77069 183 3666 84975 81264 18063 58302 77849 87634 4197 67967 67856 71895 3704 7771 75612 80879 29825 96364 45319 62007 81850 32399 50622 97136 34496 831 35106 12321 31419 39298 46753 95900 52758 5266 1130 67961 54888 10006 53597 2883 44500 57258 70551 33487 69093 22819 27144 97666 2070 75226 19778 48973 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/2.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/3.in: -------------------------------------------------------------------------------- 1 | 10 500 2 | 70956 82852 34355 80505 37562 36310 20372 49583 51 94905 3 | 70956 82852 34355 80505 37562 36310 20372 49583 51 94905 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/3.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/4.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/5.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/6.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/7.in: -------------------------------------------------------------------------------- 1 | 1 10000 2 | 97826 3 | 97826 4 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/7.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/8.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/9.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename, n,m = n,equal = true) 2 | File.open(filename,"w") do |f| 3 | a = n.times.map{ rand(100000) } 4 | if equal 5 | b = a 6 | else 7 | b = m.times.map{ rand(100000) } 8 | end 9 | f.puts "#{n} #{m}" 10 | f.puts a.join ' ' 11 | f.puts b.join ' ' 12 | end 13 | end 14 | 15 | 16 | genOneFile("1.in",10,10,false) 17 | genOneFile("2.in",100,99) 18 | genOneFile("3.in",10,500) 19 | genOneFile("4.in",500) 20 | genOneFile("5.in",500) 21 | genOneFile("6.in",500) 22 | genOneFile("7.in",1,10000) 23 | genOneFile("8.in",20000,0) 24 | genOneFile("9.in",50000,50000,false) 25 | genOneFile("10.in",50000) 26 | 27 | File.write("11.in",< 2 | #include 3 | #include "vector.h" 4 | #include "student.h" 5 | 6 | 7 | bool equal(CP::vector &a,CP::vector &b) { 8 | if (a.size() != b.size()) return false; 9 | for (size_t i = 0;i < a.size();i++) { 10 | if (a[i] != b[i]) return false; 11 | } 12 | return true; 13 | } 14 | 15 | int main() { 16 | int n,m; 17 | std::cin >> n >> m; 18 | 19 | //read input 20 | CP::vector a,b; 21 | while (n--) { 22 | int tmp; 23 | std::cin >> tmp; 24 | a.push_back(tmp); 25 | } 26 | while (m--) { 27 | int tmp; 28 | std::cin >> tmp; 29 | b.push_back(tmp); 30 | } 31 | 32 | //check 33 | std::cout << (a == b) << std::endl; 34 | std::cout << (b == a) << std::endl; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/package/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | template 5 | bool CP::vector::operator==(const vector &other) const { 6 | //write your code only in this function 7 | 8 | } 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/package/vequal.cbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 45 | 46 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/solution/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_vector_equal/solution/main -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/solution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "vector.h" 4 | #include "student.h" 5 | 6 | 7 | bool equal(CP::vector &a,CP::vector &b) { 8 | if (a.size() != b.size()) return false; 9 | for (size_t i = 0;i < a.size();i++) { 10 | if (a[i] != b[i]) return false; 11 | } 12 | return true; 13 | } 14 | 15 | int main() { 16 | int n,m; 17 | std::cin >> n >> m; 18 | 19 | //read input 20 | CP::vector a,b; 21 | while (n--) { 22 | int tmp; 23 | std::cin >> tmp; 24 | a.push_back(tmp); 25 | } 26 | while (m--) { 27 | int tmp; 28 | std::cin >> tmp; 29 | b.push_back(tmp); 30 | } 31 | 32 | //check 33 | std::cout << (a == b) << std::endl; 34 | std::cout << (b == a) << std::endl; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/solution/student.h: -------------------------------------------------------------------------------- 1 | #ifndef __STUDENT_H_ 2 | #define __STUDENT_H_ 3 | 4 | template 5 | bool CP::vector::operator==(const vector &other) const { 6 | if (other.size() != size()) return false; 7 | for (size_t i = 0;i < size();i++) { 8 | if (mData[i] != other[i]) return false; 9 | } 10 | return true; 11 | } 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/vequal.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_vector_equal/vequal.odt -------------------------------------------------------------------------------- /exercise/dq1_vector_equal/vequal.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/dq1_vector_equal/vequal.pdf -------------------------------------------------------------------------------- /exercise/ds00_reverse/.main.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse/.main.cpp.swp -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/1.in: -------------------------------------------------------------------------------- 1 | 10 2 | 5 0 2 3 9 4 1 7 6 8 3 | 6 7 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/1.sol: -------------------------------------------------------------------------------- 1 | 5 0 2 3 9 4 7 1 6 8 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/10.in: -------------------------------------------------------------------------------- 1 | 100 2 | 1 28 72 92 98 64 20 80 63 44 78 7 61 91 88 82 87 74 26 73 96 86 15 51 75 83 8 12 70 93 24 95 46 81 79 31 59 11 89 76 21 62 37 35 2 36 32 0 67 43 99 94 38 58 50 40 41 85 27 22 16 56 34 18 42 4 55 3 69 5 48 84 97 71 10 49 14 47 6 39 53 68 30 29 90 45 52 66 54 23 57 19 33 13 65 9 17 77 25 60 3 | 29 44 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/10.sol: -------------------------------------------------------------------------------- 1 | 1 28 72 92 98 64 20 80 63 44 78 7 61 91 88 82 87 74 26 73 96 86 15 51 75 83 8 12 70 2 35 37 62 21 76 89 11 59 31 79 81 46 95 24 93 36 32 0 67 43 99 94 38 58 50 40 41 85 27 22 16 56 34 18 42 4 55 3 69 5 48 84 97 71 10 49 14 47 6 39 53 68 30 29 90 45 52 66 54 23 57 19 33 13 65 9 17 77 25 60 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/2.in: -------------------------------------------------------------------------------- 1 | 100 2 | 81 65 98 45 56 10 49 97 27 80 19 67 62 34 29 83 60 28 70 59 51 68 7 52 64 95 39 42 63 85 33 6 11 46 36 99 91 87 88 50 57 40 96 31 13 44 32 15 37 8 30 72 71 0 76 26 2 84 18 12 78 74 90 82 92 69 48 20 1 94 93 4 21 22 16 66 73 89 75 77 9 43 55 53 24 58 47 41 23 17 3 79 5 25 35 38 61 86 14 54 3 | 10 10 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/2.sol: -------------------------------------------------------------------------------- 1 | 81 65 98 45 56 10 49 97 27 80 19 67 62 34 29 83 60 28 70 59 51 68 7 52 64 95 39 42 63 85 33 6 11 46 36 99 91 87 88 50 57 40 96 31 13 44 32 15 37 8 30 72 71 0 76 26 2 84 18 12 78 74 90 82 92 69 48 20 1 94 93 4 21 22 16 66 73 89 75 77 9 43 55 53 24 58 47 41 23 17 3 79 5 25 35 38 61 86 14 54 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/3.in: -------------------------------------------------------------------------------- 1 | 100 2 | 18 30 62 70 97 39 71 3 66 92 2 20 88 21 0 84 41 57 65 38 82 61 96 52 5 35 4 55 89 86 63 59 83 47 16 27 8 45 58 42 93 12 67 76 28 31 56 94 75 29 72 46 79 37 99 22 34 91 17 11 43 10 14 24 98 33 48 26 68 25 32 44 60 1 15 6 80 51 50 13 19 77 9 81 78 36 23 85 7 53 69 74 73 54 64 40 49 95 87 90 3 | 40 41 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/3.sol: -------------------------------------------------------------------------------- 1 | 18 30 62 70 97 39 71 3 66 92 2 20 88 21 0 84 41 57 65 38 82 61 96 52 5 35 4 55 89 86 63 59 83 47 16 27 8 45 58 42 12 93 67 76 28 31 56 94 75 29 72 46 79 37 99 22 34 91 17 11 43 10 14 24 98 33 48 26 68 25 32 44 60 1 15 6 80 51 50 13 19 77 9 81 78 36 23 85 7 53 69 74 73 54 64 40 49 95 87 90 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/4.in: -------------------------------------------------------------------------------- 1 | 100 2 | 85 94 49 4 3 16 35 51 89 33 64 18 73 11 22 97 48 77 25 23 70 32 75 62 67 45 46 44 31 83 84 63 50 40 43 95 71 57 2 60 91 93 69 15 1 66 8 6 24 56 0 7 20 36 42 19 12 52 30 80 76 54 37 38 96 41 86 27 39 26 5 17 29 98 79 74 81 99 14 53 61 9 13 65 34 87 68 47 28 90 10 59 72 55 88 21 78 82 92 58 3 | 50 52 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/4.sol: -------------------------------------------------------------------------------- 1 | 85 94 49 4 3 16 35 51 89 33 64 18 73 11 22 97 48 77 25 23 70 32 75 62 67 45 46 44 31 83 84 63 50 40 43 95 71 57 2 60 91 93 69 15 1 66 8 6 24 56 20 7 0 36 42 19 12 52 30 80 76 54 37 38 96 41 86 27 39 26 5 17 29 98 79 74 81 99 14 53 61 9 13 65 34 87 68 47 28 90 10 59 72 55 88 21 78 82 92 58 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/5.in: -------------------------------------------------------------------------------- 1 | 100 2 | 21 68 43 66 88 57 86 79 19 53 76 20 41 95 7 81 60 72 74 16 5 8 45 36 11 4 27 70 3 37 87 22 15 0 75 98 97 61 46 42 23 14 65 48 90 18 78 28 84 58 80 54 91 44 63 47 1 24 93 13 26 39 96 71 17 40 56 30 99 64 50 25 59 94 69 29 6 32 10 51 55 73 34 35 31 77 12 38 33 62 49 67 52 85 89 83 92 2 82 9 3 | 0 94 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/5.sol: -------------------------------------------------------------------------------- 1 | 89 85 52 67 49 62 33 38 12 77 31 35 34 73 55 51 10 32 6 29 69 94 59 25 50 64 99 30 56 40 17 71 96 39 26 13 93 24 1 47 63 44 91 54 80 58 84 28 78 18 90 48 65 14 23 42 46 61 97 98 75 0 15 22 87 37 3 70 27 4 11 36 45 8 5 16 74 72 60 81 7 95 41 20 76 53 19 79 86 57 88 66 43 68 21 83 92 2 82 9 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/6.in: -------------------------------------------------------------------------------- 1 | 100 2 | 15 1 95 83 22 7 2 79 99 51 61 50 36 49 33 56 70 64 98 27 20 67 73 84 76 17 8 47 3 57 32 48 13 46 4 72 14 89 85 74 86 18 93 42 69 9 0 81 92 21 40 53 55 11 96 71 25 24 80 41 10 45 63 44 62 87 65 91 59 77 35 82 66 29 34 60 28 52 16 30 43 19 26 23 31 68 58 6 37 88 54 75 78 94 97 39 5 38 12 90 3 | 46 87 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/6.sol: -------------------------------------------------------------------------------- 1 | 15 1 95 83 22 7 2 79 99 51 61 50 36 49 33 56 70 64 98 27 20 67 73 84 76 17 8 47 3 57 32 48 13 46 4 72 14 89 85 74 86 18 93 42 69 9 6 58 68 31 23 26 19 43 30 16 52 28 60 34 29 66 82 35 77 59 91 65 87 62 44 63 45 10 41 80 24 25 71 96 11 55 53 40 21 92 81 0 37 88 54 75 78 94 97 39 5 38 12 90 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/7.in: -------------------------------------------------------------------------------- 1 | 100 2 | 9 32 43 37 68 49 0 74 63 64 71 38 56 72 47 6 11 53 2 42 14 31 69 89 87 33 81 8 50 30 16 95 78 5 13 92 61 17 60 84 85 24 22 66 40 34 10 27 62 46 59 51 44 26 65 41 57 45 35 15 88 79 67 90 52 19 99 12 4 77 76 93 18 96 86 7 91 29 20 55 58 98 1 75 23 83 97 80 25 36 70 21 39 54 73 3 82 28 48 94 3 | 52 77 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/7.sol: -------------------------------------------------------------------------------- 1 | 9 32 43 37 68 49 0 74 63 64 71 38 56 72 47 6 11 53 2 42 14 31 69 89 87 33 81 8 50 30 16 95 78 5 13 92 61 17 60 84 85 24 22 66 40 34 10 27 62 46 59 51 29 91 7 86 96 18 93 76 77 4 12 99 19 52 90 67 79 88 15 35 45 57 41 65 26 44 20 55 58 98 1 75 23 83 97 80 25 36 70 21 39 54 73 3 82 28 48 94 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/8.in: -------------------------------------------------------------------------------- 1 | 100 2 | 71 28 98 52 51 53 92 24 36 18 20 70 66 27 56 60 63 97 40 82 4 49 39 88 15 8 13 26 3 45 32 0 73 31 67 2 91 94 64 25 42 5 41 85 33 75 47 23 17 81 35 61 90 84 6 7 58 69 30 43 62 19 29 59 96 11 65 89 48 83 86 93 46 55 79 78 16 72 34 87 77 99 80 38 76 22 9 68 14 50 74 12 1 21 37 57 54 44 10 95 3 | 7 19 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/8.sol: -------------------------------------------------------------------------------- 1 | 71 28 98 52 51 53 92 82 40 97 63 60 56 27 66 70 20 18 36 24 4 49 39 88 15 8 13 26 3 45 32 0 73 31 67 2 91 94 64 25 42 5 41 85 33 75 47 23 17 81 35 61 90 84 6 7 58 69 30 43 62 19 29 59 96 11 65 89 48 83 86 93 46 55 79 78 16 72 34 87 77 99 80 38 76 22 9 68 14 50 74 12 1 21 37 57 54 44 10 95 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/9.in: -------------------------------------------------------------------------------- 1 | 100 2 | 6 94 75 28 37 15 72 20 47 45 87 61 19 12 60 26 41 59 34 88 65 57 83 1 73 51 9 13 4 95 17 2 16 49 44 11 98 3 97 30 62 66 56 89 76 63 8 31 86 78 43 42 38 92 21 90 77 33 68 52 55 29 70 10 5 64 67 53 32 14 80 25 23 79 99 39 74 54 96 24 85 0 35 48 91 84 58 69 93 81 27 82 40 71 46 22 7 36 18 50 3 | 26 27 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/9.sol: -------------------------------------------------------------------------------- 1 | 6 94 75 28 37 15 72 20 47 45 87 61 19 12 60 26 41 59 34 88 65 57 83 1 73 51 13 9 4 95 17 2 16 49 44 11 98 3 97 30 62 66 56 89 76 63 8 31 86 78 43 42 38 92 21 90 77 33 68 52 55 29 70 10 5 64 67 53 32 14 80 25 23 79 99 39 74 54 96 24 85 0 35 48 91 84 58 69 93 81 27 82 40 71 46 22 7 36 18 50 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename,n,a = rand(n), b = rand(n)) 2 | a,b = b,a if a > b 3 | File.open(filename,'w') do |f| 4 | f.puts n 5 | f.puts n.times.to_a.shuffle.join ' ' 6 | f.puts "#{a} #{b}" 7 | end 8 | end 9 | 10 | genOneFile('1.in',10) 11 | genOneFile('2.in',100,10,10) 12 | genOneFile('3.in',100,40,41) 13 | genOneFile('4.in',100,50,52) 14 | genOneFile('5.in',100) 15 | genOneFile('6.in',100) 16 | genOneFile('7.in',100) 17 | genOneFile('8.in',100) 18 | genOneFile('9.in',100) 19 | genOneFile('10.in',100) 20 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void reverse(vector &v,int a,int b) { 7 | //write your code only in this function 8 | } 9 | 10 | int main() { 11 | //read input 12 | int n,a,b; 13 | cin >> n; 14 | vector v; 15 | for (int i = 0;i < n;i++) { 16 | int c; 17 | cin >> c; 18 | v.push_back(c); 19 | } 20 | cin >> a >> b; 21 | 22 | //call function 23 | reverse(v,a,b); 24 | 25 | //display content of the vector 26 | for (auto &x : v) 27 | cout << x << " "; 28 | cout << endl; 29 | } 30 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/reverse.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse/reverse.odt -------------------------------------------------------------------------------- /exercise/ds00_reverse/reverse.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse/reverse.pdf -------------------------------------------------------------------------------- /exercise/ds00_reverse/solution/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse/solution/main -------------------------------------------------------------------------------- /exercise/ds00_reverse/solution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void reverse(vector &v,int a,int b) { 7 | //write your code only in this function 8 | int tmp; 9 | for (int i = 0;i < (b-a+1)/2;i++) { 10 | tmp = v[a+i]; 11 | v[a+i] = v[b-i]; 12 | v[b-i] = tmp; 13 | } 14 | } 15 | 16 | int main() { 17 | //read input 18 | int n,a,b; 19 | cin >> n; 20 | vector v; 21 | for (int i = 0;i < n;i++) { 22 | int c; 23 | cin >> c; 24 | v.push_back(c); 25 | } 26 | cin >> a >> b; 27 | 28 | //call function 29 | reverse(v,a,b); 30 | 31 | //display content of the vector 32 | for (auto &x : v) { 33 | cout << x << " "; 34 | } 35 | cout << endl; 36 | } 37 | -------------------------------------------------------------------------------- /exercise/ds00_reverse/solution/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse/solution/main.o -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/1.in: -------------------------------------------------------------------------------- 1 | 10 2 | 5 0 8 9 3 6 7 1 2 4 3 | 9 9 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/1.sol: -------------------------------------------------------------------------------- 1 | 5 0 8 9 3 6 7 1 2 4 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/10.in: -------------------------------------------------------------------------------- 1 | 100 2 | 3 67 62 15 34 13 44 81 22 97 95 99 12 90 89 74 93 54 47 56 79 41 31 20 6 51 84 59 77 87 43 35 45 50 36 98 37 9 4 29 2 21 78 46 23 0 60 16 63 83 61 27 76 96 57 73 18 66 64 42 7 69 1 24 32 88 33 68 25 55 5 91 58 19 92 80 11 86 48 94 52 71 28 53 26 65 85 40 39 17 38 10 30 14 75 72 82 49 70 8 3 | 48 49 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/10.sol: -------------------------------------------------------------------------------- 1 | 3 67 62 15 34 13 44 81 22 97 95 99 12 90 89 74 93 54 47 56 79 41 31 20 6 51 84 59 77 87 43 35 45 50 36 98 37 9 4 29 2 21 78 46 23 0 60 16 83 63 61 27 76 96 57 73 18 66 64 42 7 69 1 24 32 88 33 68 25 55 5 91 58 19 92 80 11 86 48 94 52 71 28 53 26 65 85 40 39 17 38 10 30 14 75 72 82 49 70 8 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/2.in: -------------------------------------------------------------------------------- 1 | 100 2 | 17 75 14 99 77 63 36 0 79 11 56 35 82 67 68 2 44 98 55 90 61 21 26 52 49 88 34 83 73 65 31 5 60 3 84 59 23 46 51 78 86 25 91 12 19 54 45 92 57 40 8 48 38 69 42 29 71 74 87 95 50 94 16 85 66 53 58 33 9 41 24 7 13 81 28 37 6 97 89 39 93 62 80 43 18 27 22 1 47 4 76 30 64 20 70 10 96 15 32 72 3 | 10 10 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/2.sol: -------------------------------------------------------------------------------- 1 | 17 75 14 99 77 63 36 0 79 11 56 35 82 67 68 2 44 98 55 90 61 21 26 52 49 88 34 83 73 65 31 5 60 3 84 59 23 46 51 78 86 25 91 12 19 54 45 92 57 40 8 48 38 69 42 29 71 74 87 95 50 94 16 85 66 53 58 33 9 41 24 7 13 81 28 37 6 97 89 39 93 62 80 43 18 27 22 1 47 4 76 30 64 20 70 10 96 15 32 72 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/3.in: -------------------------------------------------------------------------------- 1 | 100 2 | 84 83 42 65 97 11 56 44 75 16 81 67 87 4 72 82 15 24 54 79 61 92 64 94 70 29 0 13 33 51 45 50 1 88 8 5 7 68 49 74 6 2 20 59 27 43 41 96 73 3 38 63 25 57 60 31 55 86 22 53 40 89 14 77 69 52 19 9 37 28 23 78 85 32 12 18 71 21 90 66 58 80 17 39 47 34 30 46 62 99 26 98 93 95 48 35 10 36 91 76 3 | 40 41 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/3.sol: -------------------------------------------------------------------------------- 1 | 84 83 42 65 97 11 56 44 75 16 81 67 87 4 72 82 15 24 54 79 61 92 64 94 70 29 0 13 33 51 45 50 1 88 8 5 7 68 49 74 2 6 20 59 27 43 41 96 73 3 38 63 25 57 60 31 55 86 22 53 40 89 14 77 69 52 19 9 37 28 23 78 85 32 12 18 71 21 90 66 58 80 17 39 47 34 30 46 62 99 26 98 93 95 48 35 10 36 91 76 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/4.in: -------------------------------------------------------------------------------- 1 | 100 2 | 71 38 80 19 55 70 15 32 72 11 91 29 60 87 45 74 16 97 86 12 34 67 13 6 4 81 79 17 63 47 35 52 7 61 88 31 64 92 66 73 84 41 28 76 54 83 24 3 43 46 36 95 21 93 37 44 23 14 58 59 18 77 82 50 89 9 22 26 42 5 53 56 25 40 48 78 30 69 94 75 51 2 10 20 90 49 33 68 0 85 27 98 62 39 8 96 1 65 57 99 3 | 50 52 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/4.sol: -------------------------------------------------------------------------------- 1 | 71 38 80 19 55 70 15 32 72 11 91 29 60 87 45 74 16 97 86 12 34 67 13 6 4 81 79 17 63 47 35 52 7 61 88 31 64 92 66 73 84 41 28 76 54 83 24 3 43 46 21 95 36 93 37 44 23 14 58 59 18 77 82 50 89 9 22 26 42 5 53 56 25 40 48 78 30 69 94 75 51 2 10 20 90 49 33 68 0 85 27 98 62 39 8 96 1 65 57 99 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/5.in: -------------------------------------------------------------------------------- 1 | 100 2 | 86 88 55 15 90 69 77 2 43 5 11 66 20 79 37 83 33 82 62 1 50 27 14 12 95 63 65 6 58 7 75 34 64 80 93 84 85 60 49 48 98 47 0 31 57 16 26 45 73 53 4 29 21 52 38 23 24 89 94 54 40 42 9 28 96 19 68 87 17 91 51 56 78 18 22 46 70 74 41 97 25 35 44 92 10 71 3 30 8 13 59 99 72 76 32 39 36 67 81 61 3 | 24 87 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/5.sol: -------------------------------------------------------------------------------- 1 | 86 88 55 15 90 69 77 2 43 5 11 66 20 79 37 83 33 82 62 1 50 27 14 12 30 3 71 10 92 44 35 25 97 41 74 70 46 22 18 78 56 51 91 17 87 68 19 96 28 9 42 40 54 94 89 24 23 38 52 21 29 4 53 73 45 26 16 57 31 0 47 98 48 49 60 85 84 93 80 64 34 75 7 58 6 65 63 95 8 13 59 99 72 76 32 39 36 67 81 61 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/6.in: -------------------------------------------------------------------------------- 1 | 100 2 | 92 38 63 59 1 33 89 0 94 12 7 64 53 78 98 62 54 80 48 28 87 36 68 66 34 20 83 76 22 14 21 39 85 90 86 43 42 82 50 3 32 46 25 69 26 27 99 45 30 11 91 74 4 10 60 47 79 93 17 2 49 77 15 95 35 58 16 44 37 65 19 40 13 56 96 57 55 6 24 61 5 81 52 97 31 70 8 84 18 75 51 71 72 29 41 23 67 9 73 88 3 | 17 59 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/6.sol: -------------------------------------------------------------------------------- 1 | 92 38 63 59 1 33 89 0 94 12 7 64 53 78 98 62 54 2 17 93 79 47 60 10 4 74 91 11 30 45 99 27 26 69 25 46 32 3 50 82 42 43 86 90 85 39 21 14 22 76 83 20 34 66 68 36 87 28 48 80 49 77 15 95 35 58 16 44 37 65 19 40 13 56 96 57 55 6 24 61 5 81 52 97 31 70 8 84 18 75 51 71 72 29 41 23 67 9 73 88 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/7.in: -------------------------------------------------------------------------------- 1 | 100 2 | 57 45 56 44 7 28 78 29 27 22 54 80 73 12 9 71 87 42 95 92 93 77 97 10 81 46 0 43 98 99 24 62 50 90 55 20 38 65 31 14 88 21 37 53 2 47 4 15 91 30 32 68 17 75 96 26 49 89 74 52 85 84 64 48 59 16 70 23 51 79 18 58 41 76 5 63 13 35 94 8 36 19 83 86 72 82 61 40 34 25 66 11 39 67 33 3 1 6 60 69 3 | 16 73 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/7.sol: -------------------------------------------------------------------------------- 1 | 57 45 56 44 7 28 78 29 27 22 54 80 73 12 9 71 76 41 58 18 79 51 23 70 16 59 48 64 84 85 52 74 89 49 26 96 75 17 68 32 30 91 15 4 47 2 53 37 21 88 14 31 65 38 20 55 90 50 62 24 99 98 43 0 46 81 10 97 77 93 92 95 42 87 5 63 13 35 94 8 36 19 83 86 72 82 61 40 34 25 66 11 39 67 33 3 1 6 60 69 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/8.in: -------------------------------------------------------------------------------- 1 | 100 2 | 22 2 17 70 7 86 24 54 63 49 92 80 37 95 68 29 11 23 56 61 20 48 81 84 78 99 77 33 88 59 75 73 96 82 89 35 60 16 8 67 83 40 91 28 47 15 57 10 46 44 6 13 26 52 97 0 27 85 93 34 1 9 18 71 90 3 38 39 94 4 98 25 72 36 21 32 53 66 65 14 79 55 5 62 30 41 64 69 31 12 87 51 19 45 50 42 58 76 74 43 3 | 8 43 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/8.sol: -------------------------------------------------------------------------------- 1 | 22 2 17 70 7 86 24 54 28 91 40 83 67 8 16 60 35 89 82 96 73 75 59 88 33 77 99 78 84 81 48 20 61 56 23 11 29 68 95 37 80 92 49 63 47 15 57 10 46 44 6 13 26 52 97 0 27 85 93 34 1 9 18 71 90 3 38 39 94 4 98 25 72 36 21 32 53 66 65 14 79 55 5 62 30 41 64 69 31 12 87 51 19 45 50 42 58 76 74 43 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/9.in: -------------------------------------------------------------------------------- 1 | 100 2 | 19 49 73 26 59 63 60 20 88 64 44 58 27 45 99 70 93 37 2 96 42 84 91 1 29 98 24 16 11 79 13 21 41 36 55 81 75 46 77 15 12 9 38 8 14 76 6 54 22 94 50 43 74 90 66 65 52 3 17 61 25 51 72 4 89 86 57 95 5 78 85 67 83 0 7 47 48 34 33 30 62 10 28 69 40 53 97 18 35 39 92 56 68 71 23 31 80 32 87 82 3 | 67 92 4 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/9.sol: -------------------------------------------------------------------------------- 1 | 19 49 73 26 59 63 60 20 88 64 44 58 27 45 99 70 93 37 2 96 42 84 91 1 29 98 24 16 11 79 13 21 41 36 55 81 75 46 77 15 12 9 38 8 14 76 6 54 22 94 50 43 74 90 66 65 52 3 17 61 25 51 72 4 89 86 57 68 56 92 39 35 18 97 53 40 69 28 10 62 30 33 34 48 47 7 0 83 67 85 78 5 95 71 23 31 80 32 87 82 2 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename,n,a = rand(n), b = rand(n)) 2 | a,b = b,a if a > b 3 | File.open(filename,'w') do |f| 4 | f.puts n 5 | f.puts n.times.to_a.shuffle.join ' ' 6 | f.puts "#{a} #{b}" 7 | end 8 | end 9 | 10 | genOneFile('1.in',10) 11 | genOneFile('2.in',100,10,10) 12 | genOneFile('3.in',100,40,41) 13 | genOneFile('4.in',100,50,52) 14 | genOneFile('5.in',100) 15 | genOneFile('6.in',100) 16 | genOneFile('7.in',100) 17 | genOneFile('8.in',100) 18 | genOneFile('9.in',100) 19 | genOneFile('10.in',100) 20 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void reverse(vector &v,vector::iterator a,vector::iterator b) { 7 | //write your code only in this function 8 | } 9 | 10 | int main() { 11 | //read input 12 | int n,a,b; 13 | cin >> n; 14 | vector v; 15 | for (int i = 0;i < n;i++) { 16 | int c; 17 | cin >> c; 18 | v.push_back(c); 19 | } 20 | cin >> a >> b; 21 | 22 | //call function 23 | reverse(v,v.begin()+a,v.begin()+b+1); 24 | 25 | //display content of the vector 26 | for (auto &x : v) { 27 | cout << x << " "; 28 | } 29 | cout << endl; 30 | } 31 | -------------------------------------------------------------------------------- /exercise/ds00_reverse2/reverse2.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse2/reverse2.odt -------------------------------------------------------------------------------- /exercise/ds00_reverse2/reverse2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse2/reverse2.pdf -------------------------------------------------------------------------------- /exercise/ds00_reverse2/solution/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds00_reverse2/solution/main -------------------------------------------------------------------------------- /exercise/ds00_reverse2/solution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | void reverse(vector &v,vector::iterator a,vector::iterator b) { 7 | //write your code only in this function 8 | vector::iterator it1,it2; 9 | it1 = a; 10 | it2 = b-1; 11 | while (it1 != it2) { 12 | int tmp; 13 | tmp = *it1; 14 | *it1 = *it2; 15 | *it2 = tmp; 16 | it1++; 17 | if (it1 == it2) break; 18 | it2--; 19 | } 20 | } 21 | 22 | int main() { 23 | //read input 24 | int n,a,b; 25 | cin >> n; 26 | vector v; 27 | for (int i = 0;i < n;i++) { 28 | int c; 29 | cin >> c; 30 | v.push_back(c); 31 | } 32 | cin >> a >> b; 33 | 34 | //call function 35 | reverse(v,v.begin()+a,v.begin()+b+1); 36 | 37 | //display content of the vector 38 | for (auto &x : v) { 39 | cout << x << " "; 40 | } 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/1.in: -------------------------------------------------------------------------------- 1 | 10 2 | Astir 3 | clock 4 | Subtilize 5 | moody- 6 | chrysalis 7 | unbutton 8 | vaults 9 | being 10 | Caesarian 11 | invests 12 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/1.sol: -------------------------------------------------------------------------------- 1 | vaults 1 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/10.sol: -------------------------------------------------------------------------------- 1 | powers 11 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/2.in: -------------------------------------------------------------------------------- 1 | 100 2 | diadem 3 | substantive 4 | animate 5 | Erskine 6 | accompany 7 | cetacea 8 | mast-head 9 | drain 10 | spiralizes 11 | her 12 | reg 13 | uneven 14 | strain 15 | mat 16 | sailed 17 | wheels 18 | needles 19 | replied 20 | sullenly 21 | scolding 22 | dearly 23 | dictating 24 | Supreme 25 | congratulate 26 | inferences 27 | inquiries 28 | willains 29 | Wonder 30 | extended 31 | nearing 32 | onward 33 | pictorial 34 | historian 35 | reasonably 36 | shaft 37 | carnivorous 38 | blood-muddled 39 | biased 40 | inlayings 41 | rule 42 | triangular 43 | experiences 44 | leading 45 | subtly 46 | whale-lance 47 | male 48 | assured 49 | all-receptive 50 | meditativeness 51 | conceded 52 | ravines 53 | snow-hill 54 | public 55 | drat 56 | requisite 57 | sober 58 | Specksioneer 59 | strewn 60 | avast 61 | favoured 62 | risks 63 | world-circle 64 | exhausting 65 | complaints- 66 | metallic-like 67 | arrested 68 | skies 69 | rocked 70 | grooves 71 | older 72 | excepting 73 | breathest 74 | cherrying 75 | craft 76 | slain 77 | independence 78 | Humiliation 79 | Scorpio 80 | pinny 81 | skirting 82 | hootings 83 | weight 84 | woollen 85 | panic-stricken 86 | reydan-siskur 87 | antiquities 88 | sled-like 89 | reg 90 | loitering 91 | good 92 | hull 93 | hastily 94 | bounds 95 | understandings 96 | staves 97 | Upon 98 | armies 99 | delectable 100 | to 101 | foam-glued 102 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/2.sol: -------------------------------------------------------------------------------- 1 | reg 2 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/3.sol: -------------------------------------------------------------------------------- 1 | vapory 2 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/4.sol: -------------------------------------------------------------------------------- 1 | bundle 3 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/5.sol: -------------------------------------------------------------------------------- 1 | untouchable 3 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/6.sol: -------------------------------------------------------------------------------- 1 | comparison 4 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/7.sol: -------------------------------------------------------------------------------- 1 | hauling 5 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/8.sol: -------------------------------------------------------------------------------- 1 | vortices 7 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/9.sol: -------------------------------------------------------------------------------- 1 | answered- 8 2 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/data/gen.rb: -------------------------------------------------------------------------------- 1 | $a=File.read("mobydick.txt").split(/[ \n\t'",.?!:;\[\]\(\)]/).uniq 2 | 3 | 4 | def genOneFile(filename, n) 5 | File.open(filename,"w") do |f| 6 | f.puts n 7 | n.times do 8 | loop do 9 | st = $a[rand($a.size())] 10 | unless st == '' 11 | f.puts st 12 | break 13 | end 14 | end 15 | end 16 | end 17 | end 18 | 19 | 20 | genOneFile("1.in",10) 21 | genOneFile("2.in",100) 22 | genOneFile("3.in",500) 23 | genOneFile("4.in",1000) 24 | genOneFile("5.in",3000) 25 | genOneFile("6.in",5000) 26 | genOneFile("7.in",10000) 27 | genOneFile("8.in",20000) 28 | genOneFile("9.in",30000) 29 | genOneFile("10.in",50000) 30 | -------------------------------------------------------------------------------- /exercise/ds01_most_freq/most.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_most_freq/most.odt -------------------------------------------------------------------------------- /exercise/ds01_most_freq/most.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_most_freq/most.pdf -------------------------------------------------------------------------------- /exercise/ds01_most_freq/solution/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_most_freq/solution/main -------------------------------------------------------------------------------- /exercise/ds01_most_freq/solution/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int N,max; 9 | string st; 10 | cin >> N; 11 | 12 | map m; 13 | while (N--) { 14 | cin >> st; 15 | m[st]++; 16 | 17 | if (m[st] > max) max = m[st]; 18 | } 19 | 20 | map::iterator it = m.end(); 21 | while (it != m.begin()) { 22 | it--; 23 | if (it->second == max) { 24 | cout << it->first << " " << it->second << endl; 25 | break; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/data/1.in: -------------------------------------------------------------------------------- 1 | 3 5 2 | 8 10 10 3 | 7 9 1 5 6 4 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/data/1.sol: -------------------------------------------------------------------------------- 1 | NO 2 | NO 3 | NO 4 | NO 5 | NO 6 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/data/2.in: -------------------------------------------------------------------------------- 1 | 10 10 2 | 5 3 9 1 6 3 3 10 5 6 3 | 19 11 18 10 10 14 13 16 10 16 4 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/data/2.sol: -------------------------------------------------------------------------------- 1 | YES 2 | YES 3 | NO 4 | YES 5 | YES 6 | YES 7 | YES 8 | YES 9 | YES 10 | YES 11 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename,n,m,range_a: 0..999999, range_x: 0..2000000) 2 | File.open(filename,'w') do |f| 3 | f.puts [n,m].join ' ' 4 | a = n.times.map{ rand(range_a) } 5 | f.puts a.join ' ' 6 | a = m.times.map{ rand(range_x) } 7 | f.puts a.join ' ' 8 | end 9 | end 10 | 11 | genOneFile('1.in',3,5,range_a: 1..10, range_x: 1..10) 12 | genOneFile('2.in',10,10,range_a: 1..10, range_x: 1..20) 13 | 14 | genOneFile '3.in',100,1000 15 | genOneFile '4.in',300,1000 16 | genOneFile '5.in',500,1000 17 | 18 | #we got lucky, random for the first time and it generate the case when a[i] + a[i] = x 19 | genOneFile '6.in', 1_000,1_000 20 | genOneFile '7.in', 4_000,5_000 21 | genOneFile '8.in', 6_000,6_000 22 | genOneFile '9.in', 8_000,8_000 23 | genOneFile '10.in', 10_000,10_000 24 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/pair_sum.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_pair_sum/pair_sum.odt -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/pair_sum.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_pair_sum/pair_sum.pdf -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/sol/sol: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_pair_sum/sol/sol -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/sol/sol-wrong: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds01_pair_sum/sol/sol-wrong -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/sol/sol-wrong.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | int n, m; 16 | cin >> n >> m; 17 | map data; 18 | for (int i = 0; i < n; i++) 19 | { 20 | int val; 21 | cin >> val; 22 | data[val]++; 23 | } 24 | for (int k = 0; k < m; k++) 25 | { 26 | int val; 27 | cin >> val; 28 | bool found = false; 29 | for (auto &x : data) { 30 | int need = val - x.first; 31 | if (need == x.first) { 32 | if (x.second > 0) { 33 | found = true; 34 | break; 35 | } 36 | } else { 37 | if (data.find(need) != data.end()) { 38 | found = true; 39 | break; 40 | } 41 | } 42 | } 43 | if (found) 44 | cout << "YES\n"; 45 | else 46 | cout << "NO\n"; 47 | } 48 | 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /exercise/ds01_pair_sum/sol/sol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | ios_base::sync_with_stdio(false); 14 | cin.tie(NULL); 15 | int n, m; 16 | cin >> n >> m; 17 | map data; 18 | for (int i = 0; i < n; i++) 19 | { 20 | int val; 21 | cin >> val; 22 | data[val]++; 23 | } 24 | for (int k = 0; k < m; k++) 25 | { 26 | int val; 27 | cin >> val; 28 | bool found = false; 29 | for (auto &x : data) { 30 | int need = val - x.first; 31 | if (need == x.first) { 32 | if (x.second > 1) { 33 | found = true; 34 | break; 35 | } 36 | } else { 37 | if (data.find(need) != data.end()) { 38 | found = true; 39 | break; 40 | } 41 | } 42 | } 43 | if (found) 44 | cout << "YES\n"; 45 | else 46 | cout << "NO\n"; 47 | } 48 | 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/1.in: -------------------------------------------------------------------------------- 1 | 10 10 2 | 0 2 1 3 4 4 0 0 4 1 3 | 0 1 4 1 1 3 0 4 2 3 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/1.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 3 4 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/10.sol: -------------------------------------------------------------------------------- 1 | 75 423 612 1225 1853 1892 2309 2324 2326 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/11.in: -------------------------------------------------------------------------------- 1 | 4 4 2 | 4 1 3 2 3 | 5 4 2 9 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/11.sol: -------------------------------------------------------------------------------- 1 | 2 4 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/12.in: -------------------------------------------------------------------------------- 1 | 4 2 2 | 1 2 3 4 3 | 1 1 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/12.sol: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/13.in: -------------------------------------------------------------------------------- 1 | 4 2 2 | 1 1 3 4 3 | 1 1 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/13.sol: -------------------------------------------------------------------------------- 1 | 1 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/14.in: -------------------------------------------------------------------------------- 1 | 3 5 2 | 1 2 3 3 | 20 30 50 6 7 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/14.sol: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/15.in: -------------------------------------------------------------------------------- 1 | 4 4 2 | 2 2 3 3 3 | 3 2 2 3 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/15.sol: -------------------------------------------------------------------------------- 1 | 2 3 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/16.in: -------------------------------------------------------------------------------- 1 | 2 2 2 | 100 50 3 | 50 100 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/16.sol: -------------------------------------------------------------------------------- 1 | 50 100 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/2.in: -------------------------------------------------------------------------------- 1 | 100 99 2 | 27 29 6 40 27 26 31 18 22 43 7 29 18 1 46 10 17 29 4 45 0 5 14 32 47 45 21 42 1 44 34 32 10 21 29 11 43 30 41 39 6 20 3 38 39 5 41 7 18 15 41 15 32 6 41 13 47 7 29 12 47 9 27 14 27 38 15 36 6 24 27 38 22 9 35 28 41 49 24 6 17 8 11 28 25 48 18 30 48 37 13 21 33 15 13 38 36 28 43 19 3 | 44 37 38 33 38 42 19 43 12 31 29 16 41 48 27 6 25 19 39 32 16 28 38 34 29 36 39 46 34 5 23 33 35 12 43 47 23 10 4 18 22 37 18 20 24 32 29 43 16 36 19 28 27 42 12 49 28 33 14 35 9 42 4 15 4 43 20 32 6 41 2 10 25 31 26 47 25 48 15 19 18 9 41 3 27 46 46 49 38 49 6 23 36 45 16 14 9 6 46 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/2.sol: -------------------------------------------------------------------------------- 1 | 3 4 5 6 9 10 12 14 15 18 19 20 22 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/3.in: -------------------------------------------------------------------------------- 1 | 10 500 2 | 82 74 44 176 23 56 174 217 29 45 3 | 3 192 238 106 77 158 218 124 217 178 63 47 35 193 208 158 134 161 233 60 105 160 1 218 15 144 100 36 75 84 105 173 156 148 93 148 91 34 157 109 20 53 219 106 100 191 158 184 72 93 146 146 171 56 207 52 235 87 6 62 78 38 230 12 87 173 77 127 142 103 238 8 174 97 237 60 182 43 211 56 86 193 164 95 47 187 245 11 36 64 121 232 92 80 162 16 117 238 47 91 81 102 103 34 50 35 124 17 142 218 209 248 194 34 240 37 200 10 137 222 208 117 54 160 16 49 135 213 127 196 78 115 152 23 45 107 87 223 81 52 230 123 154 192 59 127 241 188 176 229 109 192 144 211 57 214 88 0 230 117 32 155 114 218 108 137 204 114 135 88 107 43 101 189 216 55 92 217 153 0 126 59 177 147 167 33 97 212 34 45 141 96 140 79 57 77 28 49 115 135 77 241 178 31 156 161 168 8 160 106 130 167 104 61 80 7 235 151 137 106 198 86 198 42 240 72 125 235 197 138 28 227 186 224 225 49 212 227 45 16 20 155 10 67 88 122 120 18 61 12 111 100 81 53 67 137 173 133 59 150 17 121 140 103 57 237 72 213 140 139 103 218 186 44 51 227 114 98 202 109 87 76 119 2 199 37 87 15 146 139 163 71 151 12 184 144 215 43 231 27 154 7 35 13 241 84 27 86 217 27 160 140 116 141 23 16 235 127 160 154 68 154 74 215 212 15 161 154 165 177 172 160 212 84 96 135 119 42 165 80 242 232 79 43 54 174 87 170 174 94 217 113 212 43 56 244 147 134 74 191 38 59 237 63 18 132 62 135 30 204 241 56 191 195 64 170 71 154 135 66 106 237 34 208 158 133 136 63 176 121 230 3 130 198 175 240 190 80 132 83 170 49 132 107 229 121 83 139 91 132 13 18 69 234 64 20 188 85 228 169 207 78 167 248 198 8 227 43 60 148 72 30 47 224 38 238 57 66 64 245 161 125 158 219 88 176 3 189 164 144 103 7 40 7 222 162 225 165 153 157 132 30 223 35 209 161 237 222 41 59 242 110 164 43 27 206 154 25 160 139 200 66 182 181 154 139 67 221 89 100 133 96 13 105 138 164 64 13 61 217 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/3.sol: -------------------------------------------------------------------------------- 1 | 23 44 45 56 74 174 176 217 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/4.in: -------------------------------------------------------------------------------- 1 | 500 500 2 | 47 218 172 209 169 11 226 202 120 74 127 112 39 77 142 15 60 43 66 59 136 95 67 134 13 126 0 16 16 100 3 197 55 106 155 42 138 200 33 49 80 159 118 151 72 167 58 112 176 112 136 54 25 148 83 108 25 177 172 68 31 159 213 20 224 212 34 41 225 90 114 196 163 84 178 133 232 94 88 13 114 118 56 149 50 168 118 199 90 50 53 93 190 242 212 48 233 171 21 149 218 90 142 140 196 162 96 244 178 160 147 135 49 97 190 32 72 129 120 52 83 7 127 66 175 196 203 12 229 96 245 139 9 161 147 10 225 86 244 233 249 52 152 66 98 114 6 168 30 135 211 152 154 233 105 69 244 119 96 28 234 224 1 97 228 210 83 237 34 61 227 57 100 65 208 182 127 224 118 48 124 132 212 122 37 9 74 122 105 150 33 74 140 189 89 59 181 4 131 51 155 181 137 70 90 247 4 215 177 103 225 138 88 38 32 179 129 214 4 219 81 93 168 180 98 72 16 140 142 88 151 173 19 236 38 97 65 28 137 91 33 81 159 120 120 157 207 29 174 35 143 162 217 175 90 199 247 206 137 36 19 214 203 127 103 57 86 201 140 148 95 154 6 127 3 221 155 35 205 101 12 198 18 202 119 138 140 161 223 136 110 231 66 189 98 75 180 124 160 163 206 59 91 73 139 30 185 53 35 48 25 180 113 1 112 4 143 32 183 128 98 13 122 4 17 18 153 209 143 82 0 88 174 176 159 107 239 182 88 57 96 90 187 40 166 93 103 98 71 191 4 244 108 59 86 174 243 104 192 133 166 167 106 99 36 99 73 65 186 207 119 130 247 44 104 15 84 226 15 97 232 77 46 205 175 2 100 149 154 223 183 1 85 84 60 16 210 137 191 20 127 133 52 169 93 206 165 192 10 47 4 188 17 110 144 79 41 199 47 105 223 183 33 137 241 182 101 57 81 10 225 241 119 157 40 64 1 206 42 107 119 69 140 75 113 155 221 80 51 216 32 194 160 101 17 140 223 71 36 239 126 98 67 207 173 104 165 149 96 194 36 50 226 158 4 211 209 72 131 1 96 111 171 77 232 174 128 40 111 153 108 79 186 28 22 36 175 69 221 248 3 | 229 194 56 240 13 218 146 141 122 19 177 22 99 168 118 183 92 146 129 228 221 2 209 161 80 144 95 160 191 245 162 192 98 80 182 43 104 223 180 169 85 43 229 120 168 165 105 41 105 48 48 220 114 96 216 200 30 17 66 162 76 108 167 122 243 136 138 99 207 142 182 25 27 61 184 115 193 26 0 112 45 138 176 51 120 155 97 200 207 208 155 58 27 189 100 172 84 19 187 211 230 66 188 88 224 152 135 35 170 137 132 41 151 101 4 245 171 210 71 44 138 18 28 225 121 206 226 153 54 122 145 150 199 169 216 197 170 47 137 208 121 44 178 0 186 152 228 135 70 103 27 42 134 10 155 70 107 155 154 122 223 107 26 140 247 152 46 214 76 104 205 174 175 58 38 118 225 159 15 206 22 153 143 80 118 175 17 41 52 158 30 112 118 134 126 221 153 18 132 44 182 122 159 142 235 128 168 103 71 154 0 157 90 58 139 37 236 220 54 161 189 248 175 75 86 180 208 48 236 19 14 209 202 97 180 68 121 85 229 92 86 244 242 198 242 178 161 23 143 129 229 90 110 72 172 215 248 47 202 148 90 15 192 28 134 23 46 185 8 176 220 140 54 124 232 6 1 230 192 207 48 178 108 183 150 114 24 72 159 155 33 148 132 102 180 148 111 65 89 176 189 195 58 182 69 82 64 17 69 12 168 93 33 233 240 27 7 214 30 68 116 43 236 89 68 85 180 116 29 24 33 151 240 19 221 73 78 228 158 70 15 2 140 239 15 223 77 239 150 28 239 219 48 205 189 47 193 216 47 67 224 208 206 234 183 26 200 188 218 17 86 107 12 35 168 107 146 0 39 92 109 192 203 238 73 125 136 230 0 145 148 218 107 201 163 152 213 81 19 238 236 247 149 222 155 115 186 118 200 222 78 149 131 21 145 154 91 7 153 165 236 240 52 36 180 171 241 180 58 173 7 4 3 244 191 105 148 152 64 104 192 50 191 137 227 140 233 55 11 30 104 142 130 239 49 147 74 148 113 29 34 21 232 101 166 53 67 198 155 48 59 117 30 184 124 126 162 71 215 28 241 186 234 82 80 155 44 29 227 58 188 77 47 178 175 51 218 193 165 240 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/4.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 3 4 6 7 10 11 12 13 15 17 18 19 21 22 25 28 29 30 33 34 35 36 37 38 39 41 42 43 44 46 47 48 49 50 51 52 53 54 55 56 58 59 61 64 65 66 67 68 69 70 71 72 73 74 75 77 80 81 82 84 85 86 88 89 90 91 93 95 96 97 98 99 100 101 103 104 105 107 108 110 111 112 113 114 118 120 122 124 126 128 129 130 131 132 134 135 136 137 138 139 140 142 143 144 147 148 149 150 151 152 153 154 155 157 158 159 160 161 162 163 165 166 167 168 169 171 172 173 174 175 176 177 178 180 182 183 185 186 187 188 189 191 192 194 197 198 199 200 201 202 203 205 206 207 208 209 210 211 213 214 215 216 218 219 221 223 224 225 226 227 228 229 232 233 234 236 239 241 242 243 244 245 247 248 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/5.in: -------------------------------------------------------------------------------- 1 | 500 500 2 | 240 213 197 10 241 226 235 3 82 126 98 130 82 243 118 212 99 83 186 54 240 114 77 62 9 245 109 119 113 223 213 82 168 147 138 227 135 174 147 171 112 142 57 110 158 123 115 215 247 217 59 57 184 94 173 177 127 108 98 139 14 68 8 122 241 139 116 77 2 84 117 33 244 24 65 185 174 173 249 88 221 29 66 247 112 117 94 104 109 126 28 75 77 33 195 0 127 79 15 211 176 94 220 110 77 9 129 18 110 130 36 3 224 19 67 230 59 126 93 190 2 193 131 113 7 45 150 56 63 193 185 74 216 162 240 139 224 82 69 162 14 72 162 125 22 90 35 221 233 169 235 61 126 81 30 61 8 212 201 190 43 34 132 197 42 65 227 74 228 200 100 197 212 99 238 220 30 131 142 29 123 27 94 148 33 147 61 132 235 120 231 194 26 54 95 224 222 211 159 104 18 68 72 129 192 40 152 105 38 204 238 39 216 137 136 245 106 198 69 148 6 109 110 153 129 203 217 192 222 181 170 115 83 117 23 117 0 60 60 13 17 124 98 141 192 15 204 245 195 164 228 212 183 13 215 35 92 1 106 11 189 83 143 116 50 142 26 4 41 154 69 120 21 17 143 34 23 58 41 28 88 50 32 97 207 72 249 24 122 223 22 174 9 77 138 187 134 107 80 55 3 73 117 46 218 187 70 13 40 206 211 5 146 196 31 194 75 190 233 98 34 195 212 152 127 145 228 224 5 248 228 73 226 163 191 168 115 141 117 4 143 183 132 14 5 44 246 37 29 170 57 57 232 25 18 100 236 73 100 48 11 162 1 112 163 231 213 139 53 201 15 111 124 53 169 202 178 116 219 246 194 92 79 175 222 239 45 180 153 50 120 84 192 79 225 138 220 79 243 2 136 95 208 159 176 44 72 173 139 159 160 92 55 24 22 155 40 9 165 233 79 70 5 155 201 219 172 193 49 225 2 208 51 212 213 77 90 166 136 29 150 198 149 116 134 27 79 216 90 183 242 44 219 235 6 143 204 222 200 2 156 185 60 118 89 77 68 24 12 184 157 38 57 214 196 58 49 91 119 1 77 0 12 114 156 120 212 78 34 167 68 171 207 32 220 95 117 212 231 126 3 | 8 58 168 64 104 38 137 222 80 103 65 211 89 199 185 45 172 161 132 48 199 168 191 19 191 47 76 130 236 14 133 158 35 171 90 135 143 116 111 170 209 86 180 16 185 10 88 42 144 241 117 83 230 93 158 71 211 110 146 46 151 224 186 198 172 97 49 205 33 168 1 221 244 67 117 20 219 53 222 160 139 125 52 149 25 129 40 115 55 20 37 228 129 44 113 54 227 60 106 248 90 138 142 196 166 67 103 214 104 140 53 176 8 197 96 12 73 159 225 29 6 59 121 64 188 14 23 172 234 231 156 205 57 122 142 42 133 247 43 80 183 14 203 6 246 246 104 142 246 10 154 169 197 60 58 78 25 14 169 232 194 126 96 60 142 115 95 43 189 189 79 83 184 74 136 97 192 4 80 163 141 185 193 18 28 107 5 20 142 241 112 168 13 194 84 52 220 43 100 149 230 24 70 172 64 22 158 179 117 217 143 44 244 217 74 83 230 140 54 130 233 222 107 237 28 233 163 86 214 0 51 160 140 94 171 22 74 137 126 195 78 193 152 192 218 246 9 15 103 204 126 168 12 183 44 64 167 248 241 138 196 148 215 142 174 240 112 134 149 120 150 48 86 109 186 160 207 4 166 106 193 219 213 220 194 46 34 84 139 5 173 98 140 47 53 113 0 215 37 63 60 26 173 233 59 10 69 185 93 55 120 69 138 45 56 54 142 211 169 66 202 94 64 27 189 245 107 3 189 157 127 72 176 7 157 81 124 190 47 175 112 110 218 164 70 14 30 219 89 190 239 95 107 34 185 121 107 113 145 128 46 190 147 128 200 0 170 107 204 133 103 25 245 199 118 7 158 33 122 32 160 179 28 174 202 186 217 94 62 16 87 175 82 107 217 105 170 42 170 16 95 140 168 79 243 86 17 211 104 192 25 241 248 11 79 97 103 196 20 25 61 166 235 199 113 35 106 15 137 167 9 134 160 169 51 166 107 54 71 232 77 247 245 221 162 231 87 199 238 191 222 134 151 39 96 109 225 233 129 87 60 181 204 144 91 60 14 115 197 110 149 234 24 181 70 222 152 93 75 203 70 158 66 50 235 112 3 202 68 180 83 43 201 209 44 117 201 175 86 98 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/5.sol: -------------------------------------------------------------------------------- 1 | 0 1 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 22 23 24 25 26 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 46 48 49 50 51 53 54 55 56 57 58 59 60 61 62 63 65 66 67 68 69 70 72 73 74 75 77 78 79 80 81 82 83 84 88 89 90 91 93 94 95 97 98 100 104 105 106 107 109 110 111 112 113 115 116 117 118 120 122 124 125 126 127 129 130 132 134 135 136 137 138 139 141 142 143 145 146 147 148 149 150 152 154 156 157 158 159 160 162 163 164 166 167 168 169 170 171 172 173 174 175 176 180 181 183 184 185 186 189 190 191 192 193 194 195 196 197 198 200 201 202 203 204 207 211 213 214 215 217 218 219 220 221 222 224 225 227 228 230 231 232 233 235 236 238 239 240 241 243 244 245 246 247 248 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/6.in: -------------------------------------------------------------------------------- 1 | 500 500 2 | 112 54 224 32 2 111 105 193 4 246 63 207 218 247 31 163 79 119 57 111 110 230 91 143 228 144 180 185 46 42 45 237 23 35 112 136 243 97 75 183 40 74 124 95 113 208 137 2 195 171 147 182 169 120 166 119 219 232 23 78 238 186 133 150 77 140 51 47 6 36 159 211 78 65 112 61 105 123 21 60 91 204 2 241 239 78 244 145 90 97 76 113 187 212 111 155 151 18 181 67 147 128 114 112 209 142 74 200 125 234 197 0 221 4 75 190 245 91 58 239 221 97 195 166 133 87 55 137 95 153 191 218 78 224 57 219 103 161 150 100 156 56 156 87 21 96 178 76 204 245 119 3 66 106 150 132 26 93 225 94 58 175 84 248 30 163 214 171 61 235 194 153 230 225 168 207 189 158 135 180 37 153 21 138 173 176 140 234 141 59 181 186 156 236 0 68 150 182 117 129 109 219 25 181 241 121 177 122 90 69 53 26 8 245 114 68 115 175 6 14 162 37 72 170 171 200 227 53 131 241 193 149 122 169 61 9 50 91 193 222 48 150 199 159 223 54 40 142 190 80 164 203 114 94 243 204 73 84 51 1 152 241 40 211 44 243 106 174 64 40 217 121 217 192 99 235 64 28 16 173 121 112 200 38 36 98 111 192 22 226 241 43 46 189 170 198 201 146 202 142 101 245 0 112 56 57 172 152 76 117 194 237 85 40 105 248 17 132 9 20 206 127 221 155 220 6 34 171 32 187 169 43 121 195 223 75 227 238 135 76 86 194 35 85 71 136 182 71 56 156 223 136 1 150 100 171 82 21 8 59 44 158 157 201 67 61 149 93 107 153 1 78 59 128 222 180 174 106 24 101 100 61 58 116 176 55 56 53 196 205 1 197 216 138 35 66 108 19 204 54 159 163 33 242 8 248 213 114 230 242 123 97 102 88 237 197 24 188 156 195 137 215 127 106 114 184 21 122 129 145 42 114 102 23 19 228 112 125 155 126 131 174 110 45 66 95 118 136 111 150 17 170 173 40 79 37 19 43 44 73 2 63 37 21 74 45 202 71 115 80 90 177 201 88 197 27 136 96 226 153 220 124 128 0 165 134 182 5 121 194 137 155 154 67 81 49 16 208 6 66 3 | 103 78 209 38 85 133 153 236 150 100 236 141 130 227 174 58 143 165 142 124 201 134 213 125 141 220 178 43 193 98 244 159 204 87 240 139 8 173 83 20 69 72 209 92 93 23 40 65 210 66 137 206 5 240 2 125 83 181 73 83 134 7 38 74 44 222 94 122 77 170 192 71 105 243 165 67 164 218 246 87 46 204 110 162 222 154 27 58 25 179 190 98 104 168 34 111 153 196 111 10 156 94 245 122 118 55 87 61 121 78 49 93 197 55 187 239 12 108 101 126 69 154 123 19 18 58 85 211 153 110 6 38 67 103 99 193 90 196 169 156 40 198 30 130 88 4 157 174 71 102 98 100 6 3 9 134 190 94 85 221 212 199 128 137 185 199 216 206 53 249 183 153 141 39 163 28 67 81 29 87 14 120 170 145 99 207 169 18 197 181 50 187 243 135 138 16 140 109 29 11 107 63 36 60 83 5 46 69 135 39 104 141 133 0 68 63 115 32 189 34 157 110 236 79 86 157 235 220 168 48 164 35 6 173 148 85 219 227 58 52 196 108 92 135 216 165 76 127 228 231 30 40 58 116 94 192 70 192 245 143 113 68 249 39 195 16 135 248 162 21 99 188 228 224 85 84 126 54 66 125 204 82 128 129 74 166 243 142 14 93 33 219 129 197 212 163 18 160 80 183 192 124 27 81 147 186 31 210 29 38 126 39 59 156 223 55 174 69 152 137 196 135 101 228 130 240 30 7 54 14 140 53 157 89 89 173 3 142 29 25 107 112 131 82 103 221 143 26 124 125 57 107 95 93 245 124 79 9 248 191 100 14 215 156 176 206 202 247 7 17 60 154 111 146 242 16 136 15 80 3 4 186 143 4 27 240 63 62 153 64 134 51 190 163 132 41 1 169 212 143 148 41 159 195 90 184 44 77 6 76 40 189 153 159 86 110 211 155 96 79 222 160 154 5 176 141 4 148 16 163 193 167 54 84 44 50 57 95 106 249 105 237 141 120 85 105 227 106 204 116 216 122 69 56 125 128 160 109 164 183 172 197 153 118 104 43 249 131 90 170 24 101 163 88 247 121 201 219 101 30 144 220 135 94 123 74 162 166 24 6 218 32 231 127 93 12 186 236 60 133 4 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/6.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 3 4 5 6 8 9 14 16 17 18 19 20 21 23 24 25 26 27 28 30 31 32 33 34 35 36 38 40 43 44 46 48 49 50 51 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 71 72 73 74 76 77 78 79 80 81 82 84 85 86 87 88 90 93 94 95 96 98 99 100 101 102 103 105 106 107 108 109 110 111 112 113 115 116 118 120 121 122 123 124 125 126 127 128 129 131 132 133 134 135 136 137 138 140 141 142 143 144 145 146 147 150 152 153 154 155 156 157 159 162 163 164 165 166 168 169 170 172 173 174 176 178 181 183 184 185 186 187 188 189 190 191 192 193 195 196 197 198 199 201 202 204 206 207 209 211 212 213 215 216 218 219 220 221 222 223 224 227 228 235 236 237 239 242 243 244 245 246 247 248 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/7.sol: -------------------------------------------------------------------------------- 1 | 0 1 2 3 4 5 7 8 9 11 12 13 14 15 16 17 20 21 22 23 24 25 26 27 28 29 30 32 33 35 36 37 39 41 45 46 47 49 50 51 52 53 54 56 57 59 60 61 63 64 66 67 68 69 71 72 73 74 75 76 77 78 80 81 82 83 84 85 86 87 89 90 91 92 93 94 95 97 98 99 100 101 102 104 105 106 107 108 109 110 111 112 115 116 117 118 119 121 123 124 126 127 128 129 130 131 132 133 136 138 139 140 141 143 144 145 146 148 149 150 151 152 153 155 156 157 160 161 163 165 168 170 171 172 173 174 176 177 183 184 186 187 188 189 190 191 192 194 195 196 200 205 206 207 208 209 210 211 213 214 215 216 217 220 221 222 224 228 229 230 231 233 236 238 240 241 242 245 247 248 249 250 251 252 253 255 256 257 260 263 264 266 267 268 269 270 271 272 273 274 275 276 277 279 280 281 282 283 284 285 287 292 293 296 297 298 299 300 301 302 303 304 305 309 310 311 312 313 314 315 316 317 318 319 321 323 324 325 326 327 328 329 331 333 334 335 337 339 340 343 344 346 347 348 349 350 351 352 353 354 355 356 359 360 361 362 363 366 368 370 371 372 373 375 376 379 380 381 382 383 384 387 388 390 391 392 395 397 398 399 400 401 403 406 408 409 410 411 414 416 417 418 419 420 422 423 424 425 426 428 429 430 431 433 434 435 436 438 439 441 443 445 446 447 448 450 451 452 453 454 455 459 460 461 462 463 464 467 468 470 472 473 474 476 477 478 479 481 482 483 484 485 486 488 489 490 493 494 495 496 497 498 499 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/9.sol: -------------------------------------------------------------------------------- 1 | 440 604 650 858 991 1198 1367 1499 2 | -------------------------------------------------------------------------------- /exercise/ds02_intersection/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename, n,m = n) 2 | File.open(filename,"w") do |f| 3 | a = n.times.map{ rand([n,m].max / 2 ) } 4 | b = m.times.map{ rand([n,m].max / 2 ) } 5 | f.puts "#{n} #{m}" 6 | f.puts a.join ' ' 7 | f.puts b.join ' ' 8 | end 9 | end 10 | 11 | 12 | genOneFile("1.in",10,10) 13 | genOneFile("2.in",100,99) 14 | genOneFile("3.in",10,500) 15 | genOneFile("4.in",500) 16 | genOneFile("5.in",500) 17 | genOneFile("6.in",500) 18 | genOneFile("7.in",1000) 19 | genOneFile("8.in",5000) 20 | genOneFile("9.in",5000,10) 21 | genOneFile("10.in",10,5000) 22 | 23 | File.write("11.in",< 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int n,m; 9 | std::cin >> n >> m; 10 | 11 | //read input 12 | vector a,b; 13 | while (n--) { 14 | int tmp; 15 | std::cin >> tmp; 16 | a.push_back(tmp); 17 | } 18 | while (m--) { 19 | int tmp; 20 | std::cin >> tmp; 21 | b.push_back(tmp); 22 | } 23 | 24 | sort(a.begin(),a.end()); 25 | sort(b.begin(),b.end()); 26 | int lasta = a[0]-1; 27 | for (size_t i = 0;i < a.size();i++) { 28 | if (lasta == a[i]) continue; 29 | //a[i] is something new, we check it if b has it 30 | if (find(b.begin(),b.end(),a[i]) != b.end()) { 31 | cout << a[i] << " "; 32 | } 33 | lasta = a[i]; 34 | } 35 | cout << endl; 36 | } 37 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/1.in: -------------------------------------------------------------------------------- 1 | 10 10 2 | 52038 93239 46828 95054 57480 95718 24875 20591 19924 21668 3 | 74231 48456 56422 26830 11225 16016 70400 47277 93701 67336 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/1.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/10.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/11.in: -------------------------------------------------------------------------------- 1 | 3 3 2 | 1 2 3 3 | 3 2 1 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/11.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/12.in: -------------------------------------------------------------------------------- 1 | 3 2 2 | 1 2 3 3 | 1 2 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/12.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/13.in: -------------------------------------------------------------------------------- 1 | 3 3 2 | 10 20 30 3 | 10 20 30 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/13.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/14.in: -------------------------------------------------------------------------------- 1 | 3 2 2 | 9 9 9 3 | 9 9 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/14.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/15.in: -------------------------------------------------------------------------------- 1 | 1 1 2 | 1 3 | 1 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/15.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/16.in: -------------------------------------------------------------------------------- 1 | 2 2 2 | 5 5 3 | 5 5 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/16.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/17.in: -------------------------------------------------------------------------------- 1 | 0 0 2 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/17.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/18.in: -------------------------------------------------------------------------------- 1 | 0 1 2 | 10 3 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/18.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/2.in: -------------------------------------------------------------------------------- 1 | 100 99 2 | 50771 45304 86117 70374 9438 53018 87649 27072 51336 12878 29493 21664 65370 66411 25258 53283 72420 17785 29097 62735 61602 12333 14206 49556 5798 36480 58142 30408 17395 79193 24443 97159 8381 64150 67726 21737 67341 49861 22694 8988 47518 13637 16448 84506 81569 40619 57659 13892 55439 89235 82393 17043 64978 56325 64692 2576 98368 78835 96596 23385 39526 65386 10868 98480 94992 5615 32063 81799 66393 40966 38654 55807 36783 64228 61593 30770 90119 35963 51473 69886 24452 17715 21331 14010 26507 21566 26982 10007 30488 50009 13393 20764 30483 96532 27431 18465 77749 52413 67720 92549 3 | 92549 67720 52413 77749 18465 27431 96532 30483 20764 13393 50009 30488 10007 26982 21566 26507 14010 21331 17715 24452 69886 51473 35963 90119 30770 61593 64228 36783 55807 38654 40966 66393 81799 32063 5615 94992 98480 10868 65386 39526 23385 96596 78835 98368 2576 64692 56325 64978 17043 82393 89235 55439 13892 57659 40619 81569 84506 16448 13637 47518 8988 22694 49861 67341 21737 67726 64150 8381 97159 24443 79193 17395 30408 58142 36480 5798 49556 14206 12333 61602 62735 29097 17785 72420 53283 25258 66411 65370 21664 29493 12878 51336 27072 87649 53018 9438 70374 86117 45304 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/2.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/3.in: -------------------------------------------------------------------------------- 1 | 10 500 2 | 31539 38034 76352 40962 84998 65369 45433 88783 18723 82004 3 | 82004 18723 88783 45433 65369 84998 40962 76352 38034 31539 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/3.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/4.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/5.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/6.sol: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 1 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/7.in: -------------------------------------------------------------------------------- 1 | 1 10000 2 | 90717 3 | 90717 4 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/7.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/8.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/9.sol: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | -------------------------------------------------------------------------------- /exercise/ds02_is_reverse/data/gen.rb: -------------------------------------------------------------------------------- 1 | def genOneFile(filename, n,m = n,reverse = true) 2 | File.open(filename,"w") do |f| 3 | a = n.times.map{ rand(100000) } 4 | if reverse 5 | b = a.reverse 6 | b = b[0...m] 7 | else 8 | b = m.times.map{ rand(100000) } 9 | end 10 | f.puts "#{n} #{m}" 11 | f.puts a.join ' ' 12 | f.puts b.join ' ' 13 | end 14 | end 15 | 16 | 17 | genOneFile("1.in",10,10,false) 18 | genOneFile("2.in",100,99) 19 | genOneFile("3.in",10,500) 20 | genOneFile("4.in",500) 21 | genOneFile("5.in",500) 22 | genOneFile("6.in",500) 23 | genOneFile("7.in",1,10000) 24 | genOneFile("8.in",20000,0) 25 | genOneFile("9.in",50000,50000,false) 26 | genOneFile("10.in",50000) 27 | 28 | File.write("11.in",< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | namespace CP { 10 | 11 | template > 12 | class priority_queue 13 | { 14 | protected: 15 | T *mData; 16 | size_t mCap; 17 | size_t mSize; 18 | Comp mLess; 19 | 20 | void expand(size_t capacity) { 21 | T *arr = new T[capacity](); 22 | for (size_t i = 0;i < mSize;i++) { 23 | arr[i] = mData[i]; 24 | } 25 | delete [] mData; 26 | mData = arr; 27 | mCap = capacity; 28 | } 29 | 30 | void fixUp(size_t idx) { 31 | T tmp = mData[idx]; 32 | while (idx > 0) { 33 | size_t p = (idx - 1) / 2; 34 | if ( mLess(tmp,mData[p]) ) break; 35 | mData[idx] = mData[p]; 36 | idx = p; 37 | } 38 | mData[idx] = tmp; 39 | } 40 | 41 | void fixDown(size_t idx) { 42 | T tmp = mData[idx]; 43 | size_t c; 44 | while ((c = 2 * idx + 1) < mSize) { 45 | if (c + 1 < mSize && mLess(mData[c],mData[c + 1]) ) c++; 46 | if ( mLess(mData[c],tmp) ) break; 47 | mData[idx] = mData[c]; 48 | idx = c; 49 | } 50 | mData[idx] = tmp; 51 | } 52 | 53 | void print() { 54 | for (size_t i = 0;i < mSize;i++) 55 | std::cout << mData[i] << " "; 56 | std::cout << std::endl; 57 | } 58 | 59 | public: 60 | //-------------- constructor ---------- 61 | 62 | // copy constructor 63 | priority_queue(priority_queue& a) : 64 | mData(new T[a.mCap]()), mCap(a.mCap), mSize(a.mSize), mLess(a.mLess) 65 | { 66 | for (size_t i = 0; i < a.mCap;i++) { 67 | mData[i] = a.mData[i]; 68 | } 69 | } 70 | 71 | // default constructor 72 | priority_queue(const Comp& c = Comp() ) : 73 | mData( new T[1]() ), 74 | mCap( 1 ), 75 | mSize( 0 ), 76 | mLess( c ) 77 | { } 78 | 79 | 80 | // copy assignment operator 81 | priority_queue& operator=(priority_queue other) { 82 | using std::swap; 83 | swap(mSize,other.mSize); 84 | swap(mCap,other.mCap); 85 | swap(mData,other.mData); 86 | swap(mLess,other.mLess); 87 | return *this; 88 | } 89 | 90 | ~priority_queue() { 91 | delete [] mData; 92 | } 93 | 94 | //------------- capacity function ------------------- 95 | bool empty() const { 96 | return mSize == 0; 97 | } 98 | 99 | size_t size() const { 100 | return mSize; 101 | } 102 | 103 | //----------------- access ----------------- 104 | const T& top() { 105 | if (size() == 0) throw std::out_of_range("index of out range") ; 106 | return mData[0]; 107 | } 108 | 109 | //----------------- modifier ------------- 110 | void push(const T& element) { 111 | if (mSize + 1 > mCap) 112 | expand(mCap * 2); 113 | mData[mSize] = element; 114 | mSize++; 115 | fixUp(mSize-1); 116 | } 117 | 118 | void pop() { 119 | if (size() == 0) throw std::out_of_range("index of out range"); 120 | mData[0] = mData[mSize-1]; 121 | mSize--; 122 | fixDown(0); 123 | } 124 | }; 125 | 126 | } 127 | 128 | //----------------------------- MAKE YOUR CHANGE BELOW THIS LINE ONLY -------------------- 129 | class student { 130 | public: 131 | std::string name; 132 | int score; 133 | 134 | //constructor 135 | student() : name(), score() { } 136 | student(std::string aname,int ascore) : name(aname), score(ascore) { } 137 | 138 | }; 139 | CP::priority_queue pq; 140 | 141 | //----------------------------- MAKE YOUR CHANGE BOVE THIS LINE ONLY -------------------- 142 | int main(int argc, char *argv[]) { 143 | int N,K; 144 | std::cin >> N >> K; 145 | while (N--) { 146 | std::string a; 147 | int b; 148 | std::cin >> a >> b; 149 | pq.push(student(a,b)); 150 | } 151 | 152 | while (K--) { 153 | student tmp = pq.top(); 154 | pq.pop(); 155 | std::cout << tmp.name << " " << tmp.score << std::endl; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /exercise/ds03_heap_order/order.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds03_heap_order/order.odt -------------------------------------------------------------------------------- /exercise/ds03_heap_order/order.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/exercise/ds03_heap_order/order.pdf -------------------------------------------------------------------------------- /exercise/ds03_heap_order/solution/sol1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | namespace CP { 10 | 11 | template > 12 | class priority_queue 13 | { 14 | protected: 15 | T *mData; 16 | size_t mCap; 17 | size_t mSize; 18 | Comp mLess; 19 | 20 | void expand(size_t capacity) { 21 | T *arr = new T[capacity](); 22 | for (size_t i = 0;i < mSize;i++) { 23 | arr[i] = mData[i]; 24 | } 25 | delete [] mData; 26 | mData = arr; 27 | mCap = capacity; 28 | } 29 | 30 | void fixUp(size_t idx) { 31 | T tmp = mData[idx]; 32 | while (idx > 0) { 33 | size_t p = (idx - 1) / 2; 34 | if ( mLess(tmp,mData[p]) ) break; 35 | mData[idx] = mData[p]; 36 | idx = p; 37 | } 38 | mData[idx] = tmp; 39 | } 40 | 41 | void fixDown(size_t idx) { 42 | T tmp = mData[idx]; 43 | size_t c; 44 | while ((c = 2 * idx + 1) < mSize) { 45 | if (c + 1 < mSize && mLess(mData[c],mData[c + 1]) ) c++; 46 | if ( mLess(mData[c],tmp) ) break; 47 | mData[idx] = mData[c]; 48 | idx = c; 49 | } 50 | mData[idx] = tmp; 51 | } 52 | 53 | void print() { 54 | for (size_t i = 0;i < mSize;i++) 55 | std::cout << mData[i] << " "; 56 | std::cout << std::endl; 57 | } 58 | 59 | public: 60 | //-------------- constructor ---------- 61 | 62 | // copy constructor 63 | priority_queue(priority_queue& a) : 64 | mData(new T[a.mCap]()), mCap(a.mCap), mSize(a.mSize), mLess(a.mLess) 65 | { 66 | for (size_t i = 0; i < a.mCap;i++) { 67 | mData[i] = a.mData[i]; 68 | } 69 | } 70 | 71 | // default constructor 72 | priority_queue(const Comp& c = Comp() ) : 73 | mData( new T[1]() ), 74 | mCap( 1 ), 75 | mSize( 0 ), 76 | mLess( c ) 77 | { } 78 | 79 | 80 | // copy assignment operator 81 | priority_queue& operator=(priority_queue other) { 82 | using std::swap; 83 | swap(mSize,other.mSize); 84 | swap(mCap,other.mCap); 85 | swap(mData,other.mData); 86 | swap(mLess,other.mLess); 87 | return *this; 88 | } 89 | 90 | ~priority_queue() { 91 | delete [] mData; 92 | } 93 | 94 | //------------- capacity function ------------------- 95 | bool empty() const { 96 | return mSize == 0; 97 | } 98 | 99 | size_t size() const { 100 | return mSize; 101 | } 102 | 103 | //----------------- access ----------------- 104 | const T& top() { 105 | if (size() == 0) throw std::out_of_range("index of out range") ; 106 | return mData[0]; 107 | } 108 | 109 | //----------------- modifier ------------- 110 | void push(const T& element) { 111 | if (mSize + 1 > mCap) 112 | expand(mCap * 2); 113 | mData[mSize] = element; 114 | mSize++; 115 | fixUp(mSize-1); 116 | } 117 | 118 | void pop() { 119 | if (size() == 0) throw std::out_of_range("index of out range"); 120 | mData[0] = mData[mSize-1]; 121 | mSize--; 122 | fixDown(0); 123 | } 124 | }; 125 | 126 | } 127 | 128 | //----------------------------- MAKE YOUR CHANGE BELOW THIS LINE ONLY -------------------- 129 | class student { 130 | public: 131 | std::string name; 132 | int score; 133 | 134 | //constructor 135 | student() : name(), score() { } 136 | student(std::string aname,int ascore) : name(aname), score(ascore) { } 137 | 138 | // overload operator< and let std::less take care of comparison 139 | bool operator<(const student& other) const { 140 | if (score < other.score or (score == other.score && name > other.name)) return true; 141 | return false; 142 | } 143 | 144 | }; 145 | CP::priority_queue pq; 146 | 147 | //----------------------------- MAKE YOUR CHANGE BOVE THIS LINE ONLY -------------------- 148 | int main(int argc, char *argv[]) { 149 | int N,K; 150 | std::cin >> N >> K; 151 | while (N--) { 152 | std::string a; 153 | int b; 154 | std::cin >> a >> b; 155 | pq.push(student(a,b)); 156 | } 157 | 158 | while (K--) { 159 | student tmp = pq.top(); 160 | pq.pop(); 161 | std::cout << tmp.name << " " << tmp.score << std::endl; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /stl-cp/map_avl.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nattee/data-class/7b87def562ee25119f6c056e006a1837e4d54324/stl-cp/map_avl.h -------------------------------------------------------------------------------- /stl-cp/pair.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_PAIR_INCLUDED_ 2 | #define _CP_PAIR_INCLUDED_ 3 | 4 | //#include 5 | #include 6 | 7 | namespace CP { 8 | 9 | template 10 | class pair 11 | { 12 | public: 13 | T1 first; 14 | T2 second; 15 | 16 | // default constructor, using list initialize 17 | pair() : first(), second() {} 18 | 19 | // custom constructor, using list initialize 20 | pair(const T1 &a,const T2 &b) : first(a), second(b) { } 21 | 22 | // we have no destructor 23 | 24 | // equality operator 25 | bool operator==(const pair &other) { 26 | return (first == other.first && second == other.second); 27 | } 28 | 29 | // comparison operator 30 | bool operator<(const pair& other) const { 31 | return ((first < other.first) || 32 | (first == other.first && second < other.second)); 33 | } 34 | 35 | }; 36 | 37 | 38 | } 39 | 40 | #endif 41 | 42 | 43 | -------------------------------------------------------------------------------- /stl-cp/priority_queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_PRIORITY_QUEUE_INCLUDED_ 2 | #define _CP_PRIORITY_QUEUE_INCLUDED_ 3 | 4 | #include 5 | #include 6 | //#pragma once 7 | 8 | namespace CP { 9 | 10 | template > 11 | 12 | class priority_queue 13 | { 14 | protected: 15 | T *mData; 16 | size_t mCap; 17 | size_t mSize; 18 | Comp mLess; 19 | 20 | void expand(size_t capacity) { 21 | T *arr = new T[capacity](); 22 | for (size_t i = 0;i < mSize;i++) { 23 | arr[i] = mData[i]; 24 | } 25 | delete [] mData; 26 | mData = arr; 27 | mCap = capacity; 28 | } 29 | 30 | void fixUp(size_t idx) { 31 | T tmp = mData[idx]; 32 | while (idx > 0) { 33 | size_t p = (idx - 1) / 2; 34 | if ( mLess(tmp,mData[p]) ) break; 35 | mData[idx] = mData[p]; 36 | idx = p; 37 | } 38 | mData[idx] = tmp; 39 | } 40 | 41 | void fixDown(size_t idx) { 42 | T tmp = mData[idx]; 43 | size_t c; 44 | while ((c = 2 * idx + 1) < mSize) { 45 | if (c + 1 < mSize && mLess(mData[c],mData[c + 1]) ) c++; 46 | if ( mLess(mData[c],tmp) ) break; 47 | mData[idx] = mData[c]; 48 | idx = c; 49 | } 50 | mData[idx] = tmp; 51 | } 52 | 53 | void print() { 54 | for (size_t i = 0;i < mSize;i++) 55 | std::cout << mData[i] << " "; 56 | std::cout << std::endl; 57 | } 58 | 59 | public: 60 | //-------------- constructor ---------- 61 | 62 | // copy constructor 63 | priority_queue(const priority_queue& a) : 64 | mData(new T[a.mCap]()), mCap(a.mCap), mSize(a.mSize), mLess(a.mLess) 65 | { 66 | for (size_t i = 0; i < a.mCap;i++) { 67 | mData[i] = a.mData[i]; 68 | } 69 | } 70 | 71 | // default constructor 72 | priority_queue(const Comp& c = Comp() ) : 73 | mData( new T[1]() ), 74 | mCap( 1 ), 75 | mSize( 0 ), 76 | mLess( c ) 77 | { } 78 | 79 | 80 | // copy assignment operator 81 | priority_queue& operator=(priority_queue other) { 82 | using std::swap; 83 | swap(mSize,other.mSize); 84 | swap(mCap,other.mCap); 85 | swap(mData,other.mData); 86 | swap(mLess,other.mLess); 87 | return *this; 88 | } 89 | 90 | ~priority_queue() { 91 | delete [] mData; 92 | } 93 | 94 | //------------- capacity function ------------------- 95 | bool empty() const { 96 | return mSize == 0; 97 | } 98 | 99 | size_t size() const { 100 | return mSize; 101 | } 102 | 103 | //----------------- access ----------------- 104 | const T& top() { 105 | if (size() == 0) throw std::out_of_range("index of out range") ; 106 | return mData[0]; 107 | } 108 | 109 | //----------------- modifier ------------- 110 | void push(const T& element) { 111 | if (mSize + 1 > mCap) 112 | expand(mCap * 2); 113 | mData[mSize] = element; 114 | mSize++; 115 | fixUp(mSize-1); 116 | } 117 | 118 | void pop() { 119 | if (size() == 0) throw std::out_of_range("index of out range"); 120 | mData[0] = mData[mSize-1]; 121 | mSize--; 122 | fixDown(0); 123 | } 124 | 125 | 126 | }; 127 | 128 | } 129 | 130 | #endif 131 | 132 | 133 | -------------------------------------------------------------------------------- /stl-cp/queue.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_QUEUE_INCLUDED_ 2 | #define _CP_QUEUE_INCLUDED_ 3 | 4 | #include 5 | #include 6 | //#pragma once 7 | 8 | namespace CP { 9 | 10 | template 11 | class queue 12 | { 13 | protected: 14 | T *mData; 15 | size_t mCap; 16 | size_t mSize; 17 | size_t mFront; 18 | 19 | 20 | void expand(size_t capacity) { 21 | T *arr = new T[capacity](); 22 | for (size_t i = 0;i < mSize;i++) { 23 | arr[i] = mData[(mFront + i) % mCap]; 24 | } 25 | delete [] mData; 26 | mData = arr; 27 | mCap = capacity; 28 | mFront = 0; 29 | } 30 | 31 | void ensureCapacity(size_t capacity) { 32 | if (capacity > mCap) { 33 | size_t s = (capacity > 2 * mCap) ? capacity : 2 * mCap; 34 | expand(s); 35 | } 36 | } 37 | 38 | 39 | public: 40 | //-------------- constructor ---------- 41 | 42 | // copy constructor 43 | queue(const queue& a) : mData( new T[a.mCap]() ), mCap( a.mCap ), 44 | mSize( a.mSize ), mFront( a.mFront ) { 45 | for (size_t i = 0; i < a.mCap;i++) { 46 | mData[i] = a.mData[i]; 47 | } 48 | } 49 | 50 | // default constructor 51 | queue() : mData(new T[1]()), mCap(1), mSize(0), mFront(0) { } 52 | 53 | // copy assignment operator 54 | queue& operator=(queue other) { 55 | using std::swap; 56 | swap(mSize,other.mSize); 57 | swap(mCap,other.mCap); 58 | swap(mData,other.mData); 59 | swap(mFront,other.mFront); 60 | return *this; 61 | } 62 | 63 | ~queue() { 64 | delete [] mData; 65 | } 66 | 67 | //------------- capacity function ------------------- 68 | bool empty() const { 69 | return mSize == 0; 70 | } 71 | 72 | size_t size() const { 73 | return mSize; 74 | } 75 | 76 | //----------------- access ----------------- 77 | const T& front() const { 78 | if (size() == 0) throw std::out_of_range("index of out range") ; 79 | return mData[mFront]; 80 | } 81 | const T& back() const { 82 | if (size() == 0) throw std::out_of_range("index of out range") ; 83 | return mData[(mFront + mSize - 1) % mCap]; 84 | } 85 | 86 | //----------------- modifier ------------- 87 | void push(const T& element) { 88 | ensureCapacity(mSize+1); 89 | mData[(mFront + mSize) % mCap] = element; 90 | mSize++; 91 | } 92 | 93 | void pop() { 94 | if (size() == 0) throw std::out_of_range("index of out range") ; 95 | mFront = (mFront + 1) % mCap; 96 | mSize--; 97 | } 98 | 99 | 100 | }; 101 | 102 | } 103 | 104 | #endif 105 | 106 | 107 | -------------------------------------------------------------------------------- /stl-cp/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef _CP_STACK_INCLUDED_ 2 | #define _CP_STACK_INCLUDED_ 3 | 4 | #include 5 | #include 6 | //#pragma once 7 | 8 | namespace CP { 9 | 10 | template 11 | class stack 12 | { 13 | protected: 14 | T *mData; 15 | size_t mCap; 16 | size_t mSize; 17 | 18 | void expand(size_t capacity) { 19 | T *arr = new T[capacity](); 20 | for (size_t i = 0;i < mSize;i++) { 21 | arr[i] = mData[i]; 22 | } 23 | delete [] mData; 24 | mData = arr; 25 | mCap = capacity; 26 | } 27 | 28 | void ensureCapacity(size_t capacity) { 29 | if (capacity > mCap) { 30 | size_t s = (capacity > 2 * mCap) ? capacity : 2 * mCap; 31 | expand(s); 32 | } 33 | } 34 | 35 | public: 36 | //-------------- constructor ---------- 37 | 38 | // copy constructor 39 | stack(const stack& a) { 40 | this->mData = new T[a.mCap](); 41 | this->mCap = a.mCap; 42 | this->mSize = a.size(); 43 | for (size_t i = 0;i < a.size();i++) { 44 | mData[i] = a.mData[i]; 45 | } 46 | } 47 | 48 | // default constructor 49 | stack() { 50 | int cap = 1; 51 | mData = new T[cap](); 52 | mCap = cap; 53 | mSize = 0; 54 | } 55 | 56 | // copy assignment operator 57 | stack& operator=(stack other) { 58 | using std::swap; 59 | swap(mSize,other.mSize); 60 | swap(mCap,other.mCap); 61 | swap(mData,other.mData); 62 | return *this; 63 | } 64 | 65 | ~stack() { 66 | delete [] mData; 67 | } 68 | 69 | //------------- capacity function ------------------- 70 | bool empty() const { 71 | return mSize == 0; 72 | } 73 | 74 | size_t size() const { 75 | return mSize; 76 | } 77 | 78 | //----------------- access ----------------- 79 | const T& top() const{ 80 | if (size() == 0) throw std::out_of_range("index of out range") ; 81 | return mData[mSize-1]; 82 | } 83 | 84 | //----------------- modifier ------------- 85 | void push(const T& element) { // Theta(n) 86 | ensureCapacity(mSize + 1); 87 | mData[mSize] = element; 88 | mSize++; 89 | } 90 | 91 | void pop() { 92 | if (size() == 0) throw std::out_of_range("index of out range") ; 93 | mSize--; 94 | } 95 | 96 | 97 | }; 98 | 99 | } 100 | 101 | #endif 102 | 103 | 104 | -------------------------------------------------------------------------------- /stl-cp/test_list.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "list.h" 4 | #include 5 | 6 | 7 | bool test1() { 8 | CP::list a; 9 | 10 | CP::list::iterator it,it2; 11 | it =a.begin(); 12 | it2 = it; 13 | it2++; 14 | assert(it == it2); 15 | a.push_back(10); 16 | it2++; 17 | assert(it != it2); 18 | a.push_back(11); 19 | a.push_back(12); 20 | a.push_back(30); 21 | a.push_back(99); 22 | a.print(); 23 | 24 | a.clear(); 25 | assert(a.size() == 0); 26 | a.push_front(1); 27 | a.push_front(2); 28 | a.push_front(3); 29 | a.push_front(4); 30 | a.push_front(5); 31 | a.pop_front(); 32 | a.pop_back(); 33 | a.print(); 34 | 35 | assert(a.front() == 4); 36 | assert(a.back() == 2); 37 | 38 | return true; 39 | } 40 | 41 | bool test2() { 42 | CP::list l,l2; 43 | l.push_back("AAA1"); 44 | l.push_back("AAA2"); 45 | l.push_back("AAA3"); 46 | l.push_front("BBB"); 47 | l2 = l; 48 | CP::list::iterator it1,it2; 49 | it1 = l.begin(); 50 | it2 = l2.begin(); 51 | for (;it1 != l.end();it1++,it2++) { 52 | assert(*it1 == *it2); 53 | } 54 | l2.print(); 55 | return true; 56 | } 57 | 58 | bool test3() { 59 | CP::list > l,l2; 60 | l.push_back(std::make_pair("A",1)); 61 | l.push_back(std::make_pair("B",2)); 62 | l.push_back(std::make_pair("C",3)); 63 | l.push_back(std::make_pair("D",4)); 64 | 65 | auto it = l.begin(); 66 | it--;it--; 67 | 68 | for (size_t i = 0;i < l.size();i++) { 69 | l2.push_front(*it); 70 | it--; 71 | } 72 | for (auto& x: l2) { 73 | std::cout << x.first << ":" << x.second << std::endl; 74 | } 75 | 76 | return true; 77 | } 78 | 79 | int main() { 80 | if (test1()) std::cout << " ------------------------ TEST1 OK -------------------------" << std::endl; 81 | if (test2()) std::cout << " ------------------------ TEST2 OK -------------------------" << std::endl; 82 | if (test3()) std::cout << " ------------------------ TEST3 OK -------------------------" << std::endl; 83 | } 84 | -------------------------------------------------------------------------------- /stl-cp/test_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "map_bst.h" 6 | #include 7 | 8 | bool test1() { 9 | CP::map_bst m; 10 | m[10] = "A"; 11 | m[8] = "B"; 12 | m[6] = "C"; 13 | m[11] = "D"; 14 | m[12] = "E"; 15 | 16 | m.print(); 17 | m.insert(std::make_pair(13,"x")); 18 | m.insert(std::make_pair(7,"x")); 19 | m[5] = "E"; 20 | m.print(); 21 | m.erase(10); 22 | m.print(); 23 | m.erase(8); 24 | m.print(); 25 | m.erase(5); 26 | m.print(); 27 | 28 | auto result = m.insert(std::make_pair(12,"x")); 29 | assert(result.second == false); 30 | while (result.first != m.end()) { 31 | std::cout << result.first->first << ": " << result.first->second << std::endl; 32 | result.first++; 33 | } 34 | return true; 35 | } 36 | 37 | bool test2() { 38 | CP::map_bst m; 39 | m[1] = m[2] = m[3] = m[4] = m[20] = m [11] = m[5] = m[9] = m[7] = false; 40 | m[-4] = m[-2] = m[-1] = m[-3] = true; 41 | for (auto& x : m) { 42 | std::cout << x.first << ": " << x.second << std::endl; 43 | } 44 | 45 | 46 | return true; 47 | } 48 | 49 | class TestClass { 50 | public: 51 | std::string name; 52 | int value; 53 | 54 | TestClass() : name(""), value(0) { } 55 | 56 | TestClass(std::string n, int v) : name(n), value(v) { } 57 | 58 | TestClass(const TestClass &other) : name(other.name), value(other.value) { } 59 | 60 | bool operator==(const TestClass& other) const { 61 | std::cout << "comparing " << (*this) << " and " << other << std::endl; 62 | return other.name == name && other.value == value; 63 | } 64 | 65 | bool operator<(const TestClass& other) const { 66 | return name < other.name; 67 | } 68 | 69 | friend std::ostream& operator<<(std::ostream& os,const TestClass& c) { 70 | os << "(name:" << c.name << ", value:" << c.value << ")"; 71 | return os; 72 | } 73 | }; 74 | 75 | bool comparator(const TestClass &a,const TestClass &b) { 76 | return a.name > b.name; 77 | } 78 | 79 | class ComparatorClass { 80 | public: 81 | bool operator()(const TestClass &a,const TestClass &b) { 82 | return a.value < b.value; 83 | } 84 | }; 85 | 86 | typedef bool(*CompFunc)(const TestClass&, const TestClass&); // Function pointer type named "CompFunctor" 87 | 88 | bool test3() { 89 | CP::map_bst m; 90 | CP::map_bst m2; 91 | 92 | m[TestClass("somchai",1)] = 0.1; 93 | m[TestClass("nuttapong",1)] = 1.1; 94 | m[TestClass("nattee",1)] = 2.2; 95 | m[TestClass("vishnu",1)] = 3.1; 96 | m.print(); 97 | m[TestClass("somchai",1)] = 0.2; 98 | m[TestClass("somchai",2)] = 99; 99 | m.print(); 100 | m[TestClass("xxx",1)] = 11; 101 | m[TestClass("x1",2)] = 23; 102 | m2 = m; 103 | m2.print(); 104 | return true; 105 | } 106 | 107 | bool test4() { 108 | std::vector > data; 109 | 110 | data.push_back(std::make_pair(TestClass("somchai",1),99)); 111 | data.push_back(std::make_pair(TestClass("nuttapong",1),1.1)); 112 | data.push_back(std::make_pair(TestClass("nattee",1),2.2)); 113 | data.push_back(std::make_pair(TestClass("vishnu",1),3.1)); 114 | data.push_back(std::make_pair(TestClass("xxx",1),11)); 115 | data.push_back(std::make_pair(TestClass("x1",2),23)); 116 | 117 | 118 | 119 | CP::map_bst m; 120 | for (auto& x : data) { 121 | m[x.first] = x.second; 122 | } 123 | m.print(); 124 | 125 | CP::map_bst m2(comparator); 126 | for (auto& x : data) { 127 | m2[x.first] = x.second; 128 | } 129 | m2.print(); 130 | 131 | return true; 132 | } 133 | 134 | int main() { 135 | 136 | if (test1()) std::cout << "-------------------- TEST 1 OK -------------------" << std::endl; 137 | if (test2()) std::cout << "-------------------- TEST 2 OK -------------------" << std::endl; 138 | if (test3()) std::cout << "-------------------- TEST 3 OK -------------------" << std::endl; 139 | if (test4()) std::cout << "-------------------- TEST 4 OK -------------------" << std::endl; 140 | 141 | } 142 | -------------------------------------------------------------------------------- /stl-cp/test_pair.cpp: -------------------------------------------------------------------------------- 1 | #include "pair.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace CP; 9 | 10 | bool test1() { 11 | //default constructor 12 | CP::pair a; 13 | std::cout << "a = " << a.first << "," << a.second << std::endl; 14 | a.first = 1; 15 | a.second = 2; 16 | std::cout << "a = " << a.first << "," << a.second << std::endl; 17 | 18 | //copy constructor 19 | CP::pair b(a); 20 | std::cout << "b = " << b.first << "," << b.second << std::endl; 21 | 22 | //assignment operator 23 | CP::pair c; 24 | c = a; 25 | std::cout << "c = " << c.first << "," << c.second << std::endl; 26 | 27 | //custom constructor 28 | CP::pair d(10,20); 29 | std::cout << "d = " << d.first << "," << d.second << std::endl; 30 | return true; 31 | } 32 | 33 | 34 | 35 | bool test2() { 36 | CP::pair a(10,"vishnu"); 37 | std::cout << "a = " << a.first << "," << a.second << std::endl; 38 | 39 | CP::pair b(a); 40 | CP::pair c; 41 | std::cout << "a == b? " << (a == b ? "YES" : "NO") << std::endl; 42 | c = a; 43 | std::cout << "a == c? " << (a == c ? "YES" : "NO") << std::endl; 44 | c.second = "abc"; 45 | std::cout << "a == c? " << (a == c ? "YES" : "NO") << std::endl; 46 | return true; 47 | } 48 | 49 | 50 | bool test3() { 51 | CP::pair a,b,c; 52 | a.first = "abc"; 53 | a.second = "hahaha"; 54 | b = a; 55 | std::cout << "a < b? " << (a < b ? "YES" : "NO") << std::endl; 56 | b.first = "def"; 57 | std::cout << "a < b? " << (a < b ? "YES" : "NO") << std::endl; 58 | 59 | c = a; 60 | c.second = "zzz"; 61 | std::cout << "a < c? " << (a < b ? "YES" : "NO") << std::endl; 62 | std::cout << "b < c? " << (b < b ? "YES" : "NO") << std::endl; 63 | return true; 64 | } 65 | 66 | bool test4() { 67 | std::vector > v; 68 | v.push_back( CP::pair(10,"asdf")); 69 | v.push_back( CP::pair(10,"zzz")); 70 | v.push_back( CP::pair(10,"ddd")); 71 | v.push_back( CP::pair(5,"asdf")); 72 | v.push_back( CP::pair(3,"X")); 73 | v.push_back( CP::pair(1,"asdf")); 74 | std::sort(v.begin(),v.end()); 75 | for (size_t i = 0;i < v.size();i++) { 76 | std::cout << v[i].first << " " << v[i].second << std::endl; 77 | } 78 | return true; 79 | } 80 | 81 | 82 | 83 | int main() { 84 | if (test1()) std::cout << "---------------------------------------- Test1 OK!" << std::endl; 85 | if (test2()) std::cout << "---------------------------------------- Test2 OK!" << std::endl; 86 | if (test3()) std::cout << "---------------------------------------- Test3 OK!" << std::endl; 87 | if (test4()) std::cout << "---------------------------------------- Test4 OK!" << std::endl; 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /stl-cp/test_priority_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "priority_queue.h" 5 | 6 | 7 | bool test1() { 8 | int value[] = {1,2,3,4,5,6,7,8}; 9 | do { 10 | CP::priority_queue q; 11 | for (int i = 0;i < 8;i++) { 12 | q.push(value[i]); 13 | } 14 | assert(q.top() == 8); q.pop(); 15 | assert(q.top() == 7); q.pop(); 16 | assert(q.top() == 6); q.pop(); 17 | assert(q.top() == 5); q.pop(); 18 | assert(q.top() == 4); q.pop(); 19 | assert(q.top() == 3); q.pop(); 20 | assert(q.top() == 2); q.pop(); 21 | assert(q.top() == 1); q.pop(); 22 | } while (std::next_permutation(value,value+8) ); 23 | return true; 24 | } 25 | 26 | bool test2() { 27 | int value[] = {1,2,3,4,5,6,7,8}; 28 | do { 29 | CP::priority_queue> q; 30 | for (int i = 0;i < 8;i++) { 31 | q.push(value[i]); 32 | } 33 | assert(q.top() == 1); q.pop(); 34 | assert(q.top() == 2); q.pop(); 35 | assert(q.top() == 3); q.pop(); 36 | assert(q.top() == 4); q.pop(); 37 | assert(q.top() == 5); q.pop(); 38 | assert(q.top() == 6); q.pop(); 39 | assert(q.top() == 7); q.pop(); 40 | assert(q.top() == 8); q.pop(); 41 | } while (std::next_permutation(value,value+8) ); 42 | return true; 43 | } 44 | 45 | bool test3() { 46 | CP::priority_queue> q1; 47 | for (int i = 1;i <= 10;i++) q1.push(i); 48 | CP::priority_queue> q2 = q1; 49 | CP::priority_queue> q3; 50 | q3 = q2; 51 | 52 | assert(q1.top() == 1); q1.pop(); 53 | assert(q1.top() == 2); q1.pop(); 54 | assert(q1.top() == 3); q1.pop(); 55 | assert(q1.top() == 4); q1.pop(); 56 | assert(q1.top() == 5); q1.pop(); 57 | assert(q1.top() == 6); q1.pop(); 58 | assert(q1.top() == 7); q1.pop(); 59 | assert(q1.top() == 8); q1.pop(); 60 | assert(q1.size() == 2); 61 | assert(q1.top() == 9); q1.pop(); 62 | assert(q1.top() == 10); q1.pop(); 63 | assert(q1.empty()); 64 | 65 | assert(q2.top() == 1); q2.pop(); 66 | assert(q2.top() == 2); q2.pop(); 67 | assert(q2.top() == 3); q2.pop(); 68 | assert(q2.top() == 4); q2.pop(); 69 | assert(q2.top() == 5); q2.pop(); 70 | assert(q2.top() == 6); q2.pop(); 71 | assert(q2.top() == 7); q2.pop(); 72 | assert(q2.top() == 8); q2.pop(); 73 | assert(q2.size() == 2); 74 | assert(q2.top() == 9); q2.pop(); 75 | assert(q2.top() == 10); q2.pop(); 76 | assert(q2.empty()); 77 | 78 | assert(q3.top() == 1); q3.pop(); 79 | assert(q3.top() == 2); q3.pop(); 80 | assert(q3.top() == 3); q3.pop(); 81 | assert(q3.top() == 4); q3.pop(); 82 | assert(q3.top() == 5); q3.pop(); 83 | assert(q3.top() == 6); q3.pop(); 84 | assert(q3.top() == 7); q3.pop(); 85 | assert(q3.top() == 8); q3.pop(); 86 | assert(q3.size() == 2); 87 | assert(q3.top() == 9); q3.pop(); 88 | assert(q3.top() == 10); q3.pop(); 89 | assert(q3.empty()); 90 | 91 | return true; 92 | } 93 | 94 | typedef bool(*CompFunctor)(const int&, const int&); // Function pointer type named "CompFunctor" 95 | 96 | bool Compare(const int &a,const int &b) // The actual comparator function matching the CompFunctor signature 97 | { 98 | return a > b; 99 | } 100 | 101 | bool Compare2(const int& a,const int & b) // The actual comparator function matching the CompFunctor signature 102 | { 103 | return a < b; 104 | } 105 | bool test4() { 106 | //create two priority_queue with different comparator 107 | CP::priority_queue q(Compare); 108 | CP::priority_queue q2(Compare2); 109 | 110 | //add data 111 | for (int i = 0;i < 5;i++) { 112 | q.push(i); 113 | q2.push(i); 114 | } 115 | 116 | // check that q2 works as it should, popping out all data 117 | for (int i = 0;i < 5;i++) { 118 | assert(q2.top() == 5-i-1); q2.pop(); 119 | } 120 | 121 | //copy q backt q2 (now q2 should be ordered according to q 122 | q2 = q; 123 | for (int i = 0;i < 5;i++) { 124 | assert(q.top() == i); q.pop(); 125 | assert(q2.top() == i); q2.pop(); 126 | } 127 | return true; 128 | 129 | } 130 | 131 | int main() { 132 | if (test1()) std::cout << "---------------------- Test 1 OK -----------------------" << std::endl; 133 | if (test2()) std::cout << "---------------------- Test 2 OK -----------------------" << std::endl; 134 | if (test3()) std::cout << "---------------------- Test 3 OK -----------------------" << std::endl; 135 | if (test4()) std::cout << "---------------------- Test 4 OK -----------------------" << std::endl; 136 | } 137 | -------------------------------------------------------------------------------- /stl-cp/test_queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "queue.h" 4 | #include 5 | #include 6 | 7 | 8 | //typedef CP::queue Queue; 9 | // 10 | typedef CP::queue Queue; 11 | 12 | bool test1() { 13 | Queue q; 14 | assert(q.size() == 0); 15 | 16 | q.push(1); 17 | q.push(2); 18 | q.push(3); 19 | q.push(3); 20 | 21 | assert(q.front() == 1 && q.size() == 4); q.pop(); 22 | assert(q.front() == 2 && q.size() == 3); q.pop(); 23 | assert(q.front() == 3 && q.size() == 2); q.pop(); 24 | assert(q.front() == 3 && q.size() == 1); q.pop(); 25 | 26 | assert(q.size() == 0); 27 | 28 | return true; 29 | } 30 | 31 | bool test2() { 32 | Queue q; 33 | q.push(1); 34 | q.push(1); 35 | q.push(1); 36 | q.pop(); 37 | q.pop(); 38 | q.pop(); 39 | 40 | Queue s2 = q; 41 | 42 | try { 43 | int x = q.front(); 44 | std::cout << "x = " << x << std::endl; 45 | q.pop(); 46 | } catch (std::exception &e) { 47 | std::cout << "Caught an exception " << e.what() << std::endl; 48 | return true; 49 | } 50 | return false; 51 | } 52 | 53 | bool test3() { 54 | int nRun = 20; 55 | int nData = 1000000; 56 | for (int i = 0;i < nRun;i++) { 57 | Queue q; 58 | for (int j = 0;j < nData;j++) { 59 | q.push(j); 60 | } 61 | for (int j = 0;j < nData;j++) { 62 | assert(q.front() == j); 63 | q.pop(); 64 | } 65 | } 66 | return true; 67 | } 68 | bool test4() { 69 | Queue q; 70 | q.push(1); 71 | q.push(2); 72 | q.push(3); 73 | assert(q.back() == 3); 74 | q.pop(); 75 | q.push(4); 76 | assert(q.back() == 4); 77 | assert(q.front() == 2); 78 | q.pop(); 79 | assert(q.front() == 3); 80 | q.pop(); 81 | assert(q.back() == 4); 82 | assert(q.front() == 4); 83 | q.pop(); 84 | if (q.size() == 0) { 85 | return true; 86 | } else { 87 | return false; 88 | } 89 | } 90 | int main() { 91 | if (test1()) std::cout << "---------------------------------------- Test1 OK!" << std::endl; 92 | if (test2()) std::cout << "---------------------------------------- Test2 OK!" << std::endl; 93 | if (test3()) std::cout << "---------------------------------------- Test3 OK!" << std::endl; 94 | if (test4()) std::cout << "---------------------------------------- Test4 OK!" << std::endl; 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /stl-cp/test_stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "stack.h" 4 | #include 5 | #include 6 | 7 | 8 | //typedef CP::stack Stack; 9 | // 10 | typedef CP::stack Stack; 11 | 12 | bool test1() { 13 | Stack s; 14 | assert(s.size() == 0); 15 | 16 | s.push(1); 17 | s.push(2); 18 | s.push(3); 19 | s.push(3); 20 | 21 | assert(s.top() == 3 && s.size() == 4); s.pop(); 22 | assert(s.top() == 3 && s.size() == 3); s.pop(); 23 | assert(s.top() == 2 && s.size() == 2); s.pop(); 24 | assert(s.top() == 1 && s.size() == 1); s.pop(); 25 | 26 | assert(s.size() == 0); 27 | 28 | return true; 29 | } 30 | 31 | bool test2() { 32 | Stack s; 33 | s.push(1); 34 | s.push(1); 35 | s.push(1); 36 | s.pop(); 37 | s.pop(); 38 | s.pop(); 39 | 40 | Stack s2 = s; 41 | 42 | try { 43 | int x = s.top(); 44 | //we should get exception here 45 | std::cout << "x = " << x << std::endl; 46 | s.pop(); 47 | } catch (std::exception &e) { 48 | std::cout << "Caught an exception " << e.what() << std::endl; 49 | return true; 50 | } 51 | return false; 52 | } 53 | 54 | bool test3() { 55 | int nRun = 20; 56 | int nData = 1000000; 57 | for (int i = 0;i < nRun;i++) { 58 | Stack s; 59 | for (int j = 0;j < nData;j++) { 60 | s.push(j); 61 | } 62 | for (int j = 0;j < nData;j++) { 63 | s.pop(); 64 | } 65 | } 66 | return true; 67 | } 68 | 69 | int main() { 70 | if (test1()) std::cout << "---------------------------------------- Test1 OK!" << std::endl; 71 | if (test2()) std::cout << "---------------------------------------- Test2 OK!" << std::endl; 72 | if (test3()) std::cout << "---------------------------------------- Test3 OK!" << std::endl; 73 | // if (test3()) std::cout << "---------------------------------------- Test4 OK!" << std::endl; 74 | 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /stl-cp/test_unordered_map.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "unordered_map_oa.h" 5 | #include 6 | 7 | 8 | bool test1() { 9 | CP::unordered_map mymap; 10 | 11 | std::cout << "size = " << mymap.size() << std::endl; 12 | std::cout << "bucket_count = " << mymap.bucket_count() << std::endl; 13 | std::cout << "load_factor = " << mymap.load_factor() << std::endl; 14 | 15 | mymap["Bakery"]="Barbara"; // new element inserted 16 | mymap["Seafood"]="Lisa"; // new element inserted 17 | mymap["Produce"]="John"; // new element inserted 18 | 19 | std::string name = mymap["Bakery"]; // existing element accessed (read) 20 | mymap["Seafood"] = name; // existing element accessed (written) 21 | 22 | mymap["Bakery"] = mymap["Produce"]; // existing elements accessed (read/written) 23 | 24 | name = mymap["Deli"]; // non-existing element: new element "Deli" inserted! 25 | 26 | mymap["Produce"] = mymap["Gifts"]; // new element "Gifts" inserted, "Produce" written 27 | mymap["Gifts"] = "AAA"; 28 | 29 | mymap.print(); 30 | 31 | 32 | mymap.rehash(12); 33 | mymap.insert(std::make_pair("Test1","Test2")); 34 | mymap.print(); 35 | 36 | //test iterator 37 | for (auto& x: mymap) { 38 | std::cout << x.first << ": " << x.second << std::endl; 39 | } 40 | std::cout << "size = " << mymap.size() << std::endl; 41 | std::cout << "bucket_count = " << mymap.bucket_count() << std::endl; 42 | std::cout << "load_factor = " << mymap.load_factor() << std::endl; 43 | return true; 44 | } 45 | 46 | 47 | 48 | 49 | bool test2() { 50 | //test rehash 51 | CP::unordered_map map; 52 | for (int i = 0;i < 25;i++) { 53 | map[i] = i*2; 54 | } 55 | 56 | //test iterator 57 | for (auto& x: map) { 58 | std::cout << x.first << ": " << x.second << std::endl; 59 | } 60 | std::cout << "size = " << map.size() << std::endl; 61 | std::cout << "bucket_count = " << map.bucket_count() << std::endl; 62 | std::cout << "load_factor = " << map.load_factor() << std::endl; 63 | return true; 64 | } 65 | 66 | class TestClass { 67 | public: 68 | std::string name; 69 | int value; 70 | 71 | TestClass() : name(""), value(0) { } 72 | 73 | TestClass(std::string n, int v) : name(n), value(v) { } 74 | 75 | bool operator==(const TestClass& other) const { 76 | return other.name == name && other.value == value; 77 | } 78 | 79 | friend std::ostream& operator<<(std::ostream& os,const TestClass& c) { 80 | os << "(name:" << c.name << ", value:" << c.value << ")"; 81 | return os; 82 | } 83 | }; 84 | 85 | 86 | class TestHasher { 87 | public: 88 | std::size_t operator()(const TestClass &a) const { 89 | return a.value; 90 | } 91 | }; 92 | 93 | class TestEqual { 94 | public: 95 | bool operator ()(const TestClass &a,const TestClass &b) const { 96 | return a.value == b.value; 97 | } 98 | }; 99 | 100 | bool test3() { 101 | //test custom hash function with quadratic probing 102 | CP::unordered_map,CP::QuadraticProbing> map,map3; 103 | for (int i = 0;i < 12;i++) { 104 | std::stringstream s; 105 | s << "Test" << i; 106 | map[TestClass(s.str(),i%3)] = i*2; 107 | } 108 | map.print(); 109 | 110 | //test custom equal function with linear probing 111 | CP::unordered_map map2; 112 | for (int i = 0;i < 12;i++) { 113 | std::stringstream s; 114 | s << "Test" << i; 115 | map2[TestClass(s.str(),i%3)] = i*2; 116 | } 117 | map2.print(); 118 | map3 = map; 119 | map3.print(); 120 | 121 | return true; 122 | 123 | map[1] = 2; 124 | map[2] = 3; 125 | map[3] = 4; 126 | map[4] = 5; 127 | map[5] = 6; 128 | map[6] = 7; 129 | map[7] = 8; 130 | map[8] = 9; 131 | map[9] = 10; 132 | map[10] = 11; 133 | map[11] = 12; 134 | map[12] = 13; 135 | map[13] = 14; 136 | } 137 | 138 | int main() { 139 | 140 | if (test1()) std::cout << "-------------------- TEST 1 OK -------------------" << std::endl; 141 | if (test2()) std::cout << "-------------------- TEST 2 OK -------------------" << std::endl; 142 | if (test3()) std::cout << "-------------------- TEST 3 OK -------------------" << std::endl; 143 | 144 | } 145 | --------------------------------------------------------------------------------