Skip to content

Instantly share code, notes, and snippets.

View hucancode's full-sized avatar
🐼

Bang Nguyen Huu hucancode

🐼
View GitHub Profile
@hucancode
hucancode / read_mod.cpp
Last active August 25, 2022 00:23
read a very big number from string and return that number mod 1000000007
#define INF 1000000007
int read_mod(string num)
{
int ret = 0;
for (auto i = num.begin(); i != num.end(); i++) {
ret = (ret * 10 + (int)(*i) - '0') % INF;
}
return ret;
}
@hucancode
hucancode / lru_cache.cpp
Last active July 11, 2022 07:01
Least Recently Used cache mechanic, cache recently used value
/*
A cache is a component that stores data so future requests for that data can be served faster. The data stored in a cache might be the results of an earlier computation, or the duplicates of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache which is faster than recomputing a result or reading from a slower data store. Thus, the more requests that can be served from the cache, the faster the system performs.
One of the popular cache replacement policies is: "least recently used" (LRU). It discards the least recently used items first.
*/
#include <iostream>
#include <vector>
#include <map>
#include <string>
@hucancode
hucancode / matrix.h
Last active July 23, 2022 03:21
portable matrix library, support addition, multiplication, transposition, inversion
#include <stdexcept>
using namespace std;
class Matrix {
public:
Matrix(int, int);
Matrix();
~Matrix();
Matrix(const Matrix&);
Matrix& operator=(const Matrix&);
<div class="flex h-screen w-full items-center justify-center overflow-hidden bg-gray-900 group">
<div class="aspect-square w-72 overflow-hidden rounded-full bg-coin-head group-hover:animate-coin-flip"></div>
</div>
@hucancode
hucancode / sort.cpp
Created August 25, 2022 00:20
sorting algorithms
#include <iostream>
#include <algorithm>
const int n = 10;
const int MAX = 100000;
int a[n];
void print()
{
for (int i = 0; i < n; i++)
{
@hucancode
hucancode / function_object.cpp
Created August 25, 2022 00:23
c++ function pointer demo
#include <iostream>
using namespace std;
class AttackFunctionObject
{
public:
virtual void operator()(int damage)
{
printf("attack\n");
}
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
int thread_count = 0;
const int max_thread = 200;
const int n = 200000;
int a[n];
@hucancode
hucancode / insertion_sort.cpp
Created September 6, 2022 00:36
insert into a sorted vector
void insertion_sort(vector<int>& a, int& value) {
a.insert(upper_bound(a.begin(), a.end(), value), value);
}
@hucancode
hucancode / overlap.cpp
Last active September 14, 2022 02:21
check if 2 sorted lists of string have any overlaping item in O(logn)
// precondition: a & b are both sorted
bool overlap(vector<string>& a, vector<string>& b) {
auto i = a.begin();
auto j = b.begin();
auto n = upper_bound(i, a.end(), *b.rbegin());
auto m = upper_bound(j, b.end(), *a.rbegin());
while(i < n && j < m) {
if(*i == *j) {
return true;
}
@hucancode
hucancode / codeforces.cpp
Created October 12, 2022 13:14
codeforces template
#include <bits/stdc++.h>
using namespace std;
void solve() {
}
int main() {
int t;
cin>>t;