Skip to content

fenilgmehta/Misc-Programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Misc-Programming

This repository contains various types of programs. Almost all program can compile with C++14. The following files require C++17: "Graph1.cpp", "Range Iteration.cpp"

Contents

How to start

Tips

  • It is highly suggested to do Competitive Programming in group so that everyone is motivated to continue even after small failures.
  • If one participates in a programming contest (e.g. on CodeForces), then it is necessary to solve the questions which one could not solve during the contest (i.e. upsolve questions which one could not solve during the contest).
  • One should try to solve problems which are slightly above their capacity but not too hard so that he/she does not get demotivated. Secondly, beginers should try to think of solution for 15-30 minutes, and:
    • If one is able to solve the question, then too they should see the tutorial for the question to see other peoples approach and also view the solution of those people whose code got accepted and took the least amount of time and memory.
    • If one is not able to solve the question, then see the tutorial/solution as beginners have a lot to learn and the brain won't get an idea to implement a new data structure required to solve the problem. And, one should understand the concepts required for the question throughly so that he/she is able to solve similar questions in future.
  • Lastly, continue practicing question because if one stops problem solving for just a month, even then the confidence goes down. So, regularly solve question and praticipate in competitions to be confident of your skills.

Useful Data Structures and Programs

  1. Competitive Programming Template (Big).cpp
  2. Competitive Prog-ramming Template (Small).cpp
  3. Graph1.cpp
    • Directed/Un-directed Unweighted Graph
      • Depth First Search (DFS)
      • Breadth First Search (BFS)
      • bipartite_graph_split
      • detect_cycle
      • find_cycle
    • Directed/Un-directed Weighted Graph
      • minimum_spanning_tree_kruskals
      • dijkstras (i.e. Dijkstra's shortest path)
      • bellman_ford
      • floyds_warshalls
      • bfs_0_1
    • GraphAdjMatrix - to store matrix of points
  4. Tree1.cpp
  5. Segment Trees.cpp
    • SegmentTree - optimized to space complexity of O(2*N)
      • Methods: size, resize, reset, operator[], build, setval, update, query, query_idx (for prefix sum)
    • SegmentTreeSimple - use complete binary tree and iterative algorithms
      • Methods: size, resize, reset, operator[], build, setval, update, query
    • SegmentTreeSimpleLazy - use complete binary tree and recursive algorithms
      • Methods: size, resize, reset, build, setval, query
  6. Binary Index Tree (Fenwick Tree).cpp
    • update
    • query
    • size
    • resize
    • reset
    • operator[]
    • find (returns the index with given cumulative frequency in O(log(n)) time, return -1 if not found)
  7. uBigInt.cpp - infinite size unsigned integers
    • Additions operators +, +=
    • Subtraction operators -, -=
    • Multiplication operators *, *=
    • Modulous operators %, %=
    • Binary shift operators <<=, >>=
    • Comparision operators ==, !=, <, <=, >, >=
    • to_dec()
    • to_hex()
    • to_uint64_t
    • factorial
  8. Matrix2d.cpp - 2 dimetional array
  9. Disjoint Set Union (Union Find).cpp
    • find_set
    • union_sets
    • in_same_set
  10. String Data Structures.cpp
    • Trie (Ternary Search Tree - space optimized Trie)
    • Palindrome check
    • Longest Palindrome using Manacher's Algorithm
    • longest_match - finds the maximum number of characters that match between two strings
    • min_edit_distance - the edit distance between two strings is the minimum number of operations required to transform one string into the other.
    • Suffix Tree
    • Suffix Array
  11. Prime Numbers.cpp
    • SieveOfEratosthenes
    • SmallestPrimeFactor
    • is_prime - using Fermat's Theorem
    • is_prime_dp - using Dynamic Programming
    • is_prime_simple - using school method
    • Read Primes
  12. Policy Data Structures and Hashing.cpp
  13. Notes
    • Generally recursive implementations are better than iterative implementation - testing and comparison
      • However, sometime iterative can also be good. It depends on the use case and the constraints under which the problem has to be solved. Example: if we have to traverse a binary tree in O(n) time and O(1) space complexity, then iterative implementation (i.e. Morris Traversal) is better than other recursion (because recursion has O(log(n)) space complexity) and stack based iterative implementations.
    • Lambda Function (C++11 and higher): https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11

C++ tips

  • IMPORTANT: read the Question carefully
  • IMPORTANT: see INPUT limits carefully
  • IMPORTANT: if unable to get a solution, then take a small break and think in different direction.
  • Should be cautious about
    • int overflow
    • array bounds
    • special test-cases
  • int dp[10000]; /* will give better speed as compared to */
    std::vector<int> dp(100000); /* however prefer using std::vector */
  • Use iterators to traverse a data structure
  • Using #define MOD 1000000007 will give better performance as compared to const int MOD = 1000000007;
  • if(a != 0) result++; can be written as result += !!a;
  • Use #include<bits/stdc++.h> for competitions but it will drastically increase the compilation time
  • The following snippet affects the input/output speed
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(20); cout << fixed;
  • While testing the program for large input on local machine, always use
    g++ -O2 fileName.cpp    # the -O2 will optimize the output file

Good code references

About

This repository contains all sorts of programs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published