《代码仓库培训资料10851.docx》由会员分享,可在线阅读,更多相关《代码仓库培训资料10851.docx(49页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、49代码仓库代码仓库库目录:01.【数数学方法法】矩阵阵快速幂幂02.【数数学方法法】高斯斯消元(nave版版)03.【数数学方法法】高斯斯消元(mmid版版)04.【字字符串啊啊】Maanaccherr算法(回回文串)05.【字字符串啊啊】KMMP(字字符串匹匹配)06.【数数据结构构】线段段树(ZZKW单单点修改改)07.【数数据结构构】线段段树(RRMQ)08.【数数据结构构】线段段树(区区间加+赋值)09.【数数据结构构】Spplayy树(未未完全测测试)/!10.【数数据结构构】AVVL树(平平衡树)11.【图图论图论论】最小小生成树树(prrim)12.【图图论图论论】次小小生成树
2、树13.【图图论图论论】最大大流(DDiniic)14.【图图论图论论】LCCA+最最大生成成树(ttrucck)15.【动动态规划划】背包包01,多多重,完完全矩阵模板板#inccludde #inccludde#inccluddeusinngnaamesspacce sstd;typeedefflonngloong ll;consstinnt PP =99773;consstinnt NN=13;ll nn,m;struuct mattrixx ll aNN;int roww,coll; mattrixx():roww(N),cool(N)meemseet(a,0,sizzeoff(a);
3、/? mattrixx(intt x,intt y):roww(x),cool(y)meemseet(a,0,sizzeoff(a); ll*opeerattor(intt x)retturnn ax; mattrixx opperaatorr*(mattrixx x) maatriix ttmp ;for(intt i=0;i=n+1;i+)for(intt j=0;j=n+1;j+) tmppij=0;for(intt k=0;k=n+1;k+) tmmpij=(tmppij+aik*xkj)%P;retuurn tmpp;voiddopeerattor*=(mattrixx x)*tth
4、iss=*thiis* x; mattrixx opperaatorr(ll x) maatriix rret;for(intt i=0;i=1,tmpp*=tmmp)if(x&1)rett *=tmpp;retuurn rett;voidd prrintt()for(intt i=0;i=n+1;i+)for(intt j=0;j=n+1;j+) priintff(%dd ,aij); pputss();高斯消元元,判断断有无解解的#inccludde#inccludde#inccludde#inccludde#inccluddeusinngnaamesspacce sstd;typeede
5、fflonngloong LL;consstdooublle EEPS=1e-6;consstinnt NN=55;struuct mattrixxint aNN;int roww,coll; mattrixx():roww(N),cool(N)meemseet(a,0,sizzeoff(a); mattrixx(intt x,intt y):roww(x),cool(y) meemseet(a,0,sizzeoff(a);int*opeerattor(iint x)retturnn ax;voidd prrintt()for(intt i=0;iroww;i+)for(intt j=0;jc
6、oll;j+) priintff(%dd ,aij); pputss(); puuts();int Gauuss(mattrixx a,intt m,intt n)int x_ccnt =0;int coll, k;/ccol为为列号,k为行行号for(k=0,coll=0;km&cooln;+k,+cool)int r = k;/rr为第ccol列列的一个个1for(intt i=k;im;+i)if(aicool)r=i;if(!arcool) kk-;conntinnue;if(rr!=k)forr(intt i=coll;i=n;+i) sswapp( ari, aaki);for(i
7、ntt i=k+1;im;+i)if(aicool)/消消元for(intt j=coll;j=n;+j)aij=akj;for(intt i=k;im;+i)if(ain)reeturrn-1;if(kk=n)retturnn n-k;/返返回自由由元个数数高斯消元元,求出出一组解解的#inccludde #inccludde #inccludde #inccludde #inccludde usinngnaamesspacce sstd;consstinnt NN =10110;consstdooublle EEPS=1e-7;int m,n;doubble aNN,xN;int Gauu
8、ss(intt m,intt n)int coll=0, k=0;/ccol为为列号,k为行行号for(;km&cooln;+k,+coll)int r = k;for(intt i=k+1;ifaabs(arcool)r=i;if(ffabss(arcool)EPPS)k-;conntinnue;/列全为为0if(rr!=k)forr(intt i=coll;i=n;+i) sswapp(aki,ari);for(intt i=k+1;iEPPS)doubble t = aicool/akcool;for(intt j=coll;j=n;j+)aij-=akj*t; aaicool=0;fo
9、r(intt i=k ;iEPSS)retturnn-1;if(kk =0; i-)/回带带求解doubble temmp = ain;for(intt j=i+1; jn;+j) ttempp -= xj* aij; xi=(temmp / aii);retuurn00;Manaacheer算法法#inccludde#inccludde#inccludde#inccludde#inccluddeusinngnaamesspacce sstd;consstinnt NN=23333333;/220W/在oo(n)时间内内算出以以每个点点为中心心的最大大回文串串长度int Mannachher(
10、strringg stt)int lenn=st.sizze();int*p=newwinttlenn+1; memmsett(p,0,sizzeoff(p);int mx=0,id=0;for(intt i=1;ii)pi=miin(p2*id-i,mxx-i);elsee pi=1;whille(sti+pi=stti-pi)pi+;if(ii+pimxx)mxx=i+pi;idd=i;int ma=0;for(intt i=1;ilenn;i+)ma=maxx(ma,pi);deleete(p);retuurn ma-1;int maiin()/frreoppen(fuuck.in,rr
11、,sstdiin);charr sttN;whille(scaanf(%ss,st) sttrinng sst0=$#;for(intt i=0;sti!=00;i+) sst0+=stti; sst0+=#; prrinttf(%ddn,Mannachher(st00);retuurn00;KMP 字符串串匹配#inccludde#inccluddeusinngnaamesspacce sstd;typeedefflonngloong ll;consstinnt NN=10000077;consstinnt PP=1000000000007;charr aN,bN;booll maatN;i
12、nt nexxtN;ll ffN;voidd geetNeext(intt m)int i=0,j=-1; nexxt0=-1;whille(im)if(jj=-1|bi=bj)if(bb+i!=b+j)neexti=j;elsee neexti=neextj;elsse jj=nexxtj;voidd KMMP(intt n,intt m) memmsett(matt,0,sizzeoff(matt);int i=0,j=0; gettNexxt(m);whille(in&jm)if(jj=-1|ai=bj)i+,j+;elsee j=nexxtj;if(!i&!j)breeak;if(jj
13、=m) mmati=1;/prrinttf(matt%ddgeetnn,ii); jj=nexxtj;线段树(ZKWW大法)#inccludde#inccludde#inccludde#inccluddeusinngnaamesspacce sstd;consstinnt IINF=0x33f3ff3f33f;consstinnt NN=300001000;struuct linnetrree#deffinee lcc (t1)#deffinee rcc (t111)int miN,M;inliinevvoidd buuildd(intt n) M=1;whiile(Mn)M=1; M-; m
14、eemseet(mi,INFF,sizzeoff(mi);for(intt i=1+M;i=1;t-)mit=miin(milc,mirc);voidd chhangge(intt t,intt x)for(mit+=M=x,t=1;t;t=1) mmit=miin(milc,mirc);int queery(intt l,intt r)int anss = INNF;for(l+=M-1,r+=M+1;lr1;l=1,r=1)if(l&1)anss=minn(anss,mil1);if( r&1)anss=minn(anss,mir1);retuurn anss;T;int maiin()i
15、nt n,q,ordd,x,y;for(;sscannf(%dd,&n);) T.buiild(n);for(scaanf(%dd,&q);q-;) sscannf(%dd%d%d,&orrd,&x,&y);if(oord)T.chaangee(x,y);elsee prrinttf(%ddn,T.queery(x,y);retuurn00;线段树(RRMQ)#inccludde#inccludde#inccludde#inccluddeusinngnaamesspacce sstd;consstinnt IINF=0x33f3ff3f33f;consstinnt NN=60001000;in
16、t n,anss,m,aN;struuct nodde int l,r,id; nodde () nodde(intt x,intt y,intt z)l=x;r=y;id=z;bNN,cN;inliinebbooll cmmp1(nodde aa,nodde bb)reeturrn aa.lb.l;inliinebbooll cmmp2(nodde aa,nodde bb)reeturrn aa.rb.r;struuct linnetrree#deffinee lcc (t1)#deffinee rcc (t11)int lN,rN,maaN,miiN,M,taN,tiiN;inliinev
17、voidd buuildd(intt n) M=1;whiile(Mn)M=1; M-; meemseet(ma,0,sizzeoff(ma); meemseet(mi,INFF,sizzeoff(mi); meemseet(ta,0,sizzeoff(ta); meemseet(ti,INFF,sizzeoff(ti);for(intt i=1+M;i=1;t-)lt=llc,rt=rrc;inliinevvoidd doown(intt t)if(ttM)retturnn;/lleaff noode maalc=maxx(malc,tat); maarc=maxx(marc,tat); t
18、aalc=maxx(talc,tat); taarc=maxx(tarc,tat); taat=0; miilc=minn(milc,tit); miirc=minn(mirc,tit); tiilc=minn(tilc,tit); tiirc=minn(tirc,tit); tiit= INNF;inliinevvoidd maainttainn(intt t) maat=maax(malc,marc); miit=miin(milc,mirc);inliinevvoidd taag(intt t,intt x) maat=maax(mat,x); miit=miin(mit,x); taa
19、t=maax(tat,x); tiit=miin(tit,x);voidd chhangge(intt t,intt L,intt R,intt x)if(LL=lt&rt=R)taag(t,x);reeturrn;/iin doown(t);if(LL=miid)chaangee(lc,L,R,x);if(mmidM)/leaaf nnodee bbt-M=ct-M=noode(mit,maat,t-M);retuurn; doown(t); quueryy(lc); quueryy(rc); maainttainn(t);T;线段树(区区间加+赋值)#inccludde#inccludde
20、#inccludde#inccluddeusinngnaamesspacce sstd;consstinnt NN =26000000;int n,m;struuct linnetrree#deffinee lcc (t1)#deffinee rcc (t11)int lN,rN,M,taggN,suumN,leenN,SeetN;inliinevvoidd buuildd(intt n) M=1;whiile(Mn)M=1; M-;/gett M meemseet(summ,0,sizzeoff(summ); meemseet(tagg,0,sizzeoff(tagg); meemseet(
21、Sett,0,sizzeoff(Sett);for(intt i=1+M;i=M*2+1;i+)/leaafif(ii=1;t-)/fattherrs ssumt=suumlc+summrc; llt=llc, rt=rrc; llent=leenlc+lennrc;inliinevvoidd doown(intt t)if(ttM)Seett=taagt=0;retturnn;/leaaf nnodeeif(SSett) ssumlc=Settt*leenlc; ssumrc=Settt*leenrc; SSetlc=Settt; SSetrc=Settt; SSett=0; ttaglc=
22、taggrc=0;if(ttagt) ssumlc+=taagt*leenlc; ssumrc+=taagt*leenrc; ttaglc+=taagt; ttagrc+=taagt; ttagt=0;inliinevvoidd _ttag(intt t,intt x) suumt+=x*lennt; taagt+=x;inliinevvoidd _sset(intt t,intt x) suumt=x*lennt; Seett=x; taagt=0;voidd chhangge(intt t,intt L,intt R,intt x)if(LL=lt&rt=R)_ttag(t,x);reet
23、urrn; doown(t);if(LL=miid)chaangee(lc,L,R,x);if(mmid R)chaangee(rc,L,R,x); suumt=suumlc+summrc;voidd seet(intt t,intt L,intt R,intt x)if(LL=lt&rt=R)_sset(t,x);reeturrn;/iin doown(t);if(LL=miid)sett(lc,L,R,x);if(mmid R)sett(rc,L,R,x); suumt=suumlc+summrc;int queery(intt t,intt L,intt R)if(LL=lt&rt=R)
24、retturnn suumt; doown(t);int anss =0;if(LL=miid)anss+=quueryy(lc,L,R);if(mmid R)anss+=quueryy(rc,L,R);retuurn anss;T;Splaay-TTreee#inccludde#inccluddeusinngnaamesspacce sstd;struuct Noddeint keyy;/ssizee Nodde *l,*r,*f;/lleftt,riightt,faatheer;classs SSplaayTrreepubllic:voidd Innit()rrt=NULLL;voidd
25、Zaag(Nodde *x)/lefft rrotaate Noode *y=x-f;/yy iss thhe ffathher of x y-r = x-l;if(xx-l)x-l-f = y;/iif xx haas lleftt chhildd x-f =y-f;if(yy-f)/y iis nnot roootif(yy=y-f-l)y-f-l=x;/yy iff leeft chiildelsee y-f-r=x;/yy iss riightt chhildd y-f=x;x-l=y;voidd Ziig(Nodde *x)/rigght rottatee Noode *y=x-f;
26、/yy iss thhe ffathher of x y-l = x-r;if(xx-r)x-r-f=y; x-f = y-f;if(yy-f)if(yy=y-f-l)y-f-l=x;elsee y-f-r=x; y-f=x; x-r=y;voidd Spplayy(Nodde *x)whille(x-f) NNodee *p=x-f;if(!p-f)if(xx=p-l)Zigg(x);elsee Zaag(x);elsseiff(x=p-l)if(pp=p-f-l)Ziig(p);Ziig(x);elseeZigg(x);Zaag(x);elsse/xx=pp-rrif(pp=p-f-r)Z
27、aag(p);Zaag(x);elseeZagg(x);Ziig(x); rtt=x; Nodde *Finnd(intt x) Noode *T=rt;whille(T)if(TT-keey=x)Spplayy(T);reeturrn TT;elseeif(xkeey)T=T-l;elsee T=T-r;retuurn T;voidd Innserrt(intt x) Noode *T=rt,*fa=NULLL;whille(T) ffa=T;if(xxkeey)T=T-l;elseeif(xT-keey)T=T-r;elseeretturnn;/ttwo thee saame keyys
28、T=(Noode*)maallooc(sizzeoff(Nodde); T-keey=x; T-l=T-r=NULLL; T-f=fa;if(ffa)if(ffa-keyyx)fa-l=T;elsee faa-r=T; Spplayy(T);voidd Deelette(intt x) Noode *T=Finnd(x);if(NNULLL=T)retturnn;/eerroor rtt=Joiin(T-l,T-r); Nodde *Maxxnumm(Nodde *t) Noode *T=t;whille(T-r)T=T-r; Spplayy(T);retuurn T; Nodde *Minn
29、numm(Nodde *t) Noode *T=t;whille(T-l)T=T-l; Spplayy(T);retuurn T; Nodde *Lasst(intt x) Noode *T=Finnd(x); T=T-l;retuurn(Maxxnumm(T); Nodde *Nexxt(intt x) Noode *T=Finnd(x); T=T-r;retuurn(Minnnumm(T); Nodde *Joiin(Nodde *t1,Nodde *t2)if(NNULLL=t11)retturnn t22;if(NNULLL=t22)retturnn t11; Noode *T=Max
30、xnumm(t1); T-l=t2;retuurn T;voidd Spplitt(intt x,Nodde *&t11,Nodde *&t22) Noode *T=Finnd(x); t11=T-l;t2=T-r;voidd Innordder(Nodde *T)if(NNULLL=T)retturnn; Innordder(T-l); prrinttf(%dd-,T-keey); Innordder(T-r);voidd _DDeleete()DDeleete(rt);voidd Deelette(Nodde *T)if(NNULLL=T)retturnn; Deelette(T-l);
31、Deelette(T-r); frree(T);privvatee: Nodde *rt;/rroott;AVL树树/coodevvs12285 莯/byy cwww977#inccludde#inccludde#inccludde#deffinee INNF 00xfffffffff#deffinee BAASE 100000000usinngnaamesspacce sstd;int anss=0;struuct Noddeint x,bf,h;/bbf=bbalaancee faactoor,hh=heeighht Nodde *l,*r;classs AAVLTTreeepubllic:voidd Innit() rtt =NULLL;int H(Nodde *T)reeturrn(T=NUULL)?0:T-h;int BF(Nodde *l,Nodde *r)/gett baalannce facctorrif(NNULLL=l &NUULL=r)retturnn0;elseeif(NULLL= l)retturnn-r-h;elseeif(NULLL= r)retturnn ll-h;retuurn l-h - r-h; Nodde *Lrooratte(Nodde *a)/lefft rroraate Noode *b; b=a-r; a-r=b-l;
限制150内