Skip to content

Instantly share code, notes, and snippets.

@junjiah
junjiah / word_ladder.cpp
Last active December 20, 2015 05:18
Word Ladder from LeetCode OnlineJudge
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
dict.insert(start); dict.insert(end);
queue< pair<string, int> > q;
unordered_set<string> visited;
q.push(make_pair(start, 1));
int res = 0;
while (!q.empty()) {
pair<string, int> top = q.front();
@junjiah
junjiah / kmp_occurrence.cpp
Last active December 20, 2015 16:29
POJ 3461 Oulipo : using KMP to find sub strings
/* Solving POJ 3461 Oulipo */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class KMP_algorithm {
private:
vector<int> next;
# Jie Bao, 2013-07-16
# baojie@gmail.com
# simple Naive Bayes classifier
import nltk
from nltk.corpus import movie_reviews
import random
import os
import json
@junjiah
junjiah / kmp_power_string.cpp
Last active December 20, 2015 22:19
POJ 2406 - Power Strings, using KMP
/* Solving POJ 2406 Power Strings */
#include <iostream>
#include <cstring>
using namespace std;
class KMP_algorithm {
public:
int find_base(const char* pattern) {
/*
@junjiah
junjiah / kmp_period.cpp
Last active December 21, 2015 00:28
POJ 1961 - Period, using 'next' array from KMP algorithm
/* Solving POJ 1961 Period */
#include <iostream>
#include <cstring>
using namespace std;
char input[1000005];
int next[1000006];
class KMP_algorithm {
@junjiah
junjiah / kmp_cat_name.cpp
Created August 13, 2013 12:02
POJ 2752 - Seek the Name, Seek the Fame : understand next[sz] from KMP
/* Solving POJ 2752 Seek the Name, Seek the Fame */
#include <iostream>
#include <cstring>
using namespace std;
char input[400005];
int next[400006];
int q[200003];
@junjiah
junjiah / gist:6255626
Last active December 21, 2015 05:19
NLTK learning 1. classification
We couldn’t find that file to show.
@junjiah
junjiah / jewelry.py
Created August 31, 2013 15:25
TopCoder problem, failed.
'''
for instructions, check
https://www.evernote.com/shard/s46/sh/5353dced-48b4-4158-9a8f-46607d810845/2dc3d625ef7263de7ccfeda0ab7dd0e2
and archived note in google keep.
btw: I failed this problem T^T
'''
def nCr(n, r):
from itertools import izip
return reduce(lambda x, y: x * y[0] / y[1], izip(xrange(n - r + 1, n+1), xrange(1, r+1)), 1)
@junjiah
junjiah / dailyzhihu_extract.py
Last active December 22, 2015 06:39
utilize daily.zhihu' api for info extraction and peewee for database management.
#coding:utf-8
import urllib2
import json
import peewee
from datetime import date, timedelta
from BeautifulSoup import BeautifulSoup
today = date.today()
one_day = timedelta(days=1)
date_i = date(2013,5,20)
@junjiah
junjiah / lxml_tutorial.py
Last active December 22, 2015 23:49
A brief tutorial to lxml module
from lxml import etree
# parse
parser = etree.XMLParser(ns_clean=True) # support other arguments
tree = etree.parse(some_file_like_obj)
# could be file name/path, file-like object, http/ftp url. but
# name/path and url are faster. or from string as following
tree = etree.fromstring(string, parser)
tree = etree.XML('<root><a><b/></a></root>')