Skip to content

Instantly share code, notes, and snippets.

@KPCCoiL
KPCCoiL / fib.hs
Created June 3, 2014 06:28
memoization in Haskell
import qualified Data.Map as M
import Data.Map ((!))
fib :: Integer -> Integer
fib n = fst $ fib_aux M.empty n
fib_aux mp 0 = (1,mp)
fib_aux mp 1 = (1,mp)
fib_aux mp n | M.member n mp = (mp!n,mp)
| otherwise = (next,nmp)
// {{{ $VIMCODER$ <--------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <numeric>
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
sequence [funD (mkName "main")
[clause [] (normalB (appE (varE 'putStrLn) (stringE "Hello, TH!"))) []]
]
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
string s,t;
cin>>s>>t;
int n = s.length(),m = t.length();
int dp[n][m];
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> vec(n);
for (int& i : vec) cin>>i;
constexpr int INF = 1e8;
#include <algorithm>
#include <sstream>
#include <cmath>
#include <unordered_map>
using namespace std;
class PalindromePermutations {
public:
double palindromeProbability(string word) {
int len = word.size();
int odd = len%2;
#include <algorithm>
#include <vector>
using namespace std;
class BuildingHeights {
public:
int minimum(vector <int> heights) {
const int N = heights.size();
sort(heights.begin(),heights.end());
vector<int> accum(N);
accum[0] = heights[0];
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
using namespace boost::spirit;
int main(){
std::string input = "123, 456.789";
int n1;
double n2;
auto first = input.begin(),last = input.end();
bool success = qi::parse(first,last,
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
using namespace boost::spirit;
using namespace boost;
template<class It>
struct bool_grammar : qi::grammar<It,bool(),ascii::space_type> {
qi::rule<It,bool(),ascii::space_type> constant,formula;
bool_grammar() : bool_grammar::base_type(formula) {
using qi::lit;
// {{{ $VIMCODER$ <--------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}
#include <algorithm>
#include <vector>
#include <iostream>
#include <sstream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <numeric>