Skip to content

Instantly share code, notes, and snippets.

@shininglion
shininglion / basic_segment.hpp
Created May 16, 2013 08:09
basic structure of 2D point and segment objective: easy to use and extend
#include <memory>
#include "segment_attribute.hpp"
/****************************************
* structure definitions of segments *
****************************************/
enum SegmentType {Horizontal, Vertical, Italy};
template <typename T, typename U, typename Attribute=BasicSegmentAttribute>
@shininglion
shininglion / getter_and_setter.cpp
Created May 17, 2013 14:38
getter and setter in C++
class Data
{
private:
int data;
public:
void setData(const int &data);
const int& getData() const;
};
@shininglion
shininglion / with_rvalue_reference.cpp
Last active December 18, 2015 20:38
rvalue reference is a new scheme in C++11, which allows users to reference a "rvalue". With this new scheme, we can improve performance due to the avoidance of the unnecessary copy.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <ctime>
#define DIM 256
#define LIMIT 1000000
using namespace std;
class VectorWithRvalue
// ==UserScript==
// @name railwayTicketFormAutoField
// @namespace http://railway.hinet.net/ctno1.htm
// @description Change some element on login page
// @include http://*railway.hinet.net/ctno1.htm
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
var personalId = 'A123456789'; // 身份正字號
@shininglion
shininglion / bst.hpp
Created August 5, 2014 13:55
binary search tree
#ifndef BST_HPP
#define BST_HPP
#include <queue>
#include <algorithm>
#include <functional>
template <typename T>
struct BSTNode
#ifndef DISJOINT_SET_HPP
#define DISJOINT_SET_HPP
#include <cstring>
#include <algorithm>
#define USE_RVALUE
/************** disjoint set ****************
* table: table[x] is root of x *
* rank: rank[x] is depth of x *
@shininglion
shininglion / value_type.hpp
Created August 5, 2014 13:59
extract container's value_type
#ifndef VALUE_TYPE_HPP
#define VALUE_TYPE_HPP
// value is true when T is a container with value_type defined in structure
// otherwise, T is a primitive type
template <class T>
struct has_value_type
{
typedef char true_type;
typedef char false_type[2];
@shininglion
shininglion / mst.hpp
Created August 5, 2014 14:03
Minimum Spanning Tree (Kruskal's algorithm)
#ifndef MST_HPP
#define MST_HPP
#include <algorithm>
#include "value_type.hpp"
#include "disjoint_set.hpp"
/************************** Minimum Spanning Tree (MST) *************************
* find the cost of minimun spanning tree *
* using Kruskal to solve *
@shininglion
shininglion / rsg.hpp
Created August 5, 2014 14:06
Rectilinear Spanning Graph Construction
#ifndef RSG_HPP
#define RSG_HPP
#include "mst.hpp"
#include "bst.hpp"
template <typename T>
class XLess
{
private:
@shininglion
shininglion / kmp_algorithm.c
Created May 23, 2015 12:20
Knuth-Morris-Pratt algorithm
#include <string.h>
#define MAX 400005
/* Knuth-Morris-Pratt algorithm */
// used for string matching, divide into 2 part:
// compute prefix/failure function and string matching
/******** KMP algorithm - 1st part: prefix function *********
* compute the longest prefix that matches current suffix *
* i.e. abbab: the prefix function is 0 1 0 1 2 *