- 博客(57)
- 收藏
- 关注
原创 最大子数组问题的线性解法-wikipedia
def max_subarray(A): max_ending_here = max_so_far = 0 for x in A: max_ending_here = max(0, max_ending_here + x) max_so_far = max(max_so_far, max_ending_here) return max_so_
2014-07-25 14:35:44 452
原创 最大子数组问题的边界处理
暴力法--O(n^2)def max_sum(A): sums=[0]*(len(A)+1) for i in range(1,len(A)+1): sums[i]=sums[i-1]+A[i-1] #sums[i] is sum[0,i) so sums[i]-sums[j]=sum[i,j) left,right,sumval=0,0,0
2014-07-23 21:07:19 391
原创 马尔可夫链文本处理
#include#include#include#include#includeusing namespace std;typedef deque Prefix;map > statetab;string NONWORD="\n";enum {MAXGEN=20,NPREF=2};void build(Prefix&,istream&);void add(Prefix&,co
2014-07-21 10:38:24 540
原创 list 的c实现
typedef struct Nameval Nameval;struct Nameval{ char *name; int value; Nameval *next;};Nameval *newitem(char *name,int value){ Nameval *newp; newp=(Nameval*)malloc(sizeof(Nameval)); newp->nam
2014-07-20 22:47:41 452
原创 python 哈希表实现
class HashTable: def __init__(self): self.size=11 self.slots=[None]*self.size self.data=[None]*self.size def hash_function(self,key,size): return key%size d
2014-07-18 00:56:27 985
原创 二分查找
def bsearch(a_list,item): first=0 last=len(a_list)-1 found=False while first<=last and not found: midpoint=(first+last)//2 if a_list[midpoint]==item: found=
2014-07-18 00:27:19 291
原创 实现有序列表和无序列表
class Node: def __init__(self,init_data): self.data=init_data self.next=None def get_data(self): return self.data def get_next(self): return self.next d
2014-07-17 23:31:47 570
原创 chap11 哈希表实现-开放寻址法
from functools import reducefrom operator import addclass HashTable: size=97 def hash(k,i): return (reduce(add,map(ord,str(k)),0)+i)%HashTable.size def __init__(self): se
2014-07-17 15:51:17 929
原创 chap11 哈希表链接法
from functools import reducefrom operator import addfrom random import seed,randintclass Node: def __init__(self,key,prev=None,next=None): self.key,self.prev,self.next=key,prev,nextcla
2014-07-17 15:25:59 327
原创 树的各类算法
class Tree: def __init__(self,key,left=None,right=None,par=None): self.key,self.left,self.right,self.par=key,left,right,pardef inorder(t): if t!=None: inorder(t.left)
2014-07-16 21:48:56 307
原创 chap10 单链表倒置
def reverse(self): pre,cur=self.nil,self.nil.next while(cur!=self.nil): next=cur.next cur.next=pre pre,cur=cur,next self.nil.next=pre
2014-07-16 15:36:31 307
原创 chap10 list 单链表实现
class Node: def __init__(self,key,next=None): self.key=key self.next=nextclass LinkList: def __init__(self): self.nil=Node('error',None) self.nil.next=self.nil
2014-07-16 15:17:36 310
原创 chap10-DLinkList
class Node: def __init__(self,key,prev=None,next=None): self.key=key self.prev=prev self.next=nextclass List: def __init__(self): self.nil=Node('error')
2014-07-16 13:18:07 362
原创 chap10-queue
class Queue: initial_capacity=10 def __init__(self): self.A=[0]*self.initial_capacity self.capacity=self.initial_capacity self.head=0 self.tail=0 def __repr
2014-07-16 11:16:04 317
原创 chap11 stack
class Stack: initial_capacity=10 def __init__(self): self.A=[0]*self.initial_capacity self.top=0 self.capacity=self.initial_capacity def expand(self): self.
2014-07-16 10:38:58 291
原创 heapsort
class heap: def __init__(self,A): self.A=A self.size=0 self.length=len(A)-1 def __setitem__(self,i,x): self.A[i]=x def __getitem__(self,i): return s
2014-07-16 08:55:39 289
原创 quicksort in clrs
def quick_sort(A,p,r): if p<r: q=partition(A,p,r) quick_sort(A,p,q-1) quick_sort(A,q+1,r)def partition(A,p,r): x=A[r] i=p-1 for j in range(p,r): if A[j
2014-07-16 08:26:37 275
原创 简单scheme计算器-python实现
class nil(object): """The empty list""" def __len__(self): return 0 def map(self,fn): return self def __repr__(self): return 'nil'nil=nil() # nil now refers to
2014-07-14 20:08:45 517
原创 hw7
class Tree: def __init__(self,entry,left=None,right=None): self.entry=entry self.left=left self.right=right def __repr__(self): args=repr(self.entry) if
2014-07-14 18:47:32 506
原创 用python实现二项树
class Tree: def __init__(self,entry,left=None,right=None): self.entry=entry self.left=left self.right=right def __repr__(self): args=repr(self.entry) if
2014-07-14 18:29:46 526
原创 chap5
def hire_assistant(A,n): best=0 for i in range(0,n): x=A[i] if A[i]>best: best=i return bestdef online_max(A,k,n): infinity=1000000 best=-infinity
2014-07-13 20:52:06 277
原创 最大子数组
def find_max_sum(A,low,high): if high==low: return low,high,A[low] else: mid=(low+high)//2 left_low,left_high,left_sum=find_max_sum(A,low,mid) right_low,right_h
2014-07-13 20:05:42 289
原创 chap2
def insertion_sort(A): for i in range(1,len(A)): x=A[i]; j=i-1; while( j>=0 and A[j]>x): A[j+1]=A[j] j-=1 A[j+1]=xA=[5,4,3,2,1]insertion_sort(A);print(A
2014-07-13 19:51:47 279
原创 求逆序对的nlgn算法
def inversion(A,p,r): #inversion return inversion num and sort the arr if(r-p<=1): return 0 else: q=(p+r)//2 n1,n2=inversion(A,p,q),inversion(A,q,r) n3=0 L=A[p:q
2014-07-13 16:46:15 393
原创 逆序对的nlgn算法
def inversion(A,p,r): #inversion return inversion num and sort the arr if(r-p<=1): return 0 else: q=(p+r)//2 n1,n2=inversion(A,p,q),inversion(A,q,r) n3=0 L=A[p:q
2014-07-13 16:45:01 830
原创 求逆序对的nlgn算法
def inversion(A,p,r): #inversion return inversion num and sort the arr if(r-p<=1): return 0 else: q=(p+r)//2 n1,n2=inversion(A,p,q),inversion(A,q,r) n3=0 L=A[p:q
2014-07-13 16:44:07 399
原创 python list的实现
#include#includeusing namespace std;const int InitCapacity=100;class List{private: int *arr; int capacity; int size; void quicksort(int *arr,int left,int right){ if(right-left>1){ int i,
2014-07-12 16:46:01 351
原创 305
#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); int k; cin>>k; list lst; int x; while(cin>>x&&x>=0){ lst.push_back(x); } list::iterator itr1=lst.be
2014-07-12 09:58:27 310
原创 304
#include#include#include#include#include#includeusing namespace std;struct record{ int coef; int exp; bool operator<(const record& r){ return exp<r.exp; }};int main()
2014-07-12 09:36:56 292
原创 212
#include#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); vector v1,v2,v3; int x; while(cin>>x&&x!=-1){ v1.push_back(x); } bool
2014-07-11 21:56:42 385
原创 211
#include#include#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); int n; cin>>n; vector vec; while(n--){ int x; cin>>x;
2014-07-11 21:22:44 384
原创 207
#include#include#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); long int n; cin>>n; cout<<n<<"="; if(n==1){ cout<<1<<endl; return 0; } map htable
2014-07-11 18:24:12 312
原创 206
#include#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); int a,n; cin>>a>>n; vector sum(n,0); for(int i=0;i<n;i++){ sum[i]=(n-i)*a; } int carry=0; f
2014-07-11 17:18:42 332
原创 205
#include#include#include#includeusing namespace std;int main(){ freopen("in.txt","r",stdin); vector vec; int n; cin>>n; double sum=0; for(int i=0;i<n;i++){ double x; cin>>x; vec.push
2014-07-11 17:10:16 352
原创 1010
#include#include#includeusing namespace std;int main(){freopen("in.txt","r",stdin);int t;cin>>t;int i=0;for(int i=0;iint a,b,c;cin>>a>>b>>c;coutc ? "true" : "false")}}
2014-07-11 11:47:09 310
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人