Skip to content

Instantly share code, notes, and snippets.

@shihongzhi
shihongzhi / minStack.h
Created March 27, 2012 07:38
minStack.h
//google的一道面试题,可以考虑用vector替换deque
#ifndef MIN_STACK_H
#define MIN_STACK_H
#include <vector> //for vector
#include <utility> //for pair
#include <functional> //for std::less<T>
#include <limits>
template<typename T, typename Comparator = std::less<T> >
#include <sys/time.h> // for gettimeofday()
class StopWatch {
timeval started;
std::string msg;
public:
StopWatch(const std::string& m): msg(m)
{ gettimeofday(&started, NULL); }
//shihongzhi -- 2012.3.9
#include <stdio.h>
#include <string.h>
void KMP(char *T, char *P, int *pi)
{
int tLen = strlen(T);
int pLen = strlen(P);
int k = 0;
for (int i=0; i<tLen; ++i)
@shihongzhi
shihongzhi / LCS.cpp
Created March 8, 2012 08:54
Longest common subsequence problem
//http://poj.org/problem?id=1458
//http://poj.org/problem?id=1159 利用了滚动数组,要不然会内存溢出
//http://poj.org/problem?id=2250 需要打印,用一个数组记录path状态
//shihongzhi ---2012.3.8
#include <stdio.h>
int dp[100][100];
int LCS(char *a, char *b, int aLen, int bLen)
{
@shihongzhi
shihongzhi / maxSubarray.cpp
Created March 8, 2012 06:36
Maximum subarray problem
//http://poj.org/problem?id=1050
//http://poj.org/problem?id=2479
//shihongzhi --2012.3.8
#include <stdio.h>
int maxSubarray(int *A, int n)
{
int max = 0;
int sum = 0;
@shihongzhi
shihongzhi / LIS.cpp
Created March 7, 2012 14:06
Longest increasing subsequence
//http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence
//shihongzhi --2012.3.7
#include <stdio.h>
int M[100]; //相应长度的最长子序列结尾值的下标,且此结尾值为最小值的情况 M[0]没有存东西,在这里没有意义
int P[100]; //最长子序列的index索引
//时间复杂度为O(nlogk)
int LIS(int *X, int n)
@shihongzhi
shihongzhi / CarmSqrt.cpp
Created October 13, 2011 04:13
求得的是 开平方的倒数,具体原理不懂
/* 来至 Quake 3 的源码 */
//求得的是 开平方的倒数,具体原理不懂
float CarmSqrt(float x){
union{
int intPart;
float floatPart;
} convertor;
union{
int intPart;
float floatPart;
@shihongzhi
shihongzhi / lua_number2int.cpp
Created October 13, 2011 04:10
doutble2int
//http://blog.codingnow.com/2006/02/double_to_int_magic_number.html
//http://www.cnblogs.com/Xiao_bird/archive/2010/03/26/1696908.html %f--8bytes %lf--8bytes %d--4bytes
#include <stdio.h>
union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) { volatile union luai_Cast u; \
u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
int main()
{
@shihongzhi
shihongzhi / gist:1218657
Created September 15, 2011 06:11
zoj 3158
#include <stdio.h>
#include <stdlib.h>
#define MAX 7
int cake[MAX][MAX],diff[MAX][MAX-1];
int min = 100000;
void compute(int m, int n, int r, int v)
{
import maya.cmds as cmds
f1 = open(r"C:\Users\shihongzhi\Desktop\newproject\test\sparse3dpoint-absolute.txt")
i = 0
for line in f1.readlines()[40:]:
if i%6 == 0:
point = [float(t) for t in line[:-2].split(" ")]
#cmds.spaceLocator(p=(point[0],point[1],point[2]))
cmds.sphere(p=point,r=0.01)
elif i%6 == 1: